Latex: creating multiple rows in one cell, using tabular
01 Sep 2022I looked at a few methods for splitting a cell into multiple rows. I use tabular
, and the easiest method for non-regular splits is to nest the desired split as a new tabular
in an existing tabular
. Included in this was figuring out what the heck @{}
meant as arguments to tabular
.
A basic MWE – some of the cells have more information than others, so I created a mini-table of one column at those places (FYI – I like tea):
\documentclass{article}
\usepackage[english]{babel}
\usepackage[letterpaper,top=2cm,bottom=2cm,left=3cm,right=3cm,marginparwidth=1.75cm]{geometry}
\begin{document}
\begin{tabular}{|c|c|c|}
\hline
Time & Drink & Snack\\
\hline
\hline
7:00am & \begin{tabular}{c}Tea\\ Water \end{tabular} & Pancake\\
\hline
9:00am & Tea & \begin{tabular}{c}Cheese \&\\ Crackers; \\ Apple\end{tabular}\\
\hline
\end{tabular}
\end{document}
When a mini-tabular (abusing notation / terminology, whatever) is created, the mini-tabular’s columns are padded with the inter-column space, and the cell where the mini-tabular table lives is also padded with the inter-column space. (These two answers from StackOverflow were particularly helpful 1, 2).
In other words, when we use the above code example, the columns are going to be padded with space more than necessary. To get around this, use @{}
where you’d like inter-column space removed. Since only one column is in my mini-tabular, I’ll do
\begin{tabular}{@{}c@{}} .... \end{tabular}
to remove the inter-column space on both sides of my one column table. [See below].
\documentclass{article}
\usepackage[english]{babel}
\usepackage[letterpaper,top=2cm,bottom=2cm,left=3cm,right=3cm,marginparwidth=1.75cm]{geometry}
\begin{document}
\begin{tabular}{|c|c|c|}
\hline
Time & Drink & Snack\\
\hline
\hline
7:00am & \begin{tabular}{@{}c@{}}Tea\\ Water \end{tabular} & Pancake\\
\hline
9:00am & Tea & \begin{tabular}{@{}c@{}}Cheese \&\\ Crackers; \\ Apple\end{tabular}\\
\hline
\end{tabular}
\end{document}
You can play around with where this space is left, and removed, for instance
\begin{tabular}{@{}c}
Update September 19, 2022 It quickly becomes annoying to use the syntax above. A command will do the trick instead,
\newcommand{\multrow}[1]{\begin{tabular}{@{}c@{}} #1 \end{tabular}}
Leading to another MWE example:
\documentclass{article}
\usepackage[english]{babel}
\usepackage[letterpaper,top=2cm,bottom=2cm,left=3cm,right=3cm,marginparwidth=1.75cm]{geometry}
\begin{document}
\newcommand{\multrow}[1]{\begin{tabular}{@{}c@{}} #1 \end{tabular}}
\begin{tabular}{|c|c|c|}
\hline
Temporary & Multi-Row & Temporary\\
\hline
\hline
Hi & \multrow{One, \\Two, \\ Three, \\Four,\\ Let's write\\ a few lines\\ more} & Pancake\\
\hline
Hi & Hi & \multrow{Testing 1,\\ Testing 2}\\
\hline
\end{tabular}
\end{document}