Anteponer texto condicionalmente al título del capítulo en TOC

Anteponer texto condicionalmente al título del capítulo en TOC

Quiero anteponer algo de texto a los títulos de los capítulos del TOC de acuerdo con ciertas condiciones. Por ejemplo (esto no funciona):

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

este es un duplicado, pero quiero saber si algo como esto es posible porque tengo que probar varias condiciones. A saber:

  • ¿Es este el primer capítulo del TOC?
  • ¿Este título es igual a "foo"?
  • Etc.

Respuesta1

Dado que \@chapappse usa explícitamente, simplemente redefinirlo \cftchappresnumno es suficiente si no hay ninguna información sobre el cambio de nombre, ya que se ToCescribe en un momento en que la información sobre \@chapappel cambio ha pasado al olvido ;-)

Es mejor escribir esta información en ToC, con \addtocontents{toc}{\protect\renewcommand{...}}(consulte la línea correspondiente en el código.

En lugar de jugar con las palabras explícitas para \appendixnameo, \chapternamees mucho mejor dejar de babelpreocuparse por el cambio, a menos que uno tenga ideas muy específicas sobre los nombres, desviándose de la babelconfiguración de este idioma.

Dejar babelhacer los cambios, el código funcionaría para cualquier idioma definido.

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

ingrese la descripción de la imagen aquí

información relacionada