Updates and adjustments to tables chapter

This commit is contained in:
Kenneth John Odle 2023-10-31 09:50:13 -04:00
parent f4e326ea6f
commit 57642e91be

View File

@ -948,7 +948,7 @@ For example, the following code will produce the result see in table \ref{tb:sam
\end{tabular}
\end{table}
Note that I did not say ``in the table below'' because the \texttt{table} container is a float. That is, it allows \LaTeX{} to find what it thinks is the best location for the table in your document. This is often a point of confusion for beginners, because they assume that the table will appear where in their document where it appears in their file.
Note that I did not say ``in the table below'' because the \texttt{table} container is a float. That is, it allows \LaTeX{} to find what it thinks is the best location for the table in your document. This is often a point of confusion for beginners, because they assume that the table will appear in the same location in their document that it appears in their file.
The code is fairly straightforward:
@ -960,7 +960,9 @@ The code is fairly straightforward:
\item \verb+\\+ ends a row.
\end{itemize}
Typesetting tables in \LaTeX{} is straightforward, but it is not without its issues. In particular, tables that float can cause a lot of issues for users, and controlling the width of both the table and individual columns can be a challenge. However, if you do not wrap your table in a \texttt{table}, you cannot add a caption, as \LaTeX{} will give you a ``caption outside float'' error. As you can see in our sample table, \LaTeX{} has automatically sized the columns and table to fit our content, but that does not always make for an aesthetically pleasing table. It can also be difficult to easily break lines in a table cell, for which you would need to use \verb+\newline+ or add the \texttt{makecell} package.
Typesetting tables in \LaTeX{} is straightforward, but it is not without its issues. In particular, tables that float can cause a lot of issues for users, and controlling the width of both the table and individual columns can be a challenge. However, if you do not wrap your table in a \texttt{table}, you cannot add a caption, as \LaTeX{} will give you a ``caption outside float'' error.
As you can see in our sample table, \LaTeX{} has automatically sized the columns and table to fit our content, but that does not always make for an aesthetically pleasing table. Additionally, the table is not centered (although that can be fixed fairly easily by wrapping it in a \texttt{center} environment). It can also be difficult to easily break lines in a table cell, for which you would need to use \verb+\newline+ or add the \texttt{makecell} package.
There are solutions to this, and Klaus Höppner pointed out many of them in his ``Typesetting tables with \LaTeX{}'' article, which I recommend reading. (You can find it at \kref{https://tug.org/TUGboat/tb28-3/tb90hoeppner.pdf}{https://tug.org/TUGboat/tb28-3/tb90hoeppner.pdf}.)
@ -970,8 +972,11 @@ I have found a solution to all of my table problems by using the \texttt{tabular
\texttt{tabularray} offers two table environments: \texttt{tblr} and \texttt{longtblr}. I prefer the latter as it both offers the option to add a caption, and will break across pages, adding a very nice ``continued on next page'' message. (See table \ref{tb:expsizcom} in chapter \ref{ch:math} for an example.)
This is the basic format of a \texttt{longtblr} table that I use on a regular basis:
The package actually has far more options than the few I discuss in this chapter. These are merely the ones that I use most often. If you are having difficulty constructing a table, I urge you to read the package documentation, as the chances are high that you will find a solution there.
This is the basic format of a \texttt{longtblr} table that I use on a regular basis:\footnote{I actually have this set up as a template that I can pull into any \LaTeX{} file.}
\newpage
\begin{Verbatim}[frame=lines, numbers=left, xleftmargin=5mm, framesep=3mm, breaklines=true, label=\fbox{longtblr Example}]
\begin{longtblr}
[
@ -1037,8 +1042,8 @@ rowspec = {Q[t], Q[b]}
label = {tb:rowspec}
]{
width = {0.6\textwidth},
colspec = { X[1,l] X[2,l] },
rowspec = { Q[cyan7] Q[yellow3] },
colspec = { X[1,l] X[2,r] },
rowspec = { Q[t,azure9] Q[b,yellow9] },
hlines, vlines
}
This is some incredibly long text that is going to wrap around in this cell. & This is at the top.\\
@ -1129,11 +1134,11 @@ Fish & Chips & Mushy Peas \\
rows = {5mm, m, rowsep=1.5pt},
hlines, vlines
}
\SetCell[r=2]{l}Pasta & \SetCell[c=2]{r} Sauce & Meatballs \\
\SetCell[r=2]{l} Pasta & \SetCell[c=2]{r} Sauce & Meatballs \\
Fish & Chips & Mushy Peas \\
\end{longtblr}
The typical way of doing this requires use of the \Verb+\multicolmn+ command (which requires you to load the \texttt{multirow} package), and it requires that omitted cells must be empty. This is not the case with \texttt{tabularray}, however. As you can see from the source code, the words ``Meatballs'' and ``Fish'' have simply been omitted from the resulting table. We are also able to change the horizontal alignment by specifying it after we specify how many rows or columns to span.
The typical way of doing this requires use of the \Verb+\multicolumn+ command (which requires you to load the \texttt{multirow} package), and it requires that omitted cells must be empty. This is not the case with \texttt{tabularray}, however. As you can see from the source code, the words ``Meatballs'' and ``Fish'' have simply been omitted from the resulting table. We are also able to change the horizontal alignment by specifying it after we specify how many rows or columns to span. (I have found that if you do not specify an alignment, you still need to include an empty set of curly brackets or \LaTeX{} will throw up an error.)
\chapter{Math in \LaTeX{}}\label{ch:math}