如何使用超链接书签的长节标题

如何使用超链接书签的长节标题

该包hyperref使用章节的简短标题作为书签条目。是否可以改用完整格式?我有许多章节,因此无法手动将它们全部添加到书签中。

平均能量损失

\documentclass{article}

\usepackage[plainpages=false,pdfpagelabels=true,pdfencoding=auto,hidelinks]{hyperref}

\begin{document}

\section[Short title for toc]{A very long title which should also be visible as bookmark}


\end{document}

答案1

如果所有长章节、小节等标题都应出现在书签中,也许最简单的方法就是将 转移\toclevel@...到超出实际级别的某个位置,比如添加1000,添加正常内容行并使用 和明确\pdfbookmark[...]{...}{...}

\documentclass{article}

\usepackage{xpatch}

\makeatletter
\xpatchcmd{\@sect}{%
  \addcontentsline{toc}{#1}{%
    \ifnum #2>\c@secnumdepth \else
    \protect\numberline{\csname the#1\endcsname}%
    \fi
    #7}%
}{%
  \csletcs{orig@toclevel}{toclevel@#1}%
  \expandafter\renewcommand\csname toclevel@#1\endcsname{\the\numexpr#2+1000}
  \addcontentsline{toc}{#1}{%
    \ifnum #2>\c@secnumdepth \else
   \protect\numberline{\csname the#1\endcsname}%
    \fi
    #7}%
  \pdfbookmark[#2]{#8}{\csname theH#1\endcsname}%
  \expandafter\renewcommand\csname toclevel@#1\endcsname{\orig@toclevel}% Restore
}{}{}
\makeatother

\usepackage{blindtext}


\usepackage[plainpages=false,pdfpagelabels=true,pdfencoding=auto,hidelinks]{hyperref}
%\usepackage{hyperref}
\usepackage{bookmark}

\begin{document}
\tableofcontents
\clearpage
\section[Short title for toc]{A very long title which should also be visible as bookmark}
\blindtext[5]
\section[Another Short title for toc]{Another very very long title which should also be visible as bookmark}
\blindtext[6]
\subsection{A subsection title}
\section[Yet another Short title for toc]{Yet another very very long title which should also be visible as bookmark}
\blindtext[6]

\subsection{Another subsection title}

\subsubsection[Short subsubsection]{Another long long subsubsection title}

\subsection[Short subsection title one level higher]{Another short subsection title one level higher}

\end{document}

在此处输入图片描述

相关内容