# Preventing automatic Hy@writebookmark in \addcontentsline

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 :-(

• Look at macro \etoctoccontentsline which, in its non-starred incarnation expands to \Etoc@addtocontents on line 1127 of etoc's code in etoc.pdf. It shows how to add something to the .toc file without intervention of hyperref which patches only \addcontentsline and not \addtocontents.
– user4686
Jan 6 '15 at 20:55
• Here is the relevant extract from 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.
– user4686
Jan 6 '15 at 20:57
• @jfbu: Are you ready to provide an answer? ;-)
– user31729
Jan 7 '15 at 15:57

## 1 Answer

Here is an altogether different approach, but still adopting the general structure of your code.

One remark: I didn't know about package morewrites and decided not to use it. Indeed it modifies my expectations about how opening and closing file streams work in TeX. It allows it seems to use a read stream on a file which has not been closed yet.

As coloured bookmarks are not rendered in all pdf viewers, exceptionally I used Acrobat Reader for this answer.

\documentclass[12pt,paper=a4]{scrbook}

\RequirePackage{etoolbox}
\RequirePackage{xcolor}
%% \RequirePackage{morewrites}%% modifies our habits with respect to opened
%% and closed file streams. Too unsettling for a (in the process of becoming
%% old) guy.
\RequirePackage[bookmarksopen=true,bookmarksopenlevel=2]{hyperref}
\RequirePackage{bookmark}

\newwrite\CloneChapter

\let\LaTeXStandardChapter\chapter

\makeatletter

\renewcommand{\chapter}{\@ifstar{\starredchapter}{\unstarredchapter}}%

\newrobustcmd{\unstarredchapter}
{\@ifnextchar[{\unstarredchapter@opt}{\unstarredchapter@noopt}}

\newrobustcmd{\unstarredchapter@noopt}[1]{%
\unstarredchapter[#1]{#1}%  Call the version with the optional first arg%
}

\newrobustcmd{\unstarredchapter@opt}[2][]{%
\LaTeXStandardChapter[#1]{#2}% Normal Chapter, the source of the clone
\immediate\write\CloneChapter
{\string\LaTeXStandardChapter[Explanation -- #1]{Explanation -- #2}}%
}

\newrobustcmd{\starredchapter}[1]{\LaTeXStandardChapter*{#1}}%

\makeatother

\AtBeginDocument{\immediate\openout\CloneChapter=\jobname.clone }

% without morewrites, we can not wait EndDocument to close the file
% and then reopen it
% \AtEndDocument {\immediate\closeout\CloneChapter }%

\newrobustcmd{\CollectClonedChapter}{%
\immediate\closeout\CloneChapter
\part{Explanations}
\edef \ChapterCount {\arabic{chapter}}%
\renewcommand \thechapter {\the\numexpr\value{chapter}-\ChapterCount\relax}%
\bookmarksetup{color=red}%
\makeatletter
\IfFileExists{\jobname.clone}{\input{\jobname.clone}}
{CLONE FILE WAS SWALLOWED SOMEWHERE!}%
\makeatother
}%

\newcounter{somecounter}[chapter]

\newrobustcmd{\MyCommand}[2]{%
\refstepcounter{somecounter}%
\textbf{Example \thesomecounter:}

\noindent #1
\hrule
\immediate\write\CloneChapter {%
\string\textbf{Explanation to Example
\arabic{chapter}.\arabic{somecounter}}

\noindent #2
}% end of \write
}

\begin{document}
\bookmark[named=FirstPage]{Title page}

\tableofcontents

\part{Questions}

\chapter[Short 1]{Original Chapter Number One}

\MyCommand{Foo}{Well, this was foo}

\chapter[Short 2]{Original Chapter Number Two}

\MyCommand{And now for something completely different}
{Who used that line very often?}

\CollectClonedChapter

\end{document}


• a \makeatletter/\makeatother pair in \CollectClonedChapter is a remnant of an earlier code version.
– user4686
Jan 7 '15 at 21:57
• \unstarredchapter in \unstarredchapter@noopt from OP's code should be \unstarredchapter@opt for efficiency.
– user4686
Jan 7 '15 at 21:59
• I'll take a look later on, thanks in advance
– user31729
Jan 8 '15 at 4:50
• Bye the way: Do you want to have a look on tex.stackexchange.com/questions/222578/…?
– user31729
Jan 11 '15 at 17:43
• @ChristianHupfer I will (when I get some time, will be soon). Thanks for drawing my attention to it.
– user4686
Jan 11 '15 at 17:45