[Tex/LaTex] How to break lines in tables without vertical spacing

line-breakingtables

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

Best Answer

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

Related Question