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




\specialcellto bec. Make itland 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