我已经为表创建了以下宏:
\newcommand{\createtable}[7]{
\begin{table}[htbp]
\begin{tabular}{|p{8cm}||p{8cm}|}
\hline
\begin{center}\textbf{\cellcolor{Orchid!25}{#1}}\end{center} &
\begin{center}\textbf{\cellcolor{Orchid!25}{#2}}\end{center} \\
\hline\hline
{#3}
& {#4}\\
\hline
{#5}
& {#6}\\
\hline
\end{tabular}%
\centering
\caption{#7}
\end{table}
}
用法:
\createtable
{Label1}
{Label2}
{Text1-1}
{Text2-1}
{Text1-2}
{Text2-2}
{Table Title}
输出:
问题:
参考: \newcommand 带有可选的多行参数和隐式 itemize 环境
在这里,我们可以创建一个动态项目符号列表,而无需事先了解项目的数量。
我需要一个类似的表格宏,这样如果我写:
\createtable
{Label1}
{Label2}
{Label3}
{Text1-Label1
\&
Text1-Label2
\&
Text1-Label3
\\
Text2-Label1
\&
Text2-Label2
\&
Text2-Label3
}
宏应该足够智能,能够确定:
[1] Table has 3 columns -- Label1, Label2 and Label3
[2] Distribute the text-input separated by \& among these columns, i.e.
Text1-Label1 under Label1, Text1-Label2 under Label2 and Text1-Label3 under Label3
[3] Identify the end of row marker as "//"
and add further entries (using the procedure described in
point [2]) to the next row.
谁能提供一些指点。
这是我最初尝试的:
\ExplSyntaxOn
\NewDocumentCommand{\testtable}{ m o }
{
% split the \\ separated list of items
\seq_set_split:Nnn \l_egreg_outline_items_seq { \\ } { #1 }
\begin{tabular}{|p{8cm}||p{8cm}|}
\hline
\begin{center}\textbf{\cellcolor{Orchid!25}{Column1}}\end{center} &
\begin{center}\textbf{\cellcolor{Orchid!25}{Column2}}\end{center} \\
\hline\hline
\seq_map_inline:Nn \l_egreg_outline_items_seq
{
##1 %\hline
}
\end{tabular}%
}
\ExplSyntaxOff
如果我们不能在运行时提供列数,那也没关系,但行数应该动态添加。
答案1
我建议用法\createtable
如下:
\createtable 3 {3cm}
{Label1} {Label2} {Label3}
{Text1-Label1 | Text1-Label2 | Text1-Label3 |
Text2-Label1 | Text2-Label2 | Text2-Label3 }
{Table title}
您可以看到,您必须指定列数(示例中为 3)和列宽(示例中为 3cm)。您在示例中使用了固定列宽,因此有充分的理由指定它。
实施方案应为:
\newcount\tmpnum
\long\def\addto#1#2{\expandafter\def\expandafter#1\expandafter{#1#2}}
\def\createtable#1#2{\def\tabcols{#1}\def\colwidth{#2}%
\def\tabdata{}\def\tabdataA{}\tmpnum=0 \createtableA}
\def\createtableA#1{\advance\tmpnum by1
\addto\tabdata{\begin{center}\textbf{\cellcolor{red!25}{#1}}\end{center}}%
\addto\tabdataA{|p{\colwidth}|}%
\ifnum\tabcols>\tmpnum
\addto\tabdata{&}\expandafter\createtableA
\else
\addto\tabdata{\\ \hline\hline}\expandafter\createtableB
\fi
}
\def\createtableB#1{\tmpnum=0 \createtableC#1||}
\def\createtableC#1|{\ifx|#1|\expandafter\createtableD\else
\advance\tmpnum by1
\ifnum\tabcols=\tmpnum \addto\tabdata{{#1}\\ \hline}\tmpnum=0
\else \addto\tabdata{{#1}&}%
\fi
\expandafter\createtableC \fi
}
\def\createtableD#1{%
\begin{table}[htbp]
\edef\tmp{\noexpand\begin{tabular}{\tabdataA}}\tmp
\hline
\tabdata
\end{tabular}
\centering\caption{#1}
\end{table}
}
答案2
如果以唯一的参数输入标签,就很容易找出列数。
\documentclass{article}
\usepackage{xparse}
\usepackage[table]{xcolor}
\usepackage{tabularx}
\usepackage{booktabs}
\ExplSyntaxOn
\NewDocumentCommand{\createtable}{mm}
{
\sandeep_ct_create_table:nn { #1 } { #2 }
}
\int_new:N \l_sandeep_ct_cols_int
\seq_new:N \l_sandeep_ct_head_input_seq
\seq_new:N \l_sandeep_ct_head_output_seq
\seq_new:N \l_sandeep_ct_table_body_seq
\cs_new_protected:Npn \sandeep_ct_create_table:nn #1 #2
{
\seq_set_split:Nnn \l_sandeep_ct_head_input_seq { & } { #1 }
\int_set:Nn \l_sandeep_ct_cols_int
{
\seq_count:N \l_sandeep_ct_head_input_seq
}
\seq_set_map:NNn \l_sandeep_ct_head_output_seq \l_sandeep_ct_head_input_seq
{
\exp_not:n { \multicolumn{1}{c}{\ctbigstrut\bfseries ##1} }
}
\seq_set_split:Nnn \l_sandeep_ct_table_body_seq { \\ } { #2 }
\begin{tabularx}{\columnwidth}{*{\l_sandeep_ct_cols_int}{X}}
\toprule
\addlinespace[0pt]
\rowcolor{orchid}
\seq_use:Nn \l_sandeep_ct_head_output_seq { & } \\
\addlinespace[0pt]
\midrule
\seq_use:Nn \l_sandeep_ct_table_body_seq { \\ \addlinespace }
\\
\bottomrule
\end{tabularx}
}
\ExplSyntaxOff
\newcommand{\ctbigstrut}{%
\vrule height .8cm
depth \dimexpr.8cm-\ht\strutbox\relax
width 0pt
}
\definecolor{orchid}{RGB}{242,213,230}
\begin{document}
\noindent
\createtable
{
Label 1 & Label 2
}
{
Text1-Label1
&
Text1-Label2
\\
Text2-Label1
&
Text2-Label2
}
\bigskip
\noindent
\createtable
{
Label 1 & Label 2 & Label 3
}
{
Text1-Label1
&
Text1-Label2
&
Text1-Label3
\\
Text2-Label1
&
Text2-Label2
&
Text2-Label3
}
\end{document}
第一个参数在 处被拆分,以&
计算列数;然后将这些项目作为 的参数\multicolumn
。
然后我们分割表体,\\
以便能够\addlinespace
在行之间添加内容。