Fehlschlagen der \if-Bedingung innerhalb von \titlecontents

Fehlschlagen der \if-Bedingung innerhalb von \titlecontents

Ich muss tocunter bestimmten Bedingungen vor dem Inhaltslabel ein Wort hinzufügen 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}

MWEWie man an der Bedingung erkennen kann, \titlecontentsfunktioniert es im Inneren nicht (ich habe ein rotes Wort im Bild erwartet), aber gleichzeitig funktioniert es mit \titleformatdem Makro.

Bildbeschreibung hier eingeben

Antwort1

Wenn das Inhaltsverzeichnis gesetzt wird, \ifappendixwird „false“ zurückgegeben. Sie müssen es \appendixtrueauch an die .tocDatei weitergeben.

\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}

Bildbeschreibung hier eingeben

verwandte Informationen