在 LaTeX 中存储和调用可变数量的文本字符串
我目前正在用 LaTeX 编写 API 文档。我有一个包含错误代码和描述列表的表格环境,如下所示:
\begin{tabular}{|c|l|}
\hline
\textbf{Code} & \textbf{Description} \\ \hline
1 & (description of error 1) \\ \hline
2 & (description of error 2) \\ \hline
\end{tabular}
在文档后面的各个位置,我一起引用了错误的代码及其描述,如下所示:
Possible error conditions:
\begin{itemize}
\item 1---(description of error 1)
\end{itemize}
我想自动化此过程,因此我没有每次都重新输入描述。我尝试过使用计数器、标签和 \savebox 命令,但它相当麻烦:
\newcounter{error}
% Inside the tabular environment:
\newsavebox{\ErrorOne}
\savebox{\ErrorOne}{(description of error 1)}
\refstepcounter{error} \label{ErrorOne} \arabic{error} & \usebox{\ErrorOne} \\ \hline
后来,为了引用错误,
\ref{ErrorOne}---\usebox{\ErrorOne}
我特别反对必须使用 ErrorOne 作为标签,但使用 \ErrorOne (带有前导反斜杠)作为保存箱。我也不太想要像 ErrorOne 这样的名称,因为我可能需要在某些时候更改顺序。我想要的是能够定义一些命令:
\newerror{errorlabel}{Description} % defines the error (doesn't output anything)
\errorcode{errorlabel} % outputs the error code
\errordesc{errorlabel} % outputs the error description
然后能够
\newerror{ArgumentError}{Too many arguments}
\newerror{DatabaseError}{Could not connect to database}
% Inside the tabular environment:
\errorcode{ArgumentError} & \errordesc{ArgumentError} \\ \hline
\errorcode{DatabaseError} & \errordesc{DatabaseError} \\ \hline
% Later on:
\errorcode{DatabaseError}---\errordesc{DatabaseError}
像标签一样自动生成错误代码(1、2、3、...)。
有什么想法吗?
I'm currently writing an API document in LaTeX. I have a tabular environment with a list of error codes and descriptions, like so:
\begin{tabular}{|c|l|}
\hline
\textbf{Code} & \textbf{Description} \\ \hline
1 & (description of error 1) \\ \hline
2 & (description of error 2) \\ \hline
\end{tabular}
At various places later in the document, I reference an error's code and its description together, like so:
Possible error conditions:
\begin{itemize}
\item 1---(description of error 1)
\end{itemize}
I want to automate this process so I don't have to retype the descriptions every time. I have tried using a counter, labels, and the \savebox command, but it's pretty cumbersome:
\newcounter{error}
% Inside the tabular environment:
\newsavebox{\ErrorOne}
\savebox{\ErrorOne}{(description of error 1)}
\refstepcounter{error} \label{ErrorOne} \arabic{error} & \usebox{\ErrorOne} \\ \hline
and later, to reference the error,
\ref{ErrorOne}---\usebox{\ErrorOne}
I particularly object to having to use ErrorOne for the labels but \ErrorOne (with the leading backslash) for the saveboxes. I also don't really want names like ErrorOne, since I might need to change the order at some point. What I want is to be able to define a few commands:
\newerror{errorlabel}{Description} % defines the error (doesn't output anything)
\errorcode{errorlabel} % outputs the error code
\errordesc{errorlabel} % outputs the error description
and then be able to say something like
\newerror{ArgumentError}{Too many arguments}
\newerror{DatabaseError}{Could not connect to database}
% Inside the tabular environment:
\errorcode{ArgumentError} & \errordesc{ArgumentError} \\ \hline
\errorcode{DatabaseError} & \errordesc{DatabaseError} \\ \hline
% Later on:
\errorcode{DatabaseError}---\errordesc{DatabaseError}
with the error codes (1, 2, 3, ...) being automatically generated like labels would be.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下对我有用
The following works for me
在序言中,为每个错误创建新命令,然后只需调用命令:
\newcommand{\errorone}{this is the error and its description}
然后在正文中,只需调用新命令:
In your preamble, create new commands for each error, then just call the command:
\newcommand{\errorone}{this is the error and its description}
then in the body, just call the new command: