![从目录中的链接中排除章节编号:非编号章节错误](https://linux22.com/image/305845/%E4%BB%8E%E7%9B%AE%E5%BD%95%E4%B8%AD%E7%9A%84%E9%93%BE%E6%8E%A5%E4%B8%AD%E6%8E%92%E9%99%A4%E7%AB%A0%E8%8A%82%E7%BC%96%E5%8F%B7%EF%BC%9A%E9%9D%9E%E7%BC%96%E5%8F%B7%E7%AB%A0%E8%8A%82%E9%94%99%E8%AF%AF.png)
我使用 etoc从目录中的链接中排除章节编号。但是,当我插入未编号的附录部分时 — etoc 会添加一个句点:
我玩了一下代码,发现问题出\etocthenumber
在\etocsetstyle{section}
定义上(没有 etoc 就没有点)。有解决办法吗?
这里是 MWE:
\documentclass{article}
\usepackage{etoolbox}
\usepackage{xcolor}
% heading styles:
\usepackage{titlesec}
\titleformat{\section}
{\color{teal}\large\sffamily}
{\thetitle.\hspace{0.5cm}}
{0cm}
{}
\titleformat{\subsection}
{\color{violet}\bfseries}
{\hspace{0.75cm}\thetitle.\hspace{0.3cm}}
{0cm}
{}
\titleformat{\subsubsection}
{\color{olive}\itshape}
{\hspace{1.3cm}\thetitle.\hspace{0.3cm}}
{0cm}
{}
% ToC styles:
\usepackage[dotinlabels]{titletoc}
\contentsmargin{2.55em} % space for a page number
\titlecontents{section}
[3.8em] % 1.5em + 2.3em
{\vspace{0.3cm}\sffamily\bfseries\large}
{\contentslabel{2.3em}}
{\hspace*{-2.3em}}
{\normalsize\normalfont\rmfamily\titlerule*[1pc]{.}\contentspage}
[\vspace{0.1cm}]
\titlecontents{subsection}
[5.2em] % 3.8em + 1.3em
{\vspace{0.1cm}}
{\contentslabel{1.4em}}
{\hspace*{-1.4em}}
{\titlerule*[1pc]{.}\contentspage}
[]
\titlecontents{subsubsection}
[6.5em] % 5.2em + 1.3em
{\vspace{0.1cm}}
{\contentslabel{1.3em}}
{\hspace*{-1.3em}}
{\titlerule*[1pc]{.}\contentspage}
[]
\makeatletter
\let\perhapsprint\@firstofone
\patchcmd{\addtocontents}
{\glossary\@gobble}
{\glossary\@gobble\let\perhapsprint\@gobble}
{}{}
\makeatother
%\newcommand{\perhapsprint}[1]{\ifprintprefix#1\fi}
%\newif\ifprintprefix
\renewcommand\thesection{\Roman{section}}
\renewcommand\thesubsection{%
\perhapsprint{\Roman{section}.}%
\alph{subsection}%
}
\renewcommand\thesubsubsection{%
\perhapsprint{\Roman{section}.\alph{subsection}.}%
\arabic{subsubsection}%
}
% appendix:
\usepackage[toc,page]{appendix}
\renewcommand*\thesubsection{\Alph{subsection}}
\usepackage{hyperref}
% exclude number from the link in ToC:
\usepackage{etoc}
\makeatletter
% store the original table of contents macros (they are not modified by hyperref, only \contentsline is)
\let\latchapter\l@chapter
\let\latsection\l@section
\let\latsubsection\l@subsection
\let\latsubsubsection\l@subsubsection
% then use the etoc package to feed the l@ macros with a filtered out data
\etocsetstyle{chapter}{}{}
{\latchapter{\numberline{\etocthenumber}\etocname}{\etocpage}}{}
\etocsetstyle{section}{}{}
{\latsection{\numberline{\etocthenumber}\etocname}{\etocpage}}{}
\etocsetstyle{subsection}{}{}
{\latsubsection{\numberline{\etocthenumber}\etocname}{\etocpage}}{}
\etocsetstyle{subsubsection}{}{}
{\latsubsubsection{\numberline{\etocthenumber}\etocname}{\etocpage}}{}
\makeatother
\renewcommand\thesection{\Roman{section}}
\renewcommand\thesubsection{\alph{subsection}}
\renewcommand\thesubsubsection{\arabic{subsubsection}}
\renewcommand\theparagraph{\Roman{section}.\alph{subsection}.\arabic{subsubsection}.\roman{paragraph}}
\begin{document}
\tableofcontents
\section{H1}
\subsection{H2}
\subsubsection{H3}
\section{H1}
\subsection{H2}
\subsubsection{H3}
\addtocontents{toc}{\vspace*{\baselineskip}}
\renewcommand{\appendixtocname}{Appendices}
\renewcommand{\appendixpagename}{Appendices}
\begin{appendices}
\subsection{H2}
\subsubsection{H3}
\subsection{H2}
\subsubsection{H3}
\subsection{H2}
\subsubsection{H3}
\end{appendices}
\end{document}
(我也是排除目录中的章节编号)
答案1
正如评论中提到的那样,鉴于OP代码使用的其他软件包,最简单的补救措施可能是放弃使用埃托克。
以防某些用户etoc
发现这个问题,我仍然提供这个答案来解释处理该问题的方法etoc
:只需使用\etocifnumbered
命令。
\documentclass{article}
% appendix:
\usepackage[toc,page]{appendix}
\renewcommand*\thesubsection{\Alph{subsection}}
\usepackage{hyperref}
% exclude number from the link in ToC:
\usepackage{etoc}
\makeatletter
% store the original table of contents macros (they are not modified by hyperref, only \contentsline is)
\let\latchapter\l@chapter
\let\latsection\l@section
\let\latsubsection\l@subsection
\let\latsubsubsection\l@subsubsection
\etocsetstyle{chapter}{}{}
{\etocifnumbered
{\latchapter{\numberline{\etocthenumber}\etocname}{\etocpage}}
{\latchapter{\etocname}{\etocpage}}}{}
\etocsetstyle{section}{}{}
{\etocifnumbered
{\latsection{\numberline{\etocthenumber}\etocname}{\etocpage}}
{\latsection{\etocname}{\etocpage}}}{}
\etocsetstyle{subsection}{}{}
{\etocifnumbered
{\latsubsection{\numberline{\etocthenumber}\etocname}{\etocpage}}
{\latsubsection{\etocname}{\etocpage}}}{}
\etocsetstyle{subsubsection}{}{}
{\etocifnumbered
{\latsubsubsection{\numberline{\etocthenumber}\etocname}{\etocpage}}
{\latsubsubsection{\etocname}{\etocpage}}}{}
\makeatother
\renewcommand\thesection{\Roman{section}}
\renewcommand\thesubsection{\alph{subsection}}
\renewcommand\thesubsubsection{\arabic{subsubsection}}
\renewcommand\theparagraph{\Roman{section}.\alph{subsection}.\arabic{subsubsection}.\roman{paragraph}}
\begin{document}
\tableofcontents
\section{H1}
\subsection{H2}
\subsubsection{H3}
\section{H1}
\subsection{H2}
\subsubsection{H3}
\renewcommand{\appendixtocname}{Appendices}
\renewcommand{\appendixpagename}{Appendices}
\begin{appendices}
\subsection{H2}
\subsubsection{H3}
\subsection{H2}
\subsubsection{H3}
\subsection{H2}
\subsubsection{H3}
\end{appendices}
\end{document}