As a result of my (yet to be published ;-) package with questions and solutions I came across some problems, that are connected to sectioning of a document, say, a book with chapters/sections/subsections etc.
As such, in the way I was finding a solution, there is a cloning of sectioning commands with titles etc. to an external file and reading it at the end of the compilation run, via \input
.
The, say, \chapter
command has to be redefined to this, and it will first produce the ordinary chapter and later on the cloned chapter, via \immediate\write\LaTeXStandardChapter
(see MWE for definitions please). As long as the delayed chapters have accumulated chapter numbers, there is no problem. \addcontentsline
adds them nicely to the toc and generates a bookmark, when hyperref
package is used.
Well, I do not want to have accumulated chapter numbers, I rather want to start them over again, such as
Question part: Chapter First ----> Section First of First
Cloned part: Cloned Chapter First --> Cloned Section First of First
etc. i.e. the chapter numbers have to be reset before reading the cloned content file.
Now the \LaTeXStandardChapter{...}
command will add a content line --> this is fine.
But the bookmarks are wrong, since they are created from the chapter number 1 etc, which is present twice --> the click on the bookmark will go to the wrong place!
Obviously, I have to disable the automatic generation of the bookmark made by \addcontentsline
, i.e. dropping \Hy@writebookmark
in a group and add the bookmark 'manually'.
All this works, but is there some nicer way to this?
Here is my code: (Part of a larger framework, only the relevant parts are shown or shortened to quick usage)
\documentclass[12pt,paper=a4]{book}
\RequirePackage{etoolbox}%
\RequirePackage{xcolor}%
\RequirePackage{morewrites}
\RequirePackage[bookmarksopen=true,bookmarksopenlevel=2]{hyperref}%
\RequirePackage{bookmark}%
\newwrite\CloneChapter%
\let\LaTeXStandardChapter\chapter%
\makeatletter
\newrobustcmd{\unstarredchapter@noopt}[1]{%
\unstarredchapter[#1]{#1}% Call the version with the optional first arg%
}% End of \unstarredchapter@noopt
\newrobustcmd{\unstarredchapter@opt}[2][]{%
\LaTeXStandardChapter[#1]{#2}% Normal Chapter, the source of the clone%
\immediate\write\CloneChapter{%
\string\LaTeXStandardChapter[]{\unexpanded{Explanation -- #2}}% Prevent an entry to the toc from the cloned chapter
\string\phantomsection%
\string\begingroup
\string\renewcommand{\unexpanded{\Hy@writebookmark}}[5]{}% Drop this "wrong" bookmark
\string\addcontentsline{toc}{chapter}{\unexpanded{\protect\numberline{\thechapter}}{\unexpanded{Explanation -- #1}}}%
\string\endgroup
\string\pdfbookmark[0,color=red]{\unexpanded{Explanation #1}}{cloned::chapter::bookmark::\thechapter}% Manually write the bookmark
}% End of \immediate\write
}% End of \unstarredchapter@opt
% 'Rewrite' the starred chapter, to cool down `\tableofcontents` ;-)
\newrobustcmd{\starredchapter}[1]{%
\LaTeXStandardChapter*{#1}%
}%
\newrobustcmd{\unstarredchapter}{%
\@ifnextchar[{\unstarredchapter@opt}{\unstarredchapter@noopt}
}%
\renewcommand{\chapter}{%
\@ifstar{\starredchapter}{\unstarredchapter}%
}%
\makeatother
\AtBeginDocument{%
\immediate\openout\CloneChapter=\jobname.clone%
\immediate\write\CloneChapter{%
\string\makeatletter%
\string\setcounter{chapter}{0}%
}%
}%
\AtEndDocument{%
\immediate\closeout\CloneChapter%
}%
\newrobustcmd{\CollectClonedChapter}{%
\part{Explanations}%
\setcounter{chapter}{0}%
\IfFileExists{\jobname.clone}{\input{\jobname.clone}}{%
}%
}%
\newcounter{somecounter}[chapter]
\newrobustcmd{\MyCommand}[2]{%
\refstepcounter{somecounter}%
\textbf{Example \thesomecounter:}
\noindent#1
\hrule%
\immediate\write\CloneChapter{%
\expandafter\string\unexpanded{\textbf{Explanation to Example \number\value{chapter}.\number\value{somecounter}}}
}%
\immediate\write\CloneChapter{%
\noindent
}%
\immediate\write\CloneChapter{%
\expandafter\string\unexpanded{#2}%
}%
}
\begin{document}
\tableofcontents
\part{Questions}%
\chapter{Original Chapter Number One}
\MyCommand{Foo}{Well, this was foo}%
\chapter[Short Original 2]{Original Chapter Number Two}
\MyCommand{And now for something completely different}{Who used that line very often?}
\CollectClonedChapter%
\end{document}
Edit
The repeated write of \renewcommand{\Hy@writebookmark}
is a little overstressing -- in my original framework, I wrapped \addcontentsline
in another command which does the redefinition. I omitted it for simplicity here.
Obviously there is no answer to this question :-(
\etoctoccontentsline
which, in its non-starred incarnation expands to\Etoc@addtocontents
on line1127
ofetoc
's code inetoc.pdf
. It shows how to add something to the.toc
file without intervention ofhyperref
which patches only\addcontentsline
and not\addtocontents
.etoc
's doc: The etoc command\etoctoccontentsline{..}{..}
is provided as a substitute to\addcontentsline{toc}{..}{..}
: this is to avoid the patching which is done by hyperref to\addcontentsline
in its process of creation of bookmarks.