5

I'm building a table and I need to have line breaks in each cell of one column. I have found the following example: How to add a forced line break inside a table cell

Here is the code:

\documentclass{article}
\newcommand{\head}[1]{\textnormal{\textbf{#1}}}
\usepackage[english]{babel}
\usepackage{blindtext}
\newcommand{\specialcell}[2][c]{%
  \begin{tabular}[#1]{@{}c@{}}#2\end{tabular}}

\begin{document}
\begin{tabular}{ccp{8cm}}
  \hline
  \head{Command} & \head{Declaration} & \head{Output}\\
  \hline
  \verb|\textrm| & \specialcell[t]{First one\\foobar\\again} & small test example\\ \hline
  \verb|\textrm| & \specialcell[t]{First one\\foobar\\again} & small test example\\ \hline
  \verb|\textrm| & \specialcell[t]{First one\\foobar\\again} & small test example\\
  \hline
\end{tabular}
\end{document}

The problem is that the elements in the second column are not left aligned. How can I left align them?

output of code

Edit: I have made the correction suggested by Bernhard, see below. The last problem now is that if the column only contains one entry, this entry is not aligned with the others in the second table. Can this also be solved.

\documentclass{article}

\usepackage[english]{babel}

\usepackage{makecell, booktabs, stackengine}
\renewcommand\theadfont{\bfseries}
\renewcommand\theadalign{lc}
\renewcommand\cellalign{tl}

\usepackage{stackengine}
\setstackEOL{\\}

\begin{document}

\begin{tabular}{ccp{8cm}}
  \toprule
  \thead{Command} & \thead{Declaration} & \thead{Output}\\
  \midrule
  \verb|\textrm| & \makecell{First one\\foobar\\again} & small test example\\
  \midrule
  \verb|\textrm| & \makecell[tl]{Fi} & small test example\\
  \midrule
  \verb|\textrm| & \makecell[tl]{First one\\foobar\\again} & small test example\\
  \bottomrule
\end{tabular}
\vskip 1cm

\begin{tabular}{ccp{8cm}}
  \toprule
  \thead{Command} & \thead{Declaration} & \thead{Output}\\
  \midrule
  \verb|\textrm| & \Shortunderstack[l]{First one\\foobar\\again} & small test example\\
  \midrule
  \verb|\textrm| & \Shortunderstack[l]{Fi} & small test example\\
  \midrule
  \verb|\textrm| & \Shortunderstack[l]{First one\\foobar\\again} & small test example\\
  \bottomrule
\end{tabular}

\end{document}

enter image description here

Edit 2: In certain cases, there is still a problem with the solution with \eqparbox. Please see the code and screenshot below. As cou can see it it no correctly horizontally aligned.

\documentclass{article}

\usepackage[english]{babel}
\usepackage{blindtext}
\usepackage{lipsum}

\usepackage{makecell, booktabs}
\renewcommand\theadfont{\bfseries}
\renewcommand\theadalign{lc}
\renewcommand\cellalign{tl}

\usepackage{eqparbox}

\begin{document}

\begin{tabular}{ccp{6cm}}
  \toprule
  \thead{Command} & \thead{Declaration} & \thead{Output}\\
  \midrule
  \verb|\textrm| & \eqparbox{Col}{First one\\foobar\\again} & \blindtext \\
  \midrule
  \verb|\textrm| & \eqparbox{Col}{Fi}% 
  & \blindtext \\
  \midrule
  \verb|\textrm| & \eqparbox{Col}{First one\\foobar\\again} & \blindtext \\
  \bottomrule
\end{tabular}

\end{document}

enter image description here

  • What do you mean by vertical spacing? What output are you expecting? (Also it would be helpful to wrap your code fragments into a minimal document.) – Alan Munn Jan 23 '16 at 21:47
  • @AlanMunn I have edited my question – machinery Jan 23 '16 at 22:52
  • The reason they are not left aligned is because you've declared the tabular in the \specialcell to be c. Make it l and they will be left aligned. BTW, given your edit, the title doesn't seem to match the question any longer. – Alan Munn Jan 23 '16 at 22:54
2

Don't reinvent the wheel: the makecell package is done for that, and a common formatting of such cells, with its \thead and \makecell (and some others).

Another solution, to have the multiline cells in the second column centred (while the lines in each cell are left-aligned) is to use the \eqparbox command from the homonymous package. It uses a system of tags that ensures all \parboxes with the same tag have the width of the widest content. Note it may require two compilations to measure the widest box.

In addition, I suggest to use the rule commands from booktabs, which will set some vertical padding above and below horizontal rules:

\documentclass{article}

\usepackage[english]{babel}

\usepackage{makecell, booktabs}
\renewcommand\theadfont{\bfseries}
\renewcommand\theadalign{lc}
\renewcommand\cellalign{tl}

\usepackage{eqparbox}

\begin{document}

\begin{tabular}{ccp{8cm}}
  \toprule
  \thead{Command} & \thead{Declaration} & \thead{Output}\\
  \midrule
  \verb|\textrm| & \makecell{First one\\foobar\\again} & small test example\\
  \midrule
  \verb|\textrm| & \makecell[tl]{First one\\foobar\\again} & small test example\\
  \midrule
  \verb|\textrm| & \makecell[tl]{First one\\foobar\\again} & small test example\\
  \bottomrule
\end{tabular}
\vskip 1cm

\begin{tabular}{ccp{8cm}}
  \toprule
  \thead{Command} & \thead{Declaration} & \thead{Output}\\
  \midrule
  \verb|\textrm| & \eqparbox{Col}{First one\\foobar\\again} & small test example\\
  \midrule
  \verb|\textrm| & \eqparbox{Col}{First}% 
  & small test example\\
  \midrule
  \verb|\textrm| & \eqparbox{Col}{First one\\foobar\\again} & small test example\\
  \bottomrule
\end{tabular}

\end{document} 

enter image description here

  • Thank you very much. Please see my edit in the original post for one problem. – machinery Jan 24 '16 at 10:26
  • @machinery: for the problem you point in your edit, I've changed my second solution: it is now based on eqparbox instead of stackengine. – Bernard Jan 24 '16 at 13:20
  • Thank you again, please see my Edit 2 in the original post for a problem. If this could be solved, everything should be perfect. – machinery Jan 24 '16 at 18:08
  • Like for \parbox, you can use the optional argument ` t, c` or b for horizontal alignment w.r.t. the surrounding text. Default is c. Just use t. – Bernard Jan 24 '16 at 18:15

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.