LaTeX tables: Basics
Tables are one of the most important aspects of typesetting when it comes to tabletop role-playing materials. Let's see, how do you handle it in LaTeX.
Contents
Table structure ↩
When it comes to tables, LaTeX provides two environments: tabular and table. The tabular environment's features will be discussed below. The optional table environment could be wrapped around the tabular environment to make it a float (similar to the figure environment for pictures). See the previous post for details on floats.
Tabular environment
The syntax of the tabular environment is simple:
\begin{tabular}[<pos>]{<spec>}
    <cells>
\end{tabular}
- pos(optional) - vertical justification of the table relative to the baseline:- c(default) - center
- t- top
- b- bottom
 
- spec- a list of columns the table has:- l- left-justified column
- c- centered column
- r- right-justified column
- p{<width>}- paragraph column of specified- <width>
- *{<n>}{<t>}-- <n>columns of- <t>type
- @{<decl>}- suppress inter-column space and insert- <decl>text instead
- |- vertical line
- ||- double vertical line
 
- cells- the content of the table. The following commands can be used:- &- column separator
- \\- new row
- \\[<height>]- new row, skipping the extra- <height>
- \newline- new line within a paragraph column
- \hline- horizontal line
- \cline{i-j}horizontal line from column- ito column- j
 
Here's an example of a small table using some of these basic commands:
\begin{table}[h!]
	\centering
	\caption{Basic table example}
	\vspace{1ex}
	\label{table:1}
	\begin{tabular}{r|c|l||p{5em}}
		\cline{1-3}
		right & center & left & empty \\
		aligned & aligned & aligned & space \\
		\cline{4-4}
		column & column & column & paragraph \newline cell with \newline line breaks \\
		\cline{1-3}
	\end{tabular}
\end{table}
Spacing
To change the default space between columns, modify the \tabcolsep length (default value of 6pt):
\setlength{\tabcolsep}{12pt}
To change the default space between rows, modify the \arraystretch command (default value of 1.0):
\renewcommand{\arraystretch}{1.5}
To increase the spacing around the rules, you can also insert \noalign{\smallskip} before or after the rule command, but it might break the vertical lines.
Spanning
By using the \multicolumn{<num>}{<align>}{<text>} command, you can merge multiple horizontal neighboring cells into one.
- num- number of columns to span
- spec- cell specification (text justification (- l,- c,- r), etc.)
- text- merged cell's contents
To do the same with the vertical cells, you will need the multirow package1: \multirow[vpos]{<num>}{<width>}[<vmove>]{<text>}.
- vpos(optional) - vertical text positioning:- c(default) - centered
- t- top alignment
- b- bottom alignment
 
- num- number of rows to span. Negative values will span above the current row. Fractional values are also permitted.
- width- merged cell's width. Can also take the- *value, meaning the text's natural width, or- =value to use the current column's width.
- vmove(optional) - fine tuning the vertical position of the text.
- text- merged cell's contents
You can also combine both commands together, by putting \multirow inside \multicolumn. The other way around will not work: \multicolumn{2}{c}{\multirow{2}{*}{big cell}}.
\begin{table}[h!]
	\centering
	\caption{Spanning example}
	\vspace{1ex}
	\label{table:2}
	\begin{tabular}{*{4}{|p{5em}}|}
		\hline
		cell 1:1 & cell 1:2 & \multicolumn{2}{c|}{x2 col span} \\
		\hline
		\multirow{2}{=}{x2 row span} & \multicolumn{2}{c|}{} & cell 2:4 \\
		\cline{4-4}
		& \multicolumn{2}{c|}{\multirow{-2}{10em}{\centering 2x2 span}} & cell 4:4 \\
		\hline
	\end{tabular}
\end{table}
Useful packages ↩
Array
The array package2 provides additional options for the column specification:
- m{<width>}- paragraph column with centered vertical alignment
- b{<width>}- paragraph column with bottom vertical alignment
- w{<align>}{<width>}- equivalent to- \makebox[<width>][<align>]{cell}.- <align>can be- l,- c, or- r.
- W{<align>}{<width>}- like- w, but warns if the box is overfull.
- !{<decl>}- insert- <decl>instead of a vertical line (does not suppress inter-column space like- @{<decl>}does)
- >{<decl>}- insert- <decl>before the column
- <{<decl>}- insert- <decl>after the column
📝 NOTE: In paragraph columns (p, m, b), \parindent is set to 0pt. To change it, use >{\setlength{\parindent}{<width>}}p, setting the <width> to your desired value.
The array package also provides the provides a possibility of defining your own column types:
\newcolumntype{<t>}{>{<before>}{<spec>}<{<after>}}
Tabularx
The tabularx package3 requires the array package, so you don't need to declare it if you use this one. Its eponymous environment allows for the creation of fixed width tables, and provides the "stretchable" X column type. If multiple X columns are present, they all be of equal width.
📝 NOTE: While you can change the widths of X columns manually, the process is not straightforward. Consult the package documentation (4.3 Column widths).
You can also change the text justification inside the X column by using the usual \raggedright, \raggedleft, or \centering commands, but must append the \arraybackslash command afterwards for it to work as intended.
\newcolumntype{Y}{>{\centering\arraybackslash}X}
\begin{table}[h!]
	\centering
	\caption{Tabularx example}
	\vspace{1ex}
	\label{table:3}
	\begin{tabularx}{\linewidth}{|c|X|l|Y|}
		\hline
		c & X & l & Y \\
		centered & stretched & left-aligned & stretched and centered \\
		\hline
	\end{tabularx}
\end{table}
Tabulary
The tabulary package4 is another take on expanding the tabular environment, providing the following column types:
- L-- \raggedrightparagraph column
- C-- \centeringparagraph column
- R-- \raggedleftparagraph column
- J- normally justified paragraph column
The width of all of these is proportional to the natural width of the longest entry in each column.
\begin{table}[h!]
	\centering
	\caption{Tabulary example}
	\vspace{1ex}
	\label{table:4}
	\begin{tabulary}{\linewidth}{|L|C|R|J|}
		\hline
		L & C & R & J \\
		left-aligned & centered & right-aligned & justified \\
		\hline
	\end{tabulary}
\end{table}
Summary ↩
For the referencing convenience, I've combined the information reviewed above in a single section.
Columns
| Type | Description | Package | 
|---|---|---|
| l | left-justified column | |
| c | centered column | |
| r | right-justified column | |
| p{<width>} | paragraph column | |
| m{<width>} | paragraph column with centered vertical alignment | array | 
| b{<width>} | paragraph column with bottom vertical alignment | array | 
| w{<align>}{<width>} | equivalent to \makebox[<width>][<align>]{cell} | array | 
| W{<align>}{<width>} | like w, but warns if the box is overfull | array | 
| X | stretchable paragraph column | tabularx | 
| L | \raggedrightparagraph column | tabulary | 
| C | \centeringparagraph column | tabulary | 
| R | \raggedleftparagraph column | tabulary | 
| J | normally justified paragraph column | tabulary | 
| *{<n>}{<t>} | <n>columns of<t>type | |
| @{<decl>} | suppress inter-column space and insert <decl>instead | |
| !{<decl>} | like @{<decl>}, but does not suppress inter-column space | array | 
| >{<decl>} | insert <decl>before the column | array | 
| <{<decl>} | insert <decl>after the column | array | 
| \| | vertical line | |
| \|\| | double vertical line | 
Commands
| Command | Description | Package | 
|---|---|---|
| & | column separator | |
| \\ | new row | |
| \\[<height>] | new row, skipping the extra <height> | |
| \newline | new line within a paragraph column | |
| \hline | horizontal line | |
| \cline{i-j} | horizontal line from column itoj | |
| \setlength{\tabcolsep}{<length>} | change spacing between columns | |
| \renewcommand{\arraystretch}{<ratio>} | change spacing between rows | |
| \multicolumn{<num>}{<align>}{<text>} | span <num>columns | |
| \multirow[vpos]{<num>}{<width>}[<vmove>]{<text>} | span <num>rows | multirow | 
| \newcolumntype{<t>}{>{<before>}{<spec>}<{<after>}} | define a new column type | array | 
The packages and their possibilities presented here are by no means all-encompassing. There is a multitude of packages handling the tabular contents, for example, the extensive tabu5, or ctable6 packages, but that will be left for you to discover on your own.
Discuss this post on Reddit



