如何将章节标题链接回目录?我想使用仅有的 hyperref
并且没有任何与 toc 相关的包(答案是titlesec
可以找到答案这里)
我可以做hypertarget
to c 部分(回答由 cmhughes 提供,并在下面的 MWE 中显示)。因此,我的问题实际上是:把这条hyperlink
线放在哪里?
\documentclass{report}
\usepackage{hyperref}
\let\oldcontentsline\contentsline%
\renewcommand\contentsline[4]{%
\hypertarget{toc#4}{}%
\oldcontentsline{#1}{#2}{#3}{#4}}
\begin{document}
\tableofcontents
\chapter{A Chapter to act as a Link}
\section{A Section to act as a Link}
\end{document}
答案1
不确定这是否是你想要的
\documentclass{report}
\usepackage[colorlinks]{hyperref}
\let\oldcontentsline\contentsline%
\renewcommand\contentsline[4]{%
\oldcontentsline{#1}{\smash{\raisebox{1em}{\hypertarget{toc#4}{}}}#2}{#3}{#4}}
\newcommand\mychapter[1]{%
\chapter[#1]{\protect\hyperlink{tocchapter.\thechapter}{#1}}}
\newcommand\mysection[1]{%
\section[#1]{\protect\hyperlink{tocsection.\thesection}{#1}}}
\newcommand\mysubsection[1]{%
\subsection[#1]{\protect\hyperlink{tocsubsection.\thesubsection}{#1}}}
\begin{document}
\tableofcontents
\mychapter{A Chapter to act as a Link}
\mysection{A Section to act as a Link}
\mysubsection{A Subection to act as a Link}
\mysubsection{A Subection to act as a Link}
\mysection{A Section to act as a Link}
\mysubsection{A Subection to act as a Link}
\mysubsection{A Subection to act as a Link}
\mychapter{A Chapter to act as a Link}
\mysection{A Section to act as a Link}
\mysubsection{A Subection to act as a Link}
\mysubsection{A Subection to act as a Link}
\mysection{A Section to act as a Link}
\mysubsection{A Subection to act as a Link}
\mysubsection{A Subection to act as a Link}
\clearpage~
\end{document}
答案2
理想情况下,人们只需使用\apptocmd
命令(来自etoolbox
包)或\xapptocmd
(来自xpatch
包)来挂钩适当的超目标/超链接命令,但不幸的是,由于结构命令的定义(例如\part
,\chapter
等等) ,这会中断。
但是,对这些命令进行通用的“轻微”重新定义可以实现这一点。首先,使用通用结构化命令,在文档的开头,循环(七个)命令定义这些命令并添加链接。否则,这些命令将按照其原始定义运行(因此,\LaTeXStandardpart
必须通过循环和使用\csletcs
命令来定义等)。
\documentclass{book}
\usepackage{etoolbox}%
\usepackage[final,pdftex]{hyperref}%
\listgadd{\StructureCommandsList}{}%
\newcommand{\LetLaTeXStandardSectionCommand}[1]{%
\csletcs{LaTeXStandard#1}{#1}%
}%
\makeatletter%
\apptocmd{\tableofcontents}{\phantomsection\hypertarget{document::toc}{}}{}{}%
\newcommand{\redefinestructurecommands}[1]{%
\csgdef{unstarred#1@@noopt}##1{%
\csuse{unstarred#1@@opt}[##1]{##1}%
}%
\csgdef{unstarred#1@@opt}[##1]##2{%
\csuse{LaTeXStandard#1}[##1]{\hyperlink{document::toc}{##2}}%
}%
\csgdef{unstarred#1}{%
\@ifnextchar[{%
\@nameuse{unstarred#1@@opt}%
}{%
\@nameuse{unstarred#1@@noopt}%
}%
}%
\csgdef{starred#1@@noopt}##1{%
\csuse{LaTeXStandard#1}*{\hyperlink{document::toc}{##1}}%
}%
\csgdef{unstarred#1}{%
\@ifnextchar[{%
\csuse{unstarred#1@@opt}%
}{%
\csuse{unstarred#1@@noopt}%
}%
}%
\csgdef{#1}{%
\@ifstar{%
\csuse{starred#1@@noopt}%
}{%
\csuse{unstarred#1}%
}%
}%
}%
\makeatother
\newcommand{\RedefineStructuringCommands}{%
% If some of the structuring commands should not use linking back to toc, just remove them from this csv - list here (say for subparagraph))
\forcsvlist{\listgadd{\StructureCommandsList}}{part,chapter,section,subsection,subsubsection,paragraph,subparagraph}%
\forlistloop{\LetLaTeXStandardSectionCommand}{\StructureCommandsList}%
\forlistloop{\redefinestructurecommands}{\StructureCommandsList}%
}%
\AtBeginDocument{%
\RedefineStructuringCommands%
}%
\setcounter{secnumdepth}{5}% For demo only
\setcounter{tocdepth}{5}% For demo only
\begin{document}
\chapter*{Introduction}%
\tableofcontents%
\part{First part}%
\chapter{First chapter}%
\section{First Section}%
\section*{First starred section}%
\subsection{First subsection}%
\subsubsection{First subsubsection}%
\paragraph{First paragraph}
\subparagraph{First subparagraph}%
\end{document}
我省略了屏幕截图,因为它实际上没有显示任何内容。
笔记:上述重新定义命令\redefinestructurecommand
是尚未发布的包的一部分,它通常更长且更复杂;-)
答案3
我想这就是我所追求的。感谢 Symbol 1 提出的想法!
\documentclass{report}
\usepackage{hyperref}
\let\oldcontentsline\contentsline%
\renewcommand\contentsline[4]{%
\hypertarget{toc#4}{}%
\oldcontentsline{#1}{#2}{#3}{#4}}
\let\oldsection\section
\renewcommand\section[1]{%
\oldsection[#1]{\protect\hyperlink{tocsection.\thesection}{#1}}}
\begin{document}
\tableofcontents
\chapter{A Chapter to act as a Link}
\section{A Section to act as a Link}
\end{document}