我定义了一个包含表格的样式表。内容如下:
\begin{table}[h]
\centering
\begin{tabular}{ |c|c|c|c| }
\hline
Header 1 & Header 2 & Header 3 & Header 4 \\ \hline % Header row
% Put table content here!
\end{tabular}
\end{table}
我想要做的是,在样式表中定义一些命令,以便我能够在序言中不断向此表添加内容。我正在考虑使用如下命令:
\addtotable[1]{R1C1,R1C2,R1C3,R1C3}
\addtotable[2]{R2C1,R2C2,R2C3,R2C3}
这将有效地导致表格
\begin{table}[h]
\centering
\begin{tabular}{ |c|c|c|c| }
\hline
Header 1 & Header 2 & Header 3 & Header 4 \\ \hline % Header row
R1C1 & R1C2 & R1C3 & R1C4 \\ \hline
R2C1 & R2C2 & R2C3 & R2C4 \\ \hline
\end{tabular}
\end{table}
答案1
将宏放置在插入点,然后逐步添加到该宏:
\documentclass{article}
\newcommand\foo{%
\begin{table}[htp]
\centering
\begin{tabular}{ |c|c|c|c| }
\hline
Header 1 & Header 2 & Header 3 & Header 4 \\ \hline % Header row
\foorows
\hline
\end{tabular}
\end{table}}
\newcommand\foorows{}
\makeatletter
\newcommand\addtotable[1]{%
\g@addto@macro\foorows{\@gobble}%
\@for\tmp:=#1\do{%
\expandafter\g@addto@macro\expandafter\foorows
\expandafter{\expandafter&\tmp}%
}%
\g@addto@macro\foorows{\\}%
}
\makeatother
\addtotable{R1C1,R1C2,R1C3,R1C3}
\addtotable{R2C1,R2C2,R2C3,R2C3}
\begin{document}
\foo
\end{document}