我目前正在使用 memoir 包为实时角色扮演系统编写规则文档。作为其中的一部分,我需要包含技术信息 - “调用” - 即以特定方式行事的口头指令。我希望这些以某种方式出现在文本中,并附带上下文信息 - 但我也希望能够在附录章节中重现它们,有点像词汇表或关键字或库函数列表,以某种方式单一来源,这样我只需写出一次调用文本。
\documentclass{article}
\begin{document}
\begin{center}
\fbox{
\begin{minipage}{0.9\textwidth}
\begin{tabular}{p{0.1\textwidth} p{0.8\textwidth}}
\textsc{Stun} & Remain immobile for five seconds.
\end{tabular}
\end{minipage}}
\end{center}
\end{document}
这是我希望调用看起来像内联的 MWE,尽管我并不拘泥于这种特定的表示 - 它只是一个模型。我想要最简单、最巧妙的方式来做这种事情 - 显然破解它并不需要太多工作,但我希望能够自动化。我知道有一个用于重述定理的包,但这似乎与我想要做的事情有点远。
答案1
LuaLaTeX 的简单起点:
\documentclass{article}
% define calls and their descriptions here
\directlua{calls = {
["stun"] = "Remain immobile for five seconds.",
["smile"] = "Smile for five seconds."
}}
\newcommand{\calldesc}[1]{\directlua{tex.sprint(calls["#1"])}}
\begin{document}
\begin{center}
\fbox{
\begin{minipage}{0.9\textwidth}
\begin{tabular}{p{0.1\textwidth} p{0.8\textwidth}}
\textsc{stun} & \calldesc{stun}
\end{tabular}
\end{minipage}}
\end{center}
\section*{List of Calls}
\begin{tabular}{|l|l|}
\hline
Call & Description \\
\hline
\directlua{
output = {}
for name, desc in pairs(calls) do
table.insert(output, name .. " & " .. desc .. " \\\\")
end
tex.print(output)
}
\hline
\end{tabular}
\end{document}
答案2
如果您需要查找表,您可以通过包的数据库来维护它datatool
。
可以通过 进行dadatool
查找\DTLfetch
。
可以通过\DTLforeach
或 通过\DTLdisplaylongdb
或 通过打印列表/表格\DTLdisplaydb
。
\documentclass{memoir}
\usepackage{datatool,longtable, etoolbox}
\makeatletter
% datatool seems to have a bug, see <https://tex.stackexchange.com/q/642775/118714>,
% so let's fix it:
\expandafter\patchcmd\expandafter{\csname\string\DTLdisplaylongdb\endcsname}%
{\@dtl@resetdoamp\dtldisplaystarttab}%
{\dtldisplaystarttab\@dtl@resetdoamp}%
{\message{Patching succeeded.}}%
{\message{Patching failed.}}%
\makeatother
%===============================================================================
\DTLnewdb{calls}
\DTLaddcolumn{calls}{WhatKindOfCall}
\DTLsetheader{calls}{WhatKindOfCall}{Call}
\DTLaddcolumn{calls}{DescriptionOfCall}
\DTLsetheader{calls}{DescriptionOfCall}{Description}
%-------------------------------------------------------------------------------
\DTLnewrow{calls}
\DTLnewdbentry{calls}{WhatKindOfCall}{Stun}
\DTLnewdbentry{calls}{DescriptionOfCall}{Remain immobile for five seconds}
%-------------------------------------------------------------------------------
\DTLnewrow{calls}
\DTLnewdbentry{calls}{WhatKindOfCall}{Smile}
\DTLnewdbentry{calls}{DescriptionOfCall}{Smile for five seconds}
%-------------------------------------------------------------------------------
% This is one where you can use the optional argument of `\call` for
% passing in argument(s) to the macro which delivers the description.
\DTLnewrow{calls}
\DTLnewdbentry{calls}{WhatKindOfCall}{OptArg}
\newcommand\OptArg[1][\textit{\textlangle X\textrangle}]{Do this for #1 minutes}
\DTLnewdbentry{calls}{DescriptionOfCall}{\OptArg}
%===============================================================================
\newcommand\call[2][]{%
\begin{center}%
\begin{tabular}{|p{0.1\textwidth}p{0.8\textwidth}|}%
\hline
\textsc{\DTLfetch{calls}{WhatKindOfCall}{#2}{WhatKindOfCall}}&%
\DTLfetch{calls}{WhatKindOfCall}{#2}{DescriptionOfCall}#1\\%
\hline
\end{tabular}%
\end{center}%
}%
\begin{document}
\section*{List of kinds of calls}
\begingroup
\renewcommand{\dtldisplaystarttab}{\hline}%
\renewcommand\dtldisplayafterhead{\hline}%
\renewcommand\dtldisplaystartrow{\hline}%
\renewcommand{\dtldisplayendtab}{\tabularnewline\hline}%
\renewcommand{\dtlbeforecols}{|}%
\renewcommand{\dtlaftercols}{|}%
\renewcommand{\dtlbetweencols}{|}%
\renewcommand\dtlheaderformat[1]{\textbf{#1}\hfil\null}%
\DTLdisplaylongdb{calls}
\endgroup
\bigskip
\hrule
\bigskip
some text
\call{Stun}
some text
\call{Smile}
some text
\call{OptArg}
some text
\call[{[{3}]}]{OptArg}
\end{document}