Невыполнение условия \if внутри \titlecontents

Невыполнение условия \if внутри \titlecontents

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

введите описание изображения здесь

Связанный контент