我正在尝试为表格行创建一个宏。这将允许我以后更改列的顺序,而不必更改每一行的列顺序。
我曾尝试过:
\newcommand\taxrow[9]{%
\def\paper{#1}%
\def\method{#2}%
\def\optobj{#3}%
\def\workload{#4}%
\def\sla{#5}%
\def\perfpred{#6}%
\def\numofreps{#7}%
\def\faulttol{#8}%
\def\failact{#9}%
\taxrowcond
}
\newcommand\taxrowcond[1]{
\paper & \method & \optobj & \workload & \sla & \perfpred & \numofreps & \faulttol & \failact & #1
}
\begin{table}[h]
\begin{tabular}{|l|l|l|l|l|l|l|l|l|l|}
\hline
\taxrow{Paper}{Method}{Optimization Objective}{Workload}{SLA}{Performance Objective}
{Num. of Reps}{Fault Tolerant}{Failover Action}{Overload Protection}
& & & & & & & & & \\ \hline
\end{tabular}
\end{table}
如果我删除“&”符号&
,它就会起作用:
\newcommand\taxrow[9]{%
\def\paper{#1}%
\def\method{#2}%
\def\optobj{#3}%
\def\workload{#4}%
\def\sla{#5}%
\def\perfpred{#6}%
\def\numofreps{#7}%
\def\faulttol{#8}%
\def\failact{#9}%
\taxrowcond
}
\newcommand\taxrowcond[1]{
\paper
\method
\optobj
\workload
\sla
\perfpred
\numofreps
\faulttol
\failact
#1
}
\begin{table}[h]
\begin{tabular}{|l|l|l|l|l|l|l|l|l|l|}
\hline
\taxrow{Paper}{Method}{Optimization Objective}{Workload}{SLA}{Performance Objective}
{Num. of Reps}{Fault Tolerant}{Failover Action}{Overload Protection}
& & & & & & & & & \\ \hline
\end{tabular}
\end{table}
答案1
OP 的代码失败有两个原因
- 末尾缺少
\tabularnewline
(或) ,导致字符数超过允许值(由于表格有 10 列,因此最多为 9 个),因此额外的字符将被“添加”到当前行错误\\
\tablerowcond
&
& & & & ...&
\def
表格单元格内的元素被分组,因此在该表格单元格之外不可见,在下一个单元格中未定义。如果确实需要,请使用全局定义\gdef
。(笔记我根本不推荐这种编码方法...)如果由于某种原因(并非不可能!)\method
在外部定义,则将使用外部版本而不是设置为的版本#2
!
\documentclass{article}%
\newcommand\taxrow[9]{%
\gdef\paper{#1}%
\gdef\method{#2}%
\gdef\optobj{#3}%
\gdef\workload{#4}%
\gdef\sla{#5}%
\gdef\perfpred{#6}%
\gdef\numofreps{#7}%
\gdef\faulttol{#8}%
\gdef\failact{#9}%
\taxrowcond%
}
\newcommand\taxrowcond[1]{%
\paper & \method & \optobj & \workload & \sla & \perfpred & \numofreps & \faulttol & \failact & #1 \tabularnewline
}
\begin{document}
\begin{table}[h]
\begin{tabular}{|l|l|l|l|l|l|l|l|l|l|}
\hline
\taxrow{Paper}{Method}{Optimization Objective}{Workload}{SLA}{Performance Objective}
{Num. of Reps}{Fault Tolerant}{Failover Action}{Overload Protection}
& & & & & & & & & \\ \hline
\end{tabular}
\end{table}
\end{document}%
答案2
更通用的抽象,允许多种表类型。
\documentclass{article}
\usepackage[margin=1cm]{geometry}
\usepackage{booktabs}
\usepackage{xparse}
\newcommand{\splitcell}[1]{\begin{tabular}{@{}c@{}}\strut#1\strut\end{tabular}}
\ExplSyntaxOn
\NewDocumentCommand{\definecolumns}{mm}
{
\joseph_define_columns:nn { #1 } { #2 }
}
\NewDocumentCommand{\sortcolumns}{mm}
{
\joseph_sort_columns:nn { #1 } { #2 }
}
\NewDocumentCommand{\printrow}{mm}
{
\joseph_print_row:nn { #1 } { #2 }
}
\int_new:N \l__joseph_loop_int
\cs_new_protected:Npn \joseph_define_columns:nn #1 #2
{
\seq_new:c { g_joseph_columns_#1_seq }
\seq_new:c { g_joseph_sort_#1_seq }
\prop_new:c { l_joseph_row_#1_prop }
\seq_gset_from_clist:cn { g_joseph_columns_#1_seq } { #2 }
\seq_set_eq:cc { g_joseph_sort_#1_seq } { g_joseph_columns_#1_seq }
}
\cs_new_protected:Npn \joseph_sort_columns:nn #1 #2
{
\seq_gset_from_clist:cn { g_joseph_sort_#1_seq } { #2 }
}
\cs_new_protected:Npn \joseph_print_row:nn #1 #2
{
\int_zero:N \l__joseph_loop_int
\clist_map_inline:nn { #2 }
{
\int_incr:N \l__joseph_loop_int
\prop_put:cxn { l_joseph_row_#1_prop }
{
\seq_item:cn { g_joseph_columns_#1_seq } { \l__joseph_loop_int }
}
{
##1
}
}
\seq_clear:N \l__joseph_row_seq
\seq_map_inline:cn { g_joseph_sort_#1_seq }
{
\seq_put_right:Nx \l__joseph_row_seq
{
\prop_item:cn { l_joseph_row_#1_prop } { ##1 }
}
}
\seq_use:Nn \l__joseph_row_seq { & } \\
}
\cs_generate_variant:Nn \prop_put:Nnn { cx }
\ExplSyntaxOff
\definecolumns{tax}{paper,method,optobj,workload,sla,perfpred,numofreps,faulttol,failact}
\begin{document}
\begin{tabular}{*{9}{c}}
\toprule
\printrow{tax}{
Paper,Method,\splitcell{Optimization\\Objective},
Workload,SLA,\splitcell{Performance\\Objective},
\splitcell{Num.\ of\\Reps},\splitcell{Fault\\Tolerant},\splitcell{Failover\\Action}
}
\midrule
\printrow{tax}{01,02,03,04,05,06,07,08,09}
\printrow{tax}{11,12,13,14,15,16,17,18,19}
\printrow{tax}{21,22,23,24,25,26,27,28,29}
\bottomrule
\end{tabular}
\bigskip
\sortcolumns{tax}{paper,method,optobj,numofreps,faulttol,failact,workload,sla,perfpred}
\begin{tabular}{*{9}{c}}
\toprule
\printrow{tax}{
Paper,Method,\splitcell{Optimization\\Objective},
Workload,SLA,\splitcell{Performance\\Objective},
\splitcell{Num.\ of\\Reps},\splitcell{Fault\\Tolerant},\splitcell{Failover\\Action}
}
\midrule
\printrow{tax}{01,02,03,04,05,06,07,08,09}
\printrow{tax}{11,12,13,14,15,16,17,18,19}
\printrow{tax}{21,22,23,24,25,26,27,28,29}
\bottomrule
\end{tabular}
\end{document}
初始排序顺序是根据 中的顺序得出的\definecolumns
,但可以使用 随意更改\sortcolumns
,如示例所示。
答案3
您可以这样做(但这可能不是最好的解决方案):
\documentclass{article}
\makeatletter
\newcommand\taxrow[9]{%
\gdef\taxpaper{#1}%
\gdef\method{#2}%
\gdef\optobj{#3}%
\gdef\workload{#4}%
\gdef\sla{#5}%
\gdef\perfpred{#6}%
\gdef\numofreps{#7}%
\gdef\faulttol{#8}%
\gdef\failact{#9}%
\taxrowcond
}
\def\mytax#1{#1 &}
\newcommand\taxrowcond[1]{
\gdef\setthis{\taxpaper \method \optobj \workload \sla \perfpred \numofreps \faulttol \failact}%
\@for \xx:=\setthis \do{%
\mytax{\xx}}%
#1}
\makeatother
\begin{document}
\begin{table}[h]
\begin{tabular}{|l|l|l|l|l|l|l|l|l|l|}
\hline
\taxrow{Paper}{Method}{Optimization Objective}{Workload}{SLA}{Performance Objective}
{Num. of Reps}{Fault Tolerant}{Failover Action}{Overload Protection}\\
& & & & & & & & & \\ \hline
\end{tabular}
\end{table}
\end{document}