3

While trying to create tables with multiline cells, I run into some line spacing and alignment issues. The following code creates two tables:

\documentclass[10pt]{article}
\usepackage[english]{babel}
\usepackage{pbox}
\usepackage{multirow}

\begin{document}
\begin{table}[htp]
\centering
\begin{tabular}{lll}
cell11 & cell21 & cell31\\
cell12 & \begin{tabular}{l} cell221 \\ cell222 \\ \end{tabular} & cell32\\
\pbox[t]{5cm}{cell131 \\ cell132} & cell23 & cell33\\
cell14 & cell24 & \multirow{2}{*}{\pbox[t]{5cm}{cell341 \\ cell342}} \\
\\
cell15 & cell25 & cell35\\
cell16 & cell26 & cell36\\
\end{tabular}\vrule
\begin{tabular}{lll}
cell11 & cell21 & cell31\\
cell12 & cell221 & cell32\\
 & cell222 & \\
cell131 & cell23 & cell33\\
cell132 & & \\
cell14 & cell24 & cell341\\
 & & cell342\\
cell15 & cell25 & cell35\\
cell16 & cell26 & cell36\\
\end{tabular}
\caption{left: My attempts of a table with multiline cells. None of the attempts lead to the desired result. right: How it should look; the desired table with manually inserted empty cells, a lot of work and not really flexible.}
\end{table}
\end{document}

tables
The right table shows the desired result. It was created using empty cells, lacking flexibility in case of changing content. In the left table, the attempts to make multirow/multiline cells are shown. Note the line spacing and alignment issues. Lines 5 and 6 (without any multiline cells) have the correct distance from each other. A tabular within the table (cells 221 and 222) gives the correct intra- and intercell line spacing, however the horizontal alignment is incorrect. Additionally, cells 12 and 32 have an incorrect vertical alignment. Using pbox (cells 131 and 132) aligns the full row correctly. Line spacing within the cell is correct, but the spacing to the next cell (from 132 to 14) is to small. This might lead to the impression, that 132 and 14 belong together. The same problem occurs when using multirow (cells 341 and 342). Within the cell, line spacing is correct, but spacing between 342 and 35 is to small.
Question: how to create a table with multiline cells and correct spacing/alignment?


For me, the two answers provided by David and Steven both work equally well. Since I like to keep my documents as simple as possible, I decided to use David's solution, as no additional package is needed. Consequently, I accepted his answer. This is just my personal preference and no judgement on the quality of the quality of the soultions.

  • your nested tabular are vertically centred (the default) you want \begin{tabular}[t][@{}l@{}} for top aligned with no extra padding – David Carlisle Jul 31 '14 at 13:43
  • @DavidCarlisle Thanks, that solves one issue. But the nested tabular still has the wrong horizontal alignment... – Janek Jul 31 '14 at 13:45
  • ?? looks OK to me, I posted an answer – David Carlisle Jul 31 '14 at 13:56
  • My bad, it works! – Janek Jul 31 '14 at 14:03
3

enter image description here

\documentclass[10pt]{article}
\usepackage[english]{babel}


\begin{document}
\begin{table}[htp]
\centering
\begin{tabular}{lll}
cell11 & cell21 & cell31\\
cell12 & \begin{tabular}[t]{@{}l@{}} cell221 \\ cell222 \\ \end{tabular} & cell32\\
\begin{tabular}[t]{@{}l@{}} cell131 \\ cell132\end{tabular} & cell23 & cell33\\
cell14 & cell24 & \begin{tabular}[t]{@{}l@{}} cell341 \\ cell342\end{tabular} \\
cell15 & cell25 & cell35\\
cell16 & cell26 & cell36\\
\end{tabular}\vrule
\begin{tabular}{lll}
cell11 & cell21 & cell31\\
cell12 & cell221 & cell32\\
 & cell222 & \\
cell131 & cell23 & cell33\\
cell132 & & \\
cell14 & cell24 & cell341\\
 & & cell342\\
cell15 & cell25 & cell35\\
cell16 & cell26 & cell36\\
\end{tabular}
\caption{left: My attempts of a table with multiline cells. None of the attempts lead to the desired result. right: How it should look; the desired table with manually inserted empty cells, a lot of work and not really flexible.}
\end{table}
\end{document}
  • May I ask, what the {@{}l@{}} actually does? And where to find such a solution? – Janek Jul 31 '14 at 14:04
  • 1
    @Janek @{hello} (as described in the latex book, or I guess any tutorial on latex tables) adds the text hello to every row between the specified columns. In particular it adds that text in place of the default inter-column space, so @{} removes the cell padding. – David Carlisle Jul 31 '14 at 14:16
1

Here, I replace your \multirow with stacks. I define \mrstack{}{} to recreate the same vertical spacing as the right hand tabular. The mrstacks will be left-aligned.

\documentclass[10pt]{article}
\usepackage[english]{babel}
\newlength\svblskp
\svblskp=\the\baselineskip\relax
\usepackage{stackengine}
\def\stacktype{L}
\def\mrstack#1#2{\def\stackalignment{l}\stackunder[\svblskp]{\strut#1}{\strut#2}}
\begin{document}
\begin{table}[htp]
\centering
\begin{tabular}{lll}
cell11 & cell21 & cell31\\
cell12 &  \mrstack{cell221}{cell222}  & cell32\\
\mrstack{cell131}{cell132} & cell23 & cell33\\
cell14 & cell24 & \mrstack{cell341}{cell342}\\
cell15 & cell25 & cell35\\
cell16 & cell26 & cell36\\
\end{tabular}\vrule
\begin{tabular}{lll}
cell11 & cell21 & cell31\\
cell12 & cell221 & cell32\\
 & cell222 & \\
cell131 & cell23 & cell33\\
cell132 & & \\
cell14 & cell24 & cell341\\
 & & cell342\\
cell15 & cell25 & cell35\\
cell16 & cell26 & cell36\\
\end{tabular}
\caption{left: My attempts of a table with multiline cells. None of the attempts lead to the desired result. right: How it should look; the desired table with manually inserted empty cells, a lot of work and not really flexible.}
\end{table}
\end{document}

enter image description here

While the above answer is suitable in 99% of the cases, the OP notes that if the tabular were to be set in a different font size than \normalsize, the above approach would not produce the proper stacking gaps. So here is the remedy to that, using \f@size:

\documentclass[10pt]{article}
%\usepackage[english]{babel}
\usepackage{stackengine}
\def\stacktype{L}
\newlength\tmplen
\makeatletter
\def\mrstack#1#2{\def\stackalignment{l}%
  \tmplen=\f@size pt\relax%
  \stackunder[1.2\tmplen]{\strut#1}{\strut#2}}
\makeatother
\begin{document}
\begin{table}[htp]
\centering
\begin{tabular}{lll}
cell11 & cell21 & cell31\\
cell12 &  \mrstack{cell221}{cell222}  & cell32\\
\mrstack{cell131}{cell132} & cell23 & cell33\\
cell14 & cell24 & \mrstack{cell341}{cell342}\\
cell15 & cell25 & cell35\\
cell16 & cell26 & cell36\\
\end{tabular}\vrule
\begin{tabular}{lll}
cell11 & cell21 & cell31\\
cell12 & cell221 & cell32\\
 & cell222 & \\
cell131 & cell23 & cell33\\
cell132 & & \\
cell14 & cell24 & cell341\\
 & & cell342\\
cell15 & cell25 & cell35\\
cell16 & cell26 & cell36\\
\end{tabular}
\Huge\begin{tabular}{lll}
cell11 & cell21 & cell31\\
cell12 &  \mrstack{cell221}{cell222}  & cell32\\
\mrstack{cell131}{cell132} & cell23 & cell33\\
cell14 & cell24 & \mrstack{cell341}{cell342}\\
cell15 & cell25 & cell35\\
cell16 & cell26 & cell36\\
\end{tabular}\vrule
\begin{tabular}{lll}
cell11 & cell21 & cell31\\
cell12 & cell221 & cell32\\
 & cell222 & \\
cell131 & cell23 & cell33\\
cell132 & & \\
cell14 & cell24 & cell341\\
 & & cell342\\
cell15 & cell25 & cell35\\
cell16 & cell26 & cell36\\
\end{tabular}
\caption{left: My attempts of a table with multiline cells. None of the attempts lead to the desired result. right: How it should look; the desired table with manually inserted empty cells, a lot of work and not really flexible.}
\end{table}
\end{document}

enter image description here

  • Where did you get the 1ptand 4pt from? Are these fixed values i.e. if I change spacing for the whole table, will these stacks keep their spacing instead of also changing it? – Janek Jul 31 '14 at 13:52
  • 1
    @Janek I have revised my answer to take out that sensitivity by doing the following: 1) stacking based on skip between baselines (Long stack), rather than gap between lines (Short stack); and 2) strutting the content, so that inter-row spacing is automatically handled. Now, only 12pt is left, which is the \baselineskip for a 10pt font. One can't use \baselineskip directly, however, since it is redefined by tabular. – Steven B. Segletes Jul 31 '14 at 13:58
  • Your solution does work for this specific table. However, it does not seem to be very flexible. For example, if you change the font size in the table environment, the spacings are incorrect again. – Janek Jul 31 '14 at 14:11
  • 1
    @Janek I have revised again to save a copy of the value of \baselineskip in the preamble, so that I need not even use the 12pt of the prior edit... BUT I see from your later comment that you want to be able to handle font size change in the table itself. I'll address. – Steven B. Segletes Jul 31 '14 at 14:12
  • Actually I usually don't change font sizes in tables. I was merely testing the robustness of your solution as other users who get drawn to this question might encounter those issues. David's answer seems to be robust in this sense. At least I haven't found a way to distort his table yet. – Janek Jul 31 '14 at 14:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.