# Tabular macro does not work

i try to write a short macro to align some (3) words one below the other in one cell of a table, like here: tabular inside a newcommand
For a reason I do not understand it does not work.
Here is my code:

\documentclass[11pt]{article}

\usepackage[table]{xcolor}
\usepackage{cite}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{censor}
\usepackage[a4paper, left=3cm, right=2cm, top=2.5cm, bottom=2.5cm]{geometry}
\usepackage{setspace}
\usepackage{graphicx}
\usepackage{pdfpages}

%my command to create the tabular
\newcommand{\cellincell}[3]{
\begin{tabular}{l}
#1 \\
#2 \\
#3 \\
\end{tabular}
}

\begin{document}

%only writes 'test1,test2,test3' plain in one line
\cellincell{test1,test2,test3}

%works as intended
%\begin{tabular}{l}
%test1 \\
%test2 \\
%test3 \\
%\end{tabular}

\begin{table}
\caption{this is how it should look like}
\begin{tabular}{|l|l|l|l|}
\hline
Trimround & \multicolumn{1}{c|}{I} & \multicolumn{1}{c|}{II} & \multicolumn{1}{c|}{III} \\ \hline
number & \begin{tabular}{l}
test1 \\
test2 \\
test3 \\
\end{tabular} & text &  \\ \hline

\end{tabular}
\end{table}

%throws: ! Extra alignment tab has been changed to \cr. (l. 56)
\begin{table}
\begin{tabular}{|l|l|l|l|}
\hline
Trimround & \multicolumn{1}{c|}{I} & \multicolumn{1}{c|}{II} & \multicolumn{1}{c|}{III} \\ \hline
%number & \cellincell{test1,test2,test3} & text &  \\ \hline

\end{tabular}
\end{table}

\end{document}


Thanks a lot

• you have defined the macro to take three arguments so \cellincell{test1}{test2}{test3} not \cellincell{test1,test2,test3} – David Carlisle Jul 27 '16 at 22:26

Here, I use a \Centerstack to construct \cellincell, and also manage the comma parsing.

\documentclass[11pt]{article}
\usepackage[table]{xcolor}
\usepackage{cite}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{censor}
\usepackage[a4paper, left=3cm, right=2cm, top=2.5cm, bottom=2.5cm]{geometry}
\usepackage{setspace}
\usepackage{graphicx}
\usepackage{pdfpages}
\usepackage{stackengine}
\setstackEOL{,}
\setstackgap{L}{\normalbaselineskip}
%my command to create the tabular
\def\cellincell#1{\Centerstack[l]{#1}}
\begin{document}
\begin{table}
\begin{tabular}{|l|l|l|l|}
\hline
Trimround & \multicolumn{1}{c|}{I} & \multicolumn{1}{c|}{II} & \multicolumn{1}{c|}{III} \\ \hline
number & \cellincell{test1,test2a,test3} & text &  \\ \hline
\end{tabular}
\end{table}
\end{document}


The macro takes three arguments, not one argument with a comma separated list.

\documentclass[11pt]{article}

\usepackage[table]{xcolor}
\usepackage{cite}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{censor}
\usepackage[a4paper, left=3cm, right=2cm, top=2.5cm, bottom=2.5cm]{geometry}
\usepackage{setspace}
\usepackage{graphicx}
\usepackage{pdfpages}

%my command to create the tabular
\newcommand{\cellincell}[3]{%%%
\begin{tabular}{l}
#1 \\
#2 \\
#3 \\
\end{tabular}%%%
}

\begin{document}

%only writes 'test1,test2,test3' plain in one line
\cellincell{test1}{test2}{test3}

%works as intended
%\begin{tabular}{l}
%test1 \\
%test2 \\
%test3 \\
%\end{tabular}

\begin{table}
\caption{this is how it should look like}
\begin{tabular}{|l|l|l|l|}
\hline
Trimround & \multicolumn{1}{c|}{I} & \multicolumn{1}{c|}{II} & \multicolumn{1}{c|}{III} \\ \hline
number & \begin{tabular}{l}
test1 \\
test2 \\
test3 \\
\end{tabular} & text &  \\ \hline

\end{tabular}
\end{table}

%throws: ! Extra alignment tab has been changed to \cr. (l. 56)
\begin{table}
\begin{tabular}{|l|l|l|l|}
\hline
Trimround & \multicolumn{1}{c|}{I} & \multicolumn{1}{c|}{II} & \multicolumn{1}{c|}{III} \\ \hline
number & \cellincell{test1}{test2}{test3} & text &  \\ \hline

\end{tabular}
\end{table}

\end{document}

• +1 for usage of %%% – egreg Jul 27 '16 at 23:02
• @egreg: What's special about %%%? – twip Sep 28 '16 at 17:55
• @twip Inner joke, don't worry! – egreg Sep 28 '16 at 17:56
• @twip It's a joke between me and David: he maintains that my best skill in TeX is finding missing % at end of lines; he uses three of them just to be sure he doesn't miss one. ;-) – egreg Sep 28 '16 at 18:05

\cellincell takes three arguments

\cellincell{<first>}{<second>}{<third>}


and you've only supplied one

\cellincell{<first>}


So, instead of using \cellincell{test1,test2,test3} use \cellincell{test1}{test2}{test3}. You can also use the method below to update your macro to use the comma-separated format:

\documentclass{article}

%my command to create the tabular
\makeatletter
\newcommand{\cellincell}[1]{\expandafter\@cellincell#1\@nil}
\def\@cellincell#1,#2,#3\@nil{%
\begin{tabular}{@{}l@{}}
#1 \\ #2 \\ #3
\end{tabular}%
}
\makeatother

\begin{document}

\cellincell{test1,test2,test3}

\begin{tabular}{|l|l|l|l|}
\hline
Trimround & \multicolumn{1}{c|}{I} & \multicolumn{1}{c|}{II} & \multicolumn{1}{c|}{III} \\
\hline
number & \cellincell{test1,test2,test3} & text & \\
\hline
\end{tabular}

\end{document}


Just like in the case of supplying three separate arguments, you are forced to use at least two ,s in the call to \cellincell, even though some may be empty. As in \cellincell{,,last}, or \cellincell{one,,three}, for example.