0

I want to have the bookmarks in my TeX output, pdf file, such that they are inversed.

Like this:

   \documentclass{article}
   \usepackage[utf8]{inputenc}

   \begin{document}

   \tableofcontents

   \section{Hello}‎
   \section{Wow}
   \section{Bye}

   \tableofcontents

   \end{document}

enter image description here

CarLaTeX
  • 62,716
Nosrati
  • 237

2 Answers2

4

Well, update to OP's indicates what is wanted is reversed ordered in the bookmarks. But bookmarks require hyperref, and the posted mwe does not use hyperref and the posted image does not show actual TOC of document.

Thus I am afraid I got started on this on wrong premises. Anyway, here is, adapted from https://tex.stackexchange.com/a/171556/4686 how to achieve this for the LaTeX generated TOC of the document. It is with or without hyperref, but bookmarks are not modified.

\documentclass{article}

\usepackage{hyperref}

\begin{document}

\newtoks\mytoks
\makeatletter

\@ifundefined{hyperref}
{\mytoks{\def\contentsline #1#2#3{%
         \def\x{\contentsline {#1}{#2}{#3}}%
         \toks@\expandafter\expandafter\expandafter 
                {\expandafter\x\the\toks@}%
 }}}%
{\mytoks{\def\contentsline #1#2#3#4{%
         \def\x{\contentsline {#1}{#2}{#3}{#4}}%
         \toks@\expandafter\expandafter\expandafter 
                {\expandafter\x\the\toks@}%
 }}}%

\addtocontents{toc}{\begingroup\toks@{}%
                    \string\the\string\mytoks}% extra \string for space token finicking
\AtEndDocument 
  {\addtocontents {toc}{\string\expandafter\string\endgroup\string\the\string\toks@}}
\makeatother

\tableofcontents

\section{A}

\subsection{A.1}

\section{B}

\subsection{B.1}

\section{C}

\subsection{C.1}

\section{D}

\subsection{D.1}

Hello there.

\end{document}

enter image description here

Adding subsections pretty much ruins the look of the TOC, but with only sections it would be fine.

4

Here we go. This time we handle the bookmarks but leave the TOC untouched.

CAVEAT: the code is for a document with article class with sections but not parts. It is QUITE HACKY. NOT TESTED WITH PACKAGE bookmark ONLY WITH PACKAGE hyperref.

\documentclass{article}

\usepackage{hyperref}

\begin{document}

\makeatletter

\let\original@write\write

\def\write #1{\write@zzz#1\@outlinefile\@nil}

\def\write@zzz #1\@outlinefile#2\@nil
             {\if\relax\detokenize{#1}\relax
              \expandafter\@firstoftwo
              \else
              \expandafter\@secondoftwo
              \fi {\noexpand\hacked@write@tooutline}{\original@write#1}}

\newtoks\mytoksA
\newtoks\mytoksB

\def\hacked@write@tooutline #1%
   {\gdef\hacked@tmp{#1^^J}\hacked@write #1\hacked@write}

\def\hacked@write #1[#2]#3\hacked@write{%
    \ifnum #2=1
    \expandafter\@firstoftwo
    \else
    \expandafter\@secondoftwo
    \fi
    {\global\mytoksA\expandafter\expandafter\expandafter
            {\expandafter\the\expandafter\mytoksB\the\mytoksA}%  
     \global\mytoksB\expandafter{\hacked@tmp}}%
    {\global\mytoksB\expandafter\expandafter\expandafter
            {\expandafter\the\expandafter\mytoksB\hacked@tmp}}%
}%

\makeatletter

\tableofcontents

\section{A}

\subsection{A.1}

in first subsection of first section

\subsection{A.2}

in second subsection of first section

\section{B}

\subsection{B.1}

in first subsection of second section

\subsection{B.2}

in second subsection of second section

\section{C}

\subsection{C.1}

\subsection{C.2}

\section{D}

\subsection{D.1}

\subsection{D.2}

Hello there.

\makeatletter

\immediate\original@write\@outlinefile{\the\mytoksB\the\mytoksA}

\makeatother
\end{document}

enter image description here

In this view I am not sure I understand the Skim colouring of bookmarks but they are functional.

  • I should have insisted perhaps that the mwe has some important tidbit right before \end{document} that line must not be forgotten for the thing to work. –  Sep 18 '17 at 08:50
  • the thing is done in order to reverse the order of sections but within each section to keep subsections (and deeper stuff) in correct order. If the document contains parts, they will get out of sync with sections. This is all controlled by the \ifnum#2=1 line. –  Sep 18 '17 at 08:53