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

11
  • 2
    Starting from the last entry on the top? And the title page at the very end? Or do you want to have the page number in front of the line and the title at the end of the line? What is clear to you, is a complete mystery to me.
    – Johannes_B
    Commented Sep 17, 2017 at 14:19
  • As the first sentence you wrote. 'the last entry on the top'
    – Nosrati
    Commented Sep 17, 2017 at 14:21
  • 2
    Can you show a minimal working example? A possible solution depends on the class and toc changing packages if used.
    – Johannes_B
    Commented Sep 17, 2017 at 14:23
  • You can plan a tex with arbitrary sections and show me how can I have revserved table in my output!
    – Nosrati
    Commented Sep 17, 2017 at 14:26
  • 2
    @MyGlasses Now I am really confused. Do you want the reverse order in the table of contents or in the bookmarks? Commented Sep 17, 2017 at 15:18

2 Answers 2

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.

1
  • thanks for your work. this post wasn't refresh to now. I see your answer after 45mins.
    – Nosrati
    Commented Sep 17, 2017 at 16:07
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.

2
  • 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.
    – user4686
    Commented Sep 18, 2017 at 8: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.
    – user4686
    Commented Sep 18, 2017 at 8:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .