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)




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

\LaTeXStandardChapter[#1]{#2}% Normal Chapter, the source of the clone%
\string\LaTeXStandardChapter[]{\unexpanded{Explanation -- #2}}%  Prevent an entry to the toc from the cloned chapter
\string\renewcommand{\unexpanded{\Hy@writebookmark}}[5]{}%   Drop this "wrong" bookmark
\string\addcontentsline{toc}{chapter}{\unexpanded{\protect\numberline{\thechapter}}{\unexpanded{Explanation -- #1}}}%
\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` ;-)








\textbf{Example \thesomecounter:}

\expandafter\string\unexpanded{\textbf{Explanation to Example \number\value{chapter}.\number\value{somecounter}}}




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



enter image description here


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

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.


%% \RequirePackage{morewrites}%% modifies our habits with respect to opened
%% and closed file streams. Too unsettling for a (in the process of becoming
%% old) guy.






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

    \LaTeXStandardChapter[#1]{#2}% Normal Chapter, the source of the clone
      {\string\LaTeXStandardChapter[Explanation -- #1]{Explanation -- #2}}%



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

    \edef \ChapterCount {\arabic{chapter}}%
    \renewcommand \thechapter {\the\numexpr\value{chapter}-\ChapterCount\relax}%
                                 {CLONE FILE WAS SWALLOWED SOMEWHERE!}%


    \textbf{Example \thesomecounter:}

    \noindent #1
    \immediate\write\CloneChapter {%
        \string\textbf{Explanation to Example 

        \noindent #2
    }% end of \write

\bookmark[named=FirstPage]{Title page}



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



hupfer clones

| improve this answer | |
  • 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
  • 1
    @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

Your Answer

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