答案1
代码与所有宏等\@sect
相关,并且检查值。通过用另一个计数器值替换它,可以隐藏目录中的编号。\section
\addcontentsline{toc}{#1}{% \ifnum #2>\c@secnumdepth...
secnumdepth
\c@secnumdepth
secnumtocdepth
我已经为此引入了计数器。
\documentclass{article}
\newcounter{secnumtocdepth}
\setcounter{secnumtocdepth}{2}
\makeatletter
\AtBeginDocument{%
\def\@sect#1#2#3#4#5#6[#7]#8{%
\ifnum #2>\c@secnumdepth
\let\@svsec\@empty
\else
\refstepcounter{#1}%
\protected@edef\@svsec{\@seccntformat{#1}\relax}%
\fi
\@tempskipa #5\relax
\ifdim \@tempskipa>\z@
\begingroup
#6{%
\@hangfrom{\hskip #3\relax\@svsec}%
\interlinepenalty \@M #8\@@par}%
\endgroup
\csname #1mark\endcsname{#7}%
\addcontentsline{toc}{#1}{%
\ifnum #2>\c@secnumtocdepth \else
\protect\numberline{\csname the#1\endcsname}%
\fi
#7}%
\else
\def\@svsechd{%
#6{\hskip #3\relax
\@svsec #8}%
\csname #1mark\endcsname{#7}%
\addcontentsline{toc}{#1}{%
\ifnum #2>\c@secnumtocdepth \else
\protect\numberline{\csname the#1\endcsname}%
\fi
#7}}%
\fi
\@xsect{#5}}
}
\makeatother
\newcommand{\leveltohide}{2}
\begin{document}
\tableofcontents
\section{First section}
\subsection{Foo subsection}
\subsubsection{Foo subsubsection}
\section{Second section}
\subsection{Foo subsection}
\subsubsection{Foo subsubsection}
\end{document}
答案2
对于标准类,您可以使用包tocbasic
:
\documentclass{article}
\usepackage{tocbasic}
\newcommand\gobble[1]{}
\DeclareTOCStyleEntry[
entrynumberformat=\gobble,
numwidth=0pt
]{tocline}{subsubsection}
\begin{document}
\tableofcontents
\section{First section}
\subsection{Foo subsection}
\subsubsection{Foo subsubsection}
\section{Second section}
\subsection{Foo subsection}
\subsubsection{Foo subsubsection}
\end{document}
结果:
这也适用于 KOMA-Script 类,但tocbasic
已被该类加载。
有KOMA 脚本你也可以使用\RedeclareSectionCommand
\documentclass{scrartcl}
\newcommand\gobble[1]{}
\RedeclareSectionCommand[
tocentrynumberformat=\gobble,
tocnumwidth=0pt
]{subsubsection}
\begin{document}
\tableofcontents
\section{First section}
\subsection{Foo subsection}
\subsubsection{Foo subsubsection}
\section{Second section}
\subsection{Foo subsection}
\subsubsection{Foo subsubsection}
\end{document}
或者你可以重新定义\addsubsubsectiontocentry
:
\documentclass{scrartcl}
\renewcommand\addsubsubsectiontocentry[2]{%
\addtocentrydefault{subsubsection}{}{#2}%
}
\begin{document}
\tableofcontents
\section{First section}
\subsection{Foo subsection}
\subsubsection{Foo subsubsection}
\section{Second section}
\subsection{Foo subsection}
\subsubsection{Foo subsubsection}
\end{document}
结果: