
Мне нужно добавить какое-то слово перед меткой содержимого toc
по некоторому условию, используя 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}