2

I'm creating the XML from the TOC files, in this process I'm finding issue. on looking into the files there are many commands are getting expanded while writing. for example \hat has wrote in \mathaccentV {tilde}251{A}. when i have used \protect before the \hat its writing \hat in the toc. is there any method to produce the same content in the TOC without expansion

\documentclass{book} 
\begin{document} 
\chapter{This is a sample chapter $\hat{z}acffr$} 
\section{This is a sample section $\tilde{d}$} 
\end{document}

Aux file content:

\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}}
Saravanan.O
  • 595
  • 2
  • 5
  • 15
  • Hi Steven, Thanks for your response. if possible can you code same example here. – Saravanan.O Apr 23 '19 at 09:54
  • Is there any way to control this through macros as a global not in the case by case occurrences. – Saravanan.O Apr 23 '19 at 09:58
  • You are supposed to provide the example that you are having difficulty with and then we try to adjust it to work. Please provide a short (but complete) non-working example. – Steven B. Segletes Apr 23 '19 at 09:59
  • Does this question help? https://tex.stackexchange.com/questions/110883/writing-to-a-file – Steven B. Segletes Apr 23 '19 at 10:02
  • \documentclass{book}

    \begin{document}

    \chapter{This is a sample chapter $\hat{z}acffr$}

    \section{This is a sample section $\tilde{d}$}

    \end{document}

    – Saravanan.O Apr 23 '19 at 10:05
  • it was writing the aux files "@writefile{toc}{\contentsline {chapter}{\numberline {1}This is a sample chapter $\mathaccent "705E\relax {z}acffr$}{1}}" and I need @writefile{toc}{\contentsline {chapter}{\numberline {1}This is a sample chapter $\hat{z}acffr$}{1}} as like in the chapter – Saravanan.O Apr 23 '19 at 10:08
  • 1
    \MakeRobust{\tilde} in the preamble. – egreg Apr 23 '19 at 13:04
  • i'm getting Undefined control sequence error for \MakeRobust is there any package need to be use? – Saravanan.O Apr 25 '19 at 07:21

1 Answers1

1

Rather than try to change the actual aux and toc files, I just write an additional file, fauxtoc.toc, that mimics the toc file, but is unexpanded.

This version is implemented for \chapter and \section, since that was what was mentioned by OP. Additional sectioning can be added.

The method draws on egreg's answer to this question: Writing \\ to a File

\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}

This produces a file fauxtoc.toc, which matches the proper toc file, except that the sectioning names are unexpanded

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}