%! lualatex --shell-escape
\documentclass[a4paper,openany,oneside,]{extbook}
\usepackage{fontspec} % for lualatex is used
\usepackage{pgffor}
\usepackage{minted}
\usepackage[]{tocloft}
\begin{document}
% Some nomenclature: DASH -, SLASH / , UNDERSCORE, UNDERLINE _
\tableofcontents
\pagebreak
\foreach \cdossier in {circle.c, ctest1.c, check\string_elements.c}
{
\begin{figure}[htpb]
\addcontentsline{toc}{chapter}{\cdossier} % causes the error below (5 lines)
% ! Missing $ inserted.
% <inserted text>
% $
% l.3 \contentsline {chapter}{check_elements.c}{3}
% %
\inputminted[autogobble]{c}{\cdossier}
\end{figure}
}
\end{document}
我正在编写一个代码,使用包的命令tex
浏览所有c
文件。由于许多文件名包含下划线,即符号,我不得不将's 替换为current working directory
pgffor
\foreach
c
_
_
\string_
然后使用该tocloft
包,我尝试通过使用以下命令使每个文件名显示为内容行:
\addcontentsline{toc}{chapter}{\cfile}
但是当\cfile
包含时\string
会出现以下错误:
! Undefined control sequence.
<argument> ... {toc}{\protect \contentsline {chapter}{\cfile
}{\thepage }\protected@fil...
如何使包含下划线字符的文件名同时满足pgffor
和tocloft
?
答案1
您的\string
文件没有帮助,因为_
文件名中允许正常,但您的.toc
文件,无论有没有\string
,看起来都像
\contentsline {chapter}{circle.c}{2}{}%
\contentsline {chapter}{ctest1.c}{2}{}%
\contentsline {chapter}{check_elements.c}{2}{}%
读回时也会_
出现错误。
您可以使用 detokenize 例如
\documentclass[a4paper,openany,oneside,]{extbook}
\usepackage{fontspec} % for lualatex is used
\usepackage{pgffor}
\usepackage{minted}
\usepackage[]{tocloft}
\begin{document}
% Some nomenclature: DASH -, SLASH / , UNDERSCORE, UNDERLINE _
\tableofcontents
\pagebreak
\foreach \cdossier in {circle.c, ctest1.c, check_elements.c}
{
\begin{figure}[htpb]
\addcontentsline{toc}{chapter}{\string\detokenize{\cdossier}} % causes the error below (5 lines)
% ! Missing $ inserted.
% <inserted text>
% $
% l.3 \contentsline {chapter}{check_elements.c}{3}
% %
\inputminted[autogobble]{c}{\cdossier}
\end{figure}
}
\end{document}
然后(首先删除现有的 toc 文件)你将得到
\contentsline {chapter}{\detokenize {circle.c}}{2}{}%
\contentsline {chapter}{\detokenize {ctest1.c}}{2}{}%
\contentsline {chapter}{\detokenize {check_elements.c}}{2}{}%