![LaTeX etoolbox 列表在环境中不起作用](https://linux22.com/image/406725/LaTeX%20etoolbox%20%E5%88%97%E8%A1%A8%E5%9C%A8%E7%8E%AF%E5%A2%83%E4%B8%AD%E4%B8%8D%E8%B5%B7%E4%BD%9C%E7%94%A8.png)
我正在使用 LaTeX,每次调用环境(这里是环境environment
)时,我都想将一个元素添加到列表中。
当我运行该文档时,结果
['command']
却不是['environment','command']
我想要的。
当我在定义中输入将元素添加到列表(\listadd{\languagelist}{environment}
)中的命令\newenvironment
并在文档中调用它时,它不起作用(environment
列表中缺少)。
请问您有什么解决办法吗?
感谢您的帮助,
谨致
托马斯
代码如下:
\documentclass{minimal}
\usepackage{etoolbox}
%COMMAND FOUND ON INTERNET AND WORKS
% \printlist[<sep>]{<list macro>}
\newcommand{\printlist}[2][,]{{% Print list
% http://tex.stackexchange.com/a/89187/5764
\def\listsep{\def\listsep{#1}}% Delayed execution of list separator
\renewcommand{\do}[1]{\listsep`##1'}%
[\dolistloop\languagelist]
}}
%Create the environment that is supposed to add environment to the list every time I call it
\newenvironment{environment}{
\listadd{\languagelist}{environment}}{}
%Create the command that add command to the list every time I call it
\newcommand{\command}{
\listadd{\languagelist}{command}}
\begin{document}
%Add environment to the list
\begin{environment}
\end{environment}
%Add command to the list
\command
%Expected result : ['environment','command']
%Result : ['command']
\printlist{\languagelist}
\end{document}
答案1
环境形成群体,因此你\listadd
将无法在群体结束时存活。
您可以改用\listgadd
,它执行“全局加法”。
不过,我建议使用更为强大的解决方案expl3
。
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
% \printlist[<sep>]{<list name>}
\NewDocumentCommand{\printlist}{ O{,}mo }
{
\IfNoValueTF{#3}
{% no trailing optional argument, use the default wrapper: #1 -> `#1'
\cs_set_eq:NN \__thomas_list_wrap:n \__thomas_list_wrap_default:n
}
{% locally define the template
\cs_set:Nn \__thomas_list_wrap:n { #3 }
}
\thomas_list_print:nn { #1 } { #2 }
}
% \addtolist{<list name>}{<item>}
\NewDocumentCommand{\addtolist}{mm}
{
\thomas_list_add:nn { #1 } { #2 }
}
% variables
\seq_new:N \l__thomas_list_tmp_seq
% internal functions
\cs_new:Nn \__thomas_list_wrap_default:n { `#1' }
\cs_new_protected:Nn \thomas_list_print:nn
{
\seq_clear:N \l__thomas_list_tmp_seq
\seq_map_inline:cn { g__thomas_list_#2_seq }
{
\seq_put_right:Nn \l__thomas_list_tmp_seq { \__thomas_list_wrap:n { ##1 } }
}
[\seq_use:Nn \l__thomas_list_tmp_seq { #1 }]
}
\cs_new_protected:Nn \thomas_list_add:nn
{
\seq_if_exist:cF { g__thomas_list_#1_seq }
{
\seq_new:c { g__thomas_list_#1_seq }
}
\seq_gput_right:cn { g__thomas_list_#1_seq } { #2 }
}
\ExplSyntaxOff
%Create the environment that is supposed to add environment to the list every time I call it
\newenvironment{environment}
{\addtolist{languagelist}{environment}}
{}
%Create the command that add command to the list every time I call it
\newcommand{\command}{%
\addtolist{languagelist}{command}%
}
\begin{document}
%Add environment to the list
\begin{environment}
\end{environment}
%Add command to the list
\command
%Expected result : ['environment','command']
%Result : ['command']
\printlist{languagelist}
\printlist[;]{languagelist}
\printlist[;]{languagelist}[``#1'']
\end{document}
尾随参数\printlist
是一个使用某些函数包装每个项目的模板。