1

The aux file basically consists of tex commands which are read and expanded at the begining of the document. The command

\AtBeginDocument{\makeatletter\input{test2.aux}\makeatother}

will have pretty much the same effect, at least in terms of \newlabel commands. OTOH, \@writefile{toc} commands don't fare as well. Perhaps this has to do with the fact that the \@starttoc{toc} command is the one which actually writes the toc.

Anyway, I am trying to merge the table of contents without actually repeating the entire document each time. The following was used to create test2.aux:

\documentclass{book}
\begin{document}
\frontmatter
\tableofcontents
\mainmatter
\setcounter{part}{1}
\setcounter{page}{11}% this you will have to do manually
\part{Volume 2}
\chapter{v2 first}\label{test}
\chapter{v2 second}
\end{document}

which is read by

\documentclass{book}
\AtBeginDocument{\makeatletter\input{test2.aux}\makeatother}
\begin{document}
\frontmatter
\tableofcontents
\mainmatter
\part{Volume 1}
\chapter{v1 first}\pageref{test}%\newlabel{test} from test2.aux
\chapter{v1 second}
\end{document}

Note that \pageref{test} works.

  • 1
    isn't it easier to input the toc from the other document rather than the aux ? (xr.sty use \read to selectively pick labels (only) from another aux file exactly to avoid problems with other things that are there) – David Carlisle Aug 26 '15 at 14:38
  • @David Carlisle - Well, you certainly wouldn't want to use this with hyperref, and you may want to use custom \label and \ref macros to handle the volume numbers (see tex.stackexchange.com/questions/263015/…), but for printed documents, yes. – John Kormylo Aug 26 '15 at 18:50
1

I would use an \input statement literally written to the .aux file at the end of the document. The key is not to use \immediate\write but \write only.

\makeatletter
\AtEndDocument{%
    \write\@auxout{%
       \string\input{test2.aux}
      % Other \string\input{test3.aux} etc. here 
     }%
 }
\makeatother

will write \input{test2.aux} at the document end and not somewhere in between. Other files to be included can be added with \string\input{test3.aux} etc.

For better checks, \InputIfFileExists{}{}{} should be used, but I've omitted this feature for simplicity right now.

\documentclass{book}


\makeatletter

\AtEndDocument{%
    \write\@auxout{%
      \string\input{test2.aux}
      % Other \string\input{test3.aux} etc. here 
    }%
 }
\makeatother

%\AtBeginDocument{\makeatletter\input{test2.aux}\makeatother}
\begin{document}
\frontmatter
\tableofcontents
\mainmatter
\part{Volume 1}
\chapter{v1 first}\pageref{test}%\newlabel{test} from test2.aux
\chapter{v1 second}

\end{document}

enter image description here

This is the content of foo.aux (the merging document):

\relax 
\@writefile{toc}{\contentsline {part}{I\hspace  {1em}Volume 1}{1}}
\@writefile{toc}{\contentsline {chapter}{\numberline {1}v1 first}{3}}
\@writefile{lof}{\addvspace {10\p@ }}
\@writefile{lot}{\addvspace {10\p@ }}
\@writefile{toc}{\contentsline {chapter}{\numberline {2}v1 second}{5}}
\@writefile{lof}{\addvspace {10\p@ }}
\@writefile{lot}{\addvspace {10\p@ }}
\input{test2.aux} 
  • a \write at end of document isn't guaranteed to happen (it happens at the next page shipout but there may not be any further pages). – David Carlisle Aug 26 '15 at 15:30
  • @DavidCarlisle: Yes, I am aware of the possible breakdown with '\write. It might fail. An \immediate\write did not work – user31729 Aug 27 '15 at 8:15
0

A slight variation of Hupfer's solution which allows all the projects to share aux files. Project test:

\documentclass{book}
\newif\iffirstaux
\firstauxtrue
\makeatletter
\AtEndDocument{%
  \write\@auxout{\string\iffirstaux
     \string\firstauxfalse
     \string\InputIfFileExists{test2.aux}{}{}%
     \string\firstauxtrue
     \string\fi}%
 }
\makeatother
\begin{document}
\frontmatter
\tableofcontents
\mainmatter
\part{Volume 1}
\chapter{v1 first}\pageref{test}%\newlabel{test} from test2.aux
\chapter{v1 second}
\end{document}

Project test2:

\documentclass{book}
\newif\iffirstaux
\firstauxtrue
\makeatletter
\AtBeginDocument{%
  \write\@auxout{\string\iffirstaux
     \string\firstauxfalse
     \string\InputIfFileExists{test.aux}{}{}%
     \string\firstauxtrue
     \string\fi}%
 }
\makeatother
\begin{document}
\frontmatter
\tableofcontents
\mainmatter
\setcounter{part}{1}
\setcounter{page}{11}% this you will have to do manually
\part{Volume 2}
\chapter{v2 first}\label{test}
\chapter{v2 second}
\end{document}

Note that the aux files have to be \input in order, some at the beginning and some at the end.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.