
我需要toc
在某些條件下使用. 在 contentlabel 之前添加一些單字titletoc
。
\documentclass[]{report}
\usepackage{titlesec, titletoc}
\newif{\ifappendix}
\titlecontents{section}% <section-type>
[2em]% <left>
{}% <above-code>
{\ifappendix \appendixname~\fi\thecontentslabel\ }% <numbered-entry-format>
{}% <numberless-entry-format>
{\titlerule*[0.75pc]{.}\contentspage}% <filler-page-format>\\
\titleformat{\section}[block]
{\hspace{\parindent}}
{\ifappendix\appendixname~\fi\thesection}
{1em}{}
\begin{document}
\tableofcontents
\section{One}
\appendixtrue
\section{Two}
\end{document}
從MWE
裡面的條件可以看出\titlecontents
,它不起作用(我期望圖片中出現紅色字),但同時它確實可以與\titleformat
巨集一起使用。
答案1
當目錄正在排版時,\ifappendix
傳回 false。您還需要傳遞\appendixtrue
到該.toc
文件。
\documentclass[]{report}
\usepackage{titlesec, titletoc}
\newif{\ifappendix}
\titlecontents{section}% <section-type>
[2em]% <left>
{}% <above-code>
{\ifappendix \appendixname~\fi\thecontentslabel\ }% <numbered-entry-format>
{}% <numberless-entry-format>
{\titlerule*[0.75pc]{.}\contentspage}% <filler-page-format>\\
\titleformat{\section}[block]
{\hspace{\parindent}}
{\ifappendix\appendixname~\fi\thesection}
{1em}{}
\begin{document}
\tableofcontents
\section{One}
\appendixtrue
\addtocontents{toc}{\protect\appendixtrue}
\section{Two}
\end{document}