更加可定制的版本

更加可定制的版本

我有一份关键字列表以及这些关键字的描述。我希望有表格来记录这些关键字在不同上下文中的使用情况,其中只包含与案例相关的关键字。其中一些关键字在不同的上下文中重复出现,因此理想的做法是只写一次描述,然后在每个表格中“引用”该描述,以便于维护。

例子:

\section{Runtype 1 description}
\begin{table}
\begin{center}
\begin{tabular}{ l l }
runtype & Keyword to determine runtype. Can be 1 or 2. \\
        & I don't want to hardcode this description twice...\\
param_1 & Keyword to determine the parameter of runtype = 1 \\
\end{tabular}
\end{center}
\end{table}

\section{Runtype 2 description}
\begin{table}
\begin{center}
\begin{tabular}{ l l }
runtype & Keyword to determine runtype. Can be 1 or 2. \\
        & I don't want to hardcode this description twice...\\
param_2 & Keyword to determine the parameter of runtype = 2 \\
\end{tabular}
\end{center}
\end{table}

答案1

下面,我定义以下命令:

  • \setkeyword{<keyword>}{<description>}可用于声明关键字。
  • \getkeyword{<keyword>}检索您提供的描述。
  • \keywordtable{<key1>,<key2>,…}显示一个表,其中包含与以逗号分隔的关键字列表相对应的描述。

(请注意,允许使用包含下划线的关键字。)

\documentclass{article}

\usepackage[T1]{fontenc} %% <- necessary for _ to be displayed correctly
\usepackage{etoolbox}    %% <- for \csuse, \csdef, \ifcsdef and \forcsvlist

%% Define/retrieve a new keyword:
\newcommand\setkeyword[2]{\csdef{keyw@#1}{#2}}
\newcommand*\getkeyword[1]{%
  \ifcsdef{keyw@#1}{%                %% <- if the key is defined...
    \csuse{keyw@#1}%                 %% <- return the description
  }{%                                %% <- otherwise...
    ??%                              %% <- question marks
    %\errmessage{Undefined key: #1}% %% <- or an ERROR, if you prefer
  }%
}

%% Display a table describing a list of keywords:
\newcommand*\keywordtable[1]{%
  \begin{tabular}{lp{8cm}}
    \forcsvlist{\tableentry}{#1} %% <- apply \tableentry to each value in #1
  \end{tabular}%
}
\newcommand*\tableentry[1]{%
    \formattableentry{\detokenize{#1}}{\getkeyword{#1}}%
}
\newcommand\formattableentry[2]{ #1 & #2 \\ }

% %% Declaration of keywords:
\setkeyword{runtype}{Keyword to determine runtype. Can be 1 or 2. \par
                     I don't want to hardcode this description twice\ldots}
\setkeyword{param_1}{Keyword to determine the parameter of runtype = 1}
\setkeyword{param_2}{Keyword to determine the parameter of runtype = 2}

\begin{document}

\section{Runtype 1 description}
\keywordtable{runtype,param_1}

\section{Runtype 2 description}
\keywordtable{runtype,param_2}

\end{document}

输出


一些评论

  • \setkeyword{<keyword>}{<description>}有效地为你定义\keyw@<keyword>,以便它扩展到<definition>。你不能直接使用此宏因为它的名字包含一个@
  • \getkeyword{<keyword>}只是打电话\keyw@<keyword>
  • 我正在使用 e-TeX 原语\detokenize打印关键字。此命令会删除其参数中所有标记的特殊含义(通过更改其 catcode),因此您可以\detokenize{param_1}安全地使用。
  • 没有\usepackage[T1]{fontenc},下划线显示为“̇”,而不是“_”。您可能还是会想使用此包,原因如下这里
  • 我在用着tabularx创建一个与当前行宽相同的表格。\linewidth如果您想要不同的宽度,可以用其他值替换(或者直接使用tabular,在这种情况下您应该用X其他值替换列类型)。


更加可定制的版本

根据要求,这是我能想到的最可定制的版本。这允许您使用 创建关键字\setkeyword{<keyword>}{<key1>=<value1>,<key2>=<value2>,…}并使用 检索它们\getkeyword{<keyword>}{<key>}。您可以使用 创建一个表,其中行对应于一组关键字,列对应于特定键

\keywordtable[<key1>,<key2>,…]{<keyword1>,<keyword2>,…}

我正在使用pkgkeys,这在第 82 节中有记录pgf 手册

\documentclass{article}

\usepackage[T1]{fontenc} %% <- necessary for _ to be displayed correctly
\usepackage{pgfkeys}     %% <- for everything starting with \pgf
\usepackage{etoolbox}    %% <- for \forcsvlist

\newcommand*{\declarekeyword}[1]{%
  \pgfkeys{
    /keyw/#1/.is family,
    /keyw/#1/.unknown/.style = {\pgfkeyscurrentpath/\pgfkeyscurrentname/.initial={##1}},
  }%
}
\newcommand\setkeyword[2]{%
  \declarekeyword{#1}%
  \pgfkeys{/keyw/#1/.cd,name=\detokenize{#1},#2}%
}
\newcommand*\getkeyword[2]{\pgfkeysvalueof{/keyw/#1/#2}}

%% Display a table describing a list of keywords:
\newcommand*\keywordtable[2][name,description]{%
  \begin{center}
    \begin{tabular}{\forcsvlist{\getkeyword{@alignment}}{#1}}
      \forcsvlist{\tableentry[\bfseries]{#1}}{@headings}
      \forcsvlist{\tableentry{#1}}{#2}
    \end{tabular}
  \end{center}%
}
\newcommand*\tableentry[3][]{%
    \let\keywcolsep\empty
    \forcsvlist{\keywcolsep\def\keywcolsep{&}\formattableentry[#1]{#3}}{#2}\\
}
\newcommand*\formattableentry[3][]{#1{\getkeyword{#2}{#3}}}

%% "Fake" keywords (control column titles and alignment)
\setkeyword{@headings}{name=Parameter,description=Description,value=Value}
\setkeyword{@alignment}{name=l,description=l,value=r}

%% Declaration of keywords:
\setkeyword{runtype}{description=A parameter,value=1}
\setkeyword{param_1}{description=Another parameter,value=0}
\setkeyword{param_2}{name=\detokenize{PARAM_2},description=A third parameter,value=42}

\begin{document}

\section{Runtype 1 description}
\keywordtable{runtype,param_1}

\section{Runtype 2 description}
\keywordtable[name,value,description]{runtype,param_2}

\end{document}

输出

答案2

\documentclass[12pt]{article}
\newcommand\zzz{%
  runtype & Keyword to determine runtype. Can be 1 or 2. \\
          & I don't want to hardcode this description twice...}
\begin{document}
\section{Runtype 1 description}
\begin{table}[ht]
\begin{center}
\begin{tabular}{ l l }
\zzz\\
param\_1 & Keyword to determine the parameter of runtype = 1 \\
\end{tabular}
\end{center}
\end{table}

\section{Runtype 2 description}
\begin{table}[ht]
\begin{center}
\begin{tabular}{ l l }
\zzz\\
param\_2 & Keyword to determine the parameter of runtype = 2 \\
\end{tabular}
\end{center}
\end{table}
\end{document}

在此处输入图片描述

答案3

这取决于您首先想在哪里键入要重复的描述。

也许最好的地方是在文档序言中,或者在您可以访问的外部文件中\input

\documentclass{article}
\usepackage{xparse}

% a few line of code for setting up the system
\ExplSyntaxOn

\NewDocumentCommand{\newdesc}{mm}
 {% #1 is a key, #2 is the description
  \prop_gput:Nnn \g_nordico_descriptions_plist { #1 } { #2 }
 }

\NewDocumentCommand{\getdesc}{m}
 {% #1 is a key
  \prop_if_in:NnTF \g_nordico_descriptions_plist { #1 }
   { \prop_item:Nn \g_nordico_descriptions_plist { #1 } }
   { ???~non~existent~description~??? }
 }

\prop_new:N \g_nordico_descriptions_plist

\ExplSyntaxOff

% the descriptions (they can go in an external file
% say desc.tex and here you'd do \input{desc}

\newdesc{A}{I don't want to hardcode this description twice...}

\begin{document}

\section{Runtype 1 description}
\begin{center}
\begin{tabular}{ l l }
runtype  & Keyword to determine runtype. Can be 1 or 2. \\
         & \getdesc{A} \\
param\_1 & Keyword to determine the parameter of runtype = 1 \\
\end{tabular}
\end{center}

\section{Runtype 2 description}
\begin{center}
\begin{tabular}{ l l }
runtype  & Keyword to determine runtype. Can be 1 or 2. \\
         & \getdesc{A} \\
param\_2 & Keyword to determine the parameter of runtype = 2 \\
\end{tabular}
\end{center}

\end{document}

请注意,完全没有必要将其放置tabular在浮动table环境中(这可能会使其tabular转到另一个页面)。

在此处输入图片描述

相关内容