如何让书签倒序排列

如何让书签倒序排列

我希望我的 TeX 输出 pdf 文件中的书签能够反转。

像这样:

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

   \begin{document}

   \tableofcontents

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

   \tableofcontents

   \end{document}

在此处输入图片描述

答案1

嗯,OP 的更新表明想要的内容在书签。但书签需要超链接,而发布的 mwe 并未使用hyperref,且发布的图像并未显示文档的实际目录。

因此,我担心我的出发点是错误的。无论如何,以下是改编自https://tex.stackexchange.com/a/171556/4686如何实现这一点用于 LaTeX 生成的文档目录。有无都可以hyperref,但是书签不会被修改。

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

在此处输入图片描述

添加小节会严重破坏目录的外观,但仅添加章节则没问题。

答案2

开始吧。这次我们处理书签,但不改变目录。

警告:代码适用于包含章节但不包含部件的文章类文档。它相当复杂。未使用包进行测试,bookmark仅使用包进行测试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}

在此处输入图片描述

从这个角度看,我不确定我是否理解书签的 Skim 着色,但它们是实用的。

相关内容