我有一些附录不想显示在目录中。相反,我希望在目录中显示一个名为“AllAppendices”的条目。这应该是附录中的一个(未编号的)章节。真正的附录应该是(编号的)章节,但同样不在目录中。
我已经尝试过这两个版本:
\documentclass{book}
\usepackage{hyperref}
\newcommand{\nocontentsline}[3]{}
\newcommand{\tocless}[2]{\bgroup\let\addcontentsline=\nocontentsline#1{#2}\egroup}
%\renewcommand{\tocless}[2]{\bgroup\let\addcontentsline=\nocontentsline#1{#2}\egroup\addtocounter{chapter}{-1}\refstepcounter{chapter}}
\begin{document}
\tableofcontents
I want to be able to reference FirstAppendix: \ref{FirstAppendix}, \nameref{FirstAppendix}
\chapter{LastRealChapter}
\label{LastRealChapter}
\chapter*{AllAppendices (can be added to TOC manually)}
\appendix
\label{AllAppendices}
\cleardoublepage
\tocless
\chapter{FirstAppendix (numbered, but not in TOC)}
\label{FirstAppendix}
\end{document}
我得到的是:
nameref
永远无法正常工作,它引用了“AllAppendices”ref
在第二个版本中有效,但在第一个版本中无效- 我在第二个版本中收到警告:“具有相同标识符(名称{appendix.A})的目标已被使用,重复项被忽略”
我的问题是:
- 如何正确地做这件事?
- 这有何含义
\addtocounter{chapter}{-1}\refstepcounter{chapter}
?
MWE 可以简化为:
\documentclass{book}
\usepackage{hyperref}
\newcommand{\nocontentsline}[3]{}
\newcommand{\tocless}[2]{\bgroup\let\addcontentsline=\nocontentsline#1{#2}\egroup}
\renewcommand{\tocless}[2]{\bgroup\let\addcontentsline=\nocontentsline#1{#2}\egroup\addtocounter{chapter}{-1}\refstepcounter{chapter}}
\begin{document}
\tableofcontents
I want to be able to reference Appendix like this: \ref{Appendix}, \nameref{Appendix}
\chapter*{SomethingElse}
\cleardoublepage
\appendix
\tocless\chapter{Appendix (numbered, but not in TOC)}
\label{Appendix}
\end{document}
注释掉\cleardoublepage
似乎有帮助,至少对于来说\ref
。
编辑:它确实在我的文档中起作用。那么底线是“没有章节\cleardoublepage
之前的\tocless
内容”吗?
答案1
你不需要复杂的补丁。只需在.toc
文件中合适的位置插入对计数器的更改即可tocdepth
。
\documentclass{book}
\usepackage{hyperref}
\begin{document}
\tableofcontents
% added in edit: (hyperref takes into account the current value
% of tocdepth, it was set to -3 during execution of \tableofcontents
% hence we must reset it; alternative: the option bookmarksdepth
% of hyperref -- see its documentation in the README of hyperref !)
\setcounter{tocdepth}{2} % (downto subsections)
%
\vspace{1cm}
I want to be able to reference FirstAppendix: \ref{FirstAppendix},
\nameref{FirstAppendix} as well as SecondAppendix: \ref{SecondAppendix},
\nameref{SecondAppendix}.
\chapter{LastRealChapter}
\label{LastRealChapter}
\appendix
\chapter*{AllAppendices}
\addcontentsline{toc}{chapter}{AllAppendices}
\addtocontents{toc}{\setcounter{tocdepth}{-3}}
\chapter{FirstAppendix (numbered, but not in TOC)}
\label{FirstAppendix}
\chapter{SecondAppendix (numbered, but not in TOC)}
\label{SecondAppendix}
\end{document}
答案2
在序言中添加以下几行可以使所有你想要的事情自动完成:
\makeatletter
\g@addto@macro\appendix{%
\def\@schapter#1{%
\begingroup
\let\@mkboth\@gobbletwo
\Hy@MakeCurrentHrefAuto{\Hy@chapapp*}%
\Hy@raisedlink{%
\hyper@anchorstart{\@currentHref}\hyper@anchorend
}%
\endgroup
\addcontentsline{toc}{chapter}{#1}\H@old@schapter{#1}%
}
\def\Hy@org@chapter[#1]#2{\ifnum \c@secnumdepth >\m@ne
\if@mainmatter
\refstepcounter{chapter}%
\typeout{\@chapapp\space\thechapter.}%
\fi
\fi
\chaptermark{#1}%
\if@twocolumn
\@topnewpage[\@makechapterhead{#2}]%
\else
\@makechapterhead{#2}%
\@afterheading
\fi}
}
\makeatother
梅威瑟:
\documentclass{book}
\usepackage{hyperref}
\makeatletter
\g@addto@macro\appendix{%
\def\@schapter#1{%
\begingroup
\let\@mkboth\@gobbletwo
\Hy@MakeCurrentHrefAuto{\Hy@chapapp*}%
\Hy@raisedlink{%
\hyper@anchorstart{\@currentHref}\hyper@anchorend
}%
\endgroup
\addcontentsline{toc}{chapter}{#1}\H@old@schapter{#1}%
}
\def\Hy@org@chapter[#1]#2{\ifnum \c@secnumdepth >\m@ne
\if@mainmatter
\refstepcounter{chapter}%
\typeout{\@chapapp\space\thechapter.}%
\fi
\fi
\chaptermark{#1}%
\if@twocolumn
\@topnewpage[\@makechapterhead{#2}]%
\else
\@makechapterhead{#2}%
\@afterheading
\fi}
}
\makeatother
\begin{document}
\tableofcontents
\vspace*{5cm}
I want to be able to reference FirstAppendix: \ref{FirstAppendix}, \nameref{FirstAppendix}
\chapter{LastRealChapter}
\label{LastRealChapter}
\appendix
\chapter*{AllAppendices (can be added to TOC manually)}
\label{AllAppendices}
\chapter{FirstAppendix (numbered, but not in TOC)}
\label{FirstAppendix}
\end{document}
输出: