\titlecontents 内の \if 条件の失敗

\titlecontents 内の \if 条件の失敗

toc何らかの条件を使用して、contentslabel の前に何らかの単語を追加する必要があります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

目次がタイプセットされている場合はfalse を返します。ファイルにも\ifappendix渡す必要があります。\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}

ここに画像の説明を入力してください

関連情報