Bedingtes Voranstellen von Text vor Kapiteltiteln im Inhaltsverzeichnis

Bedingtes Voranstellen von Text vor Kapiteltiteln im Inhaltsverzeichnis

Ich möchte den Kapitelüberschriften im Inhaltsverzeichnis unter bestimmten Bedingungen Text voranstellen. Beispiel (das funktioniert nicht):

\documentclass[letterpaper,openany,oneside]{book}
\usepackage[spanish]{babel}
\usepackage[utf8]{inputenc}
\usepackage{tocloft}

% If the chapter is an appendix, print "Apéndice", else "Capítulo".
\makeatletter
\renewcommand\cftchappresnum{
\ifx\@chapapp\appendixname
Apéndice\
\else 
Capítulo\
\fi
 }
\makeatother

\renewcommand\cftchapaftersnumb{\newline}
\renewcommand\cftchapleader{\cftdotfill{4}}
\renewcommand\cftchappagefont{\normalfont}
\setlength{\cftchapnumwidth}{0em}

\begin{document}    
\tableofcontents
\mainmatter
\chapter{First chapter}
  \section{First section}
  \chapter{Another chapter}
  \section{this is yet another section} 
\appendix
\chapter{First Appendix}
\end{document} 

Ich weißDies ist ein Duplikat, aber ich möchte wissen, ob so etwas möglich ist, weil ich eine Reihe von Bedingungen testen muss. Und zwar:

  • Ist dies das erste Kapitel des Inhaltsverzeichnisses?
  • Ist dieser Titel gleichbedeutend mit "foo"
  • Usw.

Antwort1

Da \@chapappexplizit verwendet wird, \cftchappresnumreicht eine bloße Neudefinition in nicht aus, wenn überhaupt keine Informationen über die Namensänderung vorliegen, da dies ToCzu einem Zeitpunkt geschrieben wird, zu dem die Informationen über \@chapappdie Änderung bereits in Vergessenheit geraten sind ;-)

Besser ist es, diese Information in das zu schreiben ToC, mit \addtocontents{toc}{\protect\renewcommand{...}}(siehe entsprechende Zeile im Code).

Anstatt an den expliziten Wörtern für das \appendixnameoder herumzufummeln \chaptername, ist es viel besser, babeldie Änderung selbst zu überlassen, es sei denn, man hat sehr konkrete Vorstellungen bezüglich der Namen, die von den babelEinstellungen für diese Sprache abweichen.

Durch das Zulassen babelder Änderungen würde der Code für jede definierte Sprache funktionieren.

\documentclass[letterpaper,openany,oneside]{book}


\usepackage[utf8]{inputenc}
\usepackage[spanish]{babel}

\usepackage{tocloft}

\usepackage{xpatch}


% If the chapter is an appendix, print "Apéndice", else "Capítulo".
\makeatletter
\DeclareRobustCommand{\gettherightprefix}{
  \renewcommand{\cftchappresnum}{%
    \@chapapp~%
  }
}
\AtBeginDocument{%
  \addtocontents{toc}{\gettherightprefix}% Just use the protected version of this instead of a lot of \protect statements
}
\xpretocmd\appendix{%
  % Inform the ToC that `\@chapapp` is `\appendixname now
  \addtocontents{toc}{\protect\renewcommand{\protect\@chapapp}{\protect\appendixname}}
}{\typeout{Success}}{\typeout{Failure}}

\makeatother

\renewcommand\cftchapaftersnumb{\newline}
\renewcommand\cftchapleader{\cftdotfill{4}}
\renewcommand\cftchappagefont{\normalfont}
\setlength{\cftchapnumwidth}{0em}

\begin{document} 
\tableofcontents
\mainmatter
\chapter{First chapter}
  \section{First section}
  \chapter{Another chapter}
  \section{this is yet another section} 
\appendix
\chapter{First Appendix}
\end{document} 

Bildbeschreibung hier eingeben

verwandte Informationen