创建可重复使用的表,其中某一列的数据每次都不同

创建可重复使用的表,其中某一列的数据每次都不同

我正在尝试编写一份列出许多要求的设计文档。文档的每个部分都描述了需要满足这些要求的实体,并且我认为记录这些要求的最佳方式是在每个部分中显示一个表格,表格的第一列具有相同的列标题和相同的数据(\autoref指向要求),而每个部分将在第二列中填写该实体如何满足每个要求的自定义描述。

我考虑过编写一个宏来完成所有这些工作,但我需要在其中添加大约二十几个参数(表格的每一行一个)。此外,当我向模板添加新行时,我希望在该行的第二列\todo{}中为表格的每个后续实例自动填充一个默认值(特别是 )。我也同意将模板放在序言中,因为我认为这是必需的。

我知道我可以使用宏来做到这一点,但我希望避免解决参数限制问题(并且处理默认值会更加困难),所以我希望有人知道一种更优雅的方法来获得我想要的重复结构。

答案1

下面是一个与 Christian Hupfer 的答案类似的例子,但是使用的pgfkeysxkeyval

\documentclass{article}
\usepackage{pgfkeys, pgffor} 
\usepackage{xcolor}

% this command is used for undefined cells
\newcommand{\undefinedcell}{\textcolor{red}{undefined}}

% set up the keys that store the entries
\pgfkeys{table/.is family, table, 
    list of rows/.initial={foo, bar, baz},
    add row/.style={#1/.value required, #1/.initial={\undefinedcell}},
    /table, first column/.is family, first column,
    /table/add row/.list/.expanded=\pgfkeysvalueof{/table/list of rows},
    /table, second column/.is family, second column,
    /table/add row/.list/.expanded=\pgfkeysvalueof{/table/list of rows},
}

% code to store the content of the table in a key
    \pgfkeys{table,
    content/.initial={},
    add content/.style = {
        content/.append={#1 & \pgfkeysvalueof{/table/first column/#1} & \pgfkeysvalueof{/table/second column/#1} \\\hline},
    }, 
    fill content/.style = {add content/.list/.expanded = \pgfkeysvalueof{/table/list of rows}},
}

% define the entries in the first column
\pgfkeys{table, first column,
    foo = entry 1,
    bar = entry 2   
}

% the command takes a list of key=val pairs and adds them to the second column
\newcommand{\printtable}[1]{
    \begingroup
    \pgfkeys{table, second column, #1, /table, fill content}
        \begin{tabular}{|l | l | l |}\hline
            \pgfkeysvalueof{/table/content}
        \end{tabular}
    \endgroup
}

\parindent=0pt 
\begin{document}

\section{First}
\printtable{foo=1}  

\section{Second}
\printtable{bar=2}

\end{document}

生成的表

答案2

根据 Christian Hupfer 的评论,我得到了一个可行的解决方案。

% Set up the requirement table macro
\usepackage{xkeyval}
% Required for \define@key to work
\makeatletter
% Define the default value that is assigned to the key
\def\contentRowOne{\todo[inline]{Fill in cell}}
% Define the key for parsing
\define@key{brt}{keyRowOne}{%
    \def\contentRowOne{#1}%
}
% Here you would add more \def and \define@key commands for each key needed
% Revert the changes made by \makeatletter
\makeatother

% This is the macro that is used in the body of the document
\newcommand\busreqtab[1][]{
% Grouping makes sure that later calls get the default values rather than
% the values from the last table
\begingroup 
% This parses the optional key-value parameters and runs the defined macros
% for each
\setkeys{brt}{#1}
% Standard table with the macros used here
\begin{table}
    \centering
    \begin{tabular}{|c|p{5cm}|}
    \hline
    Requirement & Fulfilled by \\
    \hline
    \autoref{req:label} & \contentRowOne \\
    \hline
    \end{tabular}
\end{table}
% end of the group, replaces the changes to the key macros with default values
\endgroup
}

\begin{document}
% Generates table with todo in second column
\busreqtab
% Generates table with Test in second column
\busreqtab[contentRowOne=Test]
\end{document}

相关内容