Vladar's Blog

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}

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}

table-basic

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.

To do the same with the vertical cells, you will need the multirow package1: \multirow[vpos]{<num>}{<width>}[<vmove>]{<text>}.

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}

table-spanning


Useful packages

Array

The array package2 provides additional options for the column specification:

📝 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}

table-tabularx

Tabulary

The tabulary package4 is another take on expanding the tabular environment, providing the following column types:

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}

table-tabulary


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 \raggedright paragraph column tabulary
C \centering paragraph column tabulary
R \raggedleft paragraph 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 i to j
\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


  1. https://www.ctan.org/pkg/multirow

  2. https://www.ctan.org/pkg/array

  3. https://www.ctan.org/pkg/tabularx

  4. https://www.ctan.org/pkg/tabulary

  5. https://www.ctan.org/pkg/tabu

  6. https://www.ctan.org/pkg/ctable

#gamedev #guide #latex #software #ttrpg