我想知道在 LaTeX 中是否可以自动在页眉或页脚中报告文档的当前章节名称,并且可以通过单击页眉或页脚中的此章节名称跳回到当前章节的开头。
本章有同样的问题。
答案1
book
对于定义自己的页面样式的类的可能解决方案:
\documentclass[]{book}
\usepackage{hyperref}
\usepackage{lipsum}
\makeatletter
\def\ps@myheadings{
\def\chaptermark##1{%
\markboth {\protect\hyperlink{\@currentHref}{\MakeUppercase{%
\ifnum \c@secnumdepth >\m@ne
\if@mainmatter
\@chapapp\ \thechapter. \ %
\fi
\fi
##1}}}{}}%
\def\schaptermark##1{%
\markboth {\protect\hyperlink{\@currentHref}{\MakeUppercase{%
\ifnum \c@secnumdepth >\m@ne
\fi
##1}}}{}}%
\def\sectionmark##1{%
\markright {\protect\hyperlink{\@currentHref}{\MakeUppercase{%
\ifnum \c@secnumdepth >\z@
\thesection. \ %
\fi
##1}}}}
\def\ssectionmark##1{%
\markright {\protect\hyperlink{\@currentHref}{\MakeUppercase{%
##1}}}}
}
\def\schapter#1{
\chapter*{#1}
\addcontentsline{toc}{chapter}{#1}
\phantomsection
\schaptermark{#1}
}
\def\ssection#1{
\section*{#1}
\addcontentsline{toc}{section}{#1}
\phantomsection
\ssectionmark{#1}
}
\makeatother
\begin{document}
\pagestyle{myheadings}
\tableofcontents
\schapter{Intro}
\lipsum
\ssection{One}
\lipsum
\lipsum
\section{Two}
\lipsum
\chapter{title}
\lipsum
\section{One}
\lipsum
\end{document}
解决方案KOMA
(基于 javi_gg1 的回答):
\documentclass{scrbook}
\usepackage{lipsum}
\usepackage[automark]{scrlayer-scrpage}
\usepackage{hyperref}
\makeatletter
\def\chaptermark#1{%
\markboth {\protect\hyperlink{\@currentHref}{\MakeUppercase{%
\ifnum \c@secnumdepth >\m@ne
\if@mainmatter
\@chapapp\ \thechapter. \ %
\fi
\fi
#1}}}{}}%
\def\sectionmark#1{%
\markright {\protect\hyperlink{\@currentHref}{\MakeUppercase{%
\ifnum \c@secnumdepth >\z@
\thesection. \ %
\fi
#1}}}}
\makeatother
\ohead{\leftmark}
\rohead{\rightmark}
\pagestyle{scrheadings}
\begin{document}
\tableofcontents
\addchap[Intro]{Intro}
\lipsum
\chapter{Ch1}
\section{Ch1, S1}
\lipsum[1-8]
\section{Ch1, S2}
\lipsum[1-8]
\section{Ch1, S3}
\lipsum[1-8]
\chapter{Ch2}
\section{Ch2, S1}
\lipsum[1-8]
\section{Ch2, S2}
\lipsum[1-8]
\section{Ch2, S3}
\lipsum[1-8]
\end{document}
答案2
scrbook
以下是使用类、hyperref
包和\automark
选项的解决方案。您可以通过将选项scrlayer-scrpage
添加到来隐藏链接。命令hidelinks
hyperref
\automark[section]{section}
将命令更新\headmark
为偶数页和奇数页的当前章节的标题。然后,命令
\ohead{\hyperlink{section.\arabic{chapter}.\arabic{section}}{\headmark}}
将外部页眉设置为超链接,其文本为\headmark
,目标是当前章节的开头。请注意,章节的第一页默认没有页眉。
您还可以通过取消注释下面相应的代码来生成章节而不是章节的链接。
\documentclass{scrbook}
\usepackage{nameref}
\usepackage{lipsum}
\usepackage{scrlayer-scrpage}
\usepackage{hyperref}
% Use this to link to sections on both sides
\automark[section]{section}
\ohead{\hyperlink{section.\arabic{chapter}.\arabic{section}}{\headmark}}
% Use this to link to chapters on both sides
%\automark[chapter]{chapter}
%\ohead{\hyperlink{chapter.\arabic{chapter}}{\headmark}}
% Use this to link to chapters on even pages and sections on odd pages
%\automark[section]{chapter}
%\lehead{\hyperlink{chapter.\arabic{chapter}}{\headmark}}
%\rohead{\hyperlink{section.\arabic{chapter}.\arabic{section}}{\headmark}}
\pagestyle{scrheadings}
\begin{document}
\chapter{Ch1}
\section{Ch1, S1}
\lipsum[1-8]
\section{Ch1, S2}
\lipsum[1-8]
\section{Ch1, S3}
\lipsum[1-8]
\chapter{Ch2}
\section{Ch2, S1}
\lipsum[1-8]
\section{Ch2, S2}
\lipsum[1-8]
\section{Ch2, S3}
\lipsum[1-8]
\end{document}