
我需要從目錄中刪除我的小節。我通過使用這個來正確地完成該部分,沒有任何問題關聯。謝謝!
但我無法用於\ref{"label from my subsection"}
剛剛從目錄中刪除的所選小節。好吧,我仍然可以連結它,但我只會獲得對我的小節上方部分的引用。
範例:(我的小節未顯示在目錄中)
\section{Hello World!}
\label{sec:hello_world}
\subsection{Goodbye}
\label{sec:goodbye}
\ref{sec:goodbye}
--- Latex 將回傳結果如下 ---
1.世界你好!
1.1.再見
1
答案1
如果您希望\subsection
從目錄中刪除所有 s,那麼您的文件序言中只需要以下內容:
\setcounter{tocdepth}{1}
這會將 ToC 中條目的最大深度設為 1(或\section
)。
如果您只想將選定\subsections
內容插入/不在目錄中,那麼您應該使用不同的技術。下面定義\stoptocentries
和\starttocentries
。前者透過停用 來停用在目錄中插入內容\addcontentsline
。後者恢復了功能。
\documentclass{article}
\let\oldaddcontentsline\addcontentsline
\newcommand{\stoptocentries}{\renewcommand{\addcontentsline}[3]{}}
\newcommand{\starttocentries}{\let\addcontentsline\oldaddcontentsline}
\begin{document}
\tableofcontents
\section{A section}
\stoptocentries% Stop adding content to the ToC
\subsection{A subsection}
\starttocentries% Resume adding content to the ToC
\subsection{Another subsection}
\end{document}
當然, 的用法\stoptocentries
也適用於使用 的其他分段單元\addcontentsline
。
答案2
您可以使用套件開箱即用地執行此操作埃托克。
代碼:
\documentclass{article}
\usepackage{etoc}
\begin{document}
\tableofcontents
\section{First Section}
Hello
\subsection{A}
\subsection{B}
\section{Second Section}
\etocsettocdepth.toc {section}
\subsection {EXCLUDE ME}\label{ssec:excluded}
\etocsettocdepth.toc {subsection}
\subsection {D}
Too bad we have excluded subsection \ref{ssec:excluded} from the TOC!
\end{document}
答案3
我看到上面的答案完全回答了問題,但我在LaTeX網站上找到了另一種方法。
如果你想要隱藏目錄中的所有小節,你應該將計數器 tocdepth 設定為 1:
\setcounter{tocdepth}{1}
但如果您只想將其應用於某些(子)部分,您可以更改文件內的 toc深度:
\documentclass[a4paper,10pt]{book}
\begin{document}
\tableofcontents
\chapter{One}
\section{One}
\subsection{One}
\addtocontents{toc}{\protect\setcounter{tocdepth}{1}}
\subsection{Two}
\addtocontents{toc}{\protect\setcounter{tocdepth}{2}}
\subsection{Three}
\end{document}