问题在于标题 - 如何隐藏(排除)书籍类文档(pdflatex)中某一章节的所有部分?提前致谢
答案1
改进版本,使用用户定义的\HideFromTOC{2,4,8}
命令自动从目录中删除,数字为章节编号:
\documentclass{book}
\usepackage{xparse}
\usepackage{xpatch}
\newcommand{\hidelevel}{0} % 0 means chapter and part only
\newcounter{backuptocdepth}
\AtBeginDocument{%
\setcounter{backuptocdepth}{\value{tocdepth}}%
}
\ExplSyntaxOn%
\clist_new:N \g_chapterhide_clist
\newcommand{\HideFromTOC}[1]{%
\clist_gclear:N \g_chapterhide_clist%
\clist_gset:Nx \g_chapterhide_clist {#1}
}
\newcommand{\checkiftohide}[1]{%
\tl_set:Nx \l_tmpa_tl {#1}
\clist_if_in:NVT \g_chapterhide_clist {\l_tmpa_tl}{%
\addtocontents{toc}{\protect\setcounter{tocdepth}{\hidelevel}}
}
}
\ExplSyntaxOff
\makeatletter
\xpatchcmd{\@chapter}{%
\ifnum \c@secnumdepth >\m@ne
}{%
\checkiftohide{\the\numexpr\value{chapter}+1}%
\ifnum \c@secnumdepth >\m@ne
}{\typeout{Success}}{\typeout{Patch failure}}
\xpretocmd{\chapter}{%
\setcounter{tocdepth}{\value{backuptocdepth}}%
\addtocontents{toc}{\protect\setcounter{tocdepth}{\number\value{tocdepth}}}% Write the old value back to ToC
}{}{} % Restore the old tocdepth counter value
\makeatother
\HideFromTOC{2,4,8}
\begin{document}
\tableofcontents
\chapter{First}
\section{Foo}
\section{Bar}
\chapter{Second}
\section{Foo}
\section{Bar}
\chapter{Third}
\section{Foo}
\section{Bar}
\chapter{Fourth}
\section{Foo}
\section{Bar}
\chapter{Fifth}
\section{Foo}
\section{Bar}
\chapter{Sixth}
\section{Foo}
\section{Bar}
\chapter{Seventh}
\section{Foo}
\section{Bar}
\chapter{Eighth}
\section{Foo}
\section{Bar}
\end{document}
输出:
旧版
第一个版本被tocdepth
设置为0
临时的,稍后再恢复。
\documentclass{book}
\begin{document}
\tableofcontents
\chapter{First}
\section{Foo}
\section{Bar}
\chapter{Second}
\section{Foo}
\section{Bar}
\chapter{Third}
\addtocontents{toc}{\setcounter{tocdepth}{0}}
\section{Foo}
\section{Bar}
\addtocontents{toc}{\setcounter{tocdepth}{2}}
\chapter{Fourth}
\section{Foo}
\section{Bar}
\chapter{Fifth}
\section{Foo}
\section{Bar}
\end{document}
答案2
最简单的解决方案是
\newcommand{\emptysection}[1]{%
\begingroup\renewcommand\addcontentsline[3]{}%
\section{#1}\endgroup%
}