表格宏不起作用

表格宏不起作用

我尝试编写一个简短的宏来将表格的一个单元格中的一些(3)个单词一个接一个地对齐,如下所示:新命令中的表格
由于某种原因,我不明白它不起作用。
这是我的代码:

\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[hidelinks]{hyperref}
\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

这里,我使用 来\Centerstack构造\cellincell,并且管理逗号解析。

\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[hidelinks]{hyperref}
\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}

在此处输入图片描述

答案2

该宏采用三个参数,而不是一个用逗号分隔的参数列表。

在此处输入图片描述

\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[hidelinks]{hyperref}
\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}

答案3

\cellincell需要参数

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

你只提供了

\cellincell{<first>}

因此,除了使用\cellincell{test1,test2,test3}use之外\cellincell{test1}{test2}{test3},您还可以使用以下方法更新宏以使用逗号分隔的格式:

在此处输入图片描述

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

就像提供三个独立参数的情况一样,您必须,在调用 时至少使用两个\cellincell,即使有些可能为空。例如\cellincell{,,last}、 或\cellincell{one,,three}

相关内容