我正在从目录文件创建 XML,在此过程中我发现了问题。查看文件时发现许多命令在写入时被扩展。例如,在\hat
中写入 。当我之前\mathaccentV {tilde}251{A}
使用时,它在目录中写入 \hat。有没有什么方法可以在不扩展的情况下在目录中生成相同的内容\protect
\hat
\documentclass{book}
\begin{document}
\chapter{This is a sample chapter $\hat{z}acffr$}
\section{This is a sample section $\tilde{d}$}
\end{document}
Aux 文件内容:
\relax
\@writefile{toc}{\contentsline {chapter}{\numberline {1}This is a sample chapter $\mathaccent "705E\relax {z}acffr$}{1}}
\@writefile{lof}{\addvspace {10\p@ }}
\@writefile{lot}{\addvspace {10\p@ }}
\@writefile{toc}{\contentsline {section}{\numberline {1.1}This is a sample section $\mathaccent "707E\relax {d}$}{1}}
答案1
我没有尝试更改实际的aux
和toc
文件,而是只编写了一个附加文件,fauxtoc.toc
它模仿 toc 文件,但未扩展。
\chapter
此版本针对和实施\section
,因为这是 OP 所提到的。可以添加其他部分。
该方法借鉴了egreg对这个问题的回答:将 \\ 写入文件
\documentclass{book}
\usepackage{xpatch}
\makeatletter
\let\protected@iwrite\protected@write
\xpatchcmd{\protected@iwrite}{\write}{\immediate\write}{}{}
\let\svchapter\chapter
\let\svsection\section
\renewcommand\chapter{\@ifstar{\svchapter*}{\mychapnostar}}
\newcommand\mychapnostar[2][]{%
\ifx\relax#1\relax\svchapter{#2}\fauxtocwrite{chapter}{\thechapter}{#2}%
\else\svchapter[#1]{#2}\fauxtocwrite{chapter}{\thechapter}{#1}\fi%
}
\renewcommand\section{\@ifstar{\svsection*}{\mysectnostar}}
\newcommand\mysectnostar[2][]{%
\ifx\relax#1\relax\svsection{#2}\fauxtocwrite{section}{\thesection}{#2}%
\else\svsection[#1]{#2}\fauxtocwrite{section}{\thesection}{#1}\fi%
}
\newcommand\fauxtocwrite[3]{%
\edef\tmpA{\thepage}%
\protected@iwrite\tempfile{}{\detokenize{\contentsline{#1}}%
{\detokenize{\numberline}{#2}\detokenize{#3}}{\tmpA}}%
}
\makeatother
\newwrite\tempfile
\AtBeginDocument{\immediate\openout\tempfile=fauxtoc.toc}
\AtEndDocument{\immediate\closeout\tempfile}
\begin{document}
\tableofcontents
\chapter{Sample chapter $\hat{z}acffr$}
\chapter[$\hat{z}acffr$]{Next}
\section{This is a sample section $\tilde{d}$}
\end{document}
这将生成一个文件fauxtoc.toc
,该文件与正确的 toc 文件匹配,只是部分名称未展开
normal.toc
:
\contentsline {chapter}{\numberline {1}Sample chapter $\mathaccent "705E\relax {z}acffr$}{3}
\contentsline {chapter}{\numberline {2}$\mathaccent "705E\relax {z}acffr$}{5}
\contentsline {section}{\numberline {2.1}This is a sample section $\mathaccent "707E\relax {d}$}{5}
fauxtoc.toc
:
\contentsline {chapter}{\numberline {1}Sample chapter $\hat {z}acffr$}{3}
\contentsline {chapter}{\numberline {2}$\hat {z}acffr$}{5}
\contentsline {section}{\numberline {2.1}This is a sample section $\tilde {d}$}{5}