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}
Sé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 \@chapapp
se usa explícitamente, simplemente redefinirlo \cftchappresnum
no es suficiente si no hay ninguna información sobre el cambio de nombre, ya que se ToC
escribe en un momento en que la información sobre \@chapapp
el 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 \appendixname
o, \chaptername
es mucho mejor dejar de babel
preocuparse por el cambio, a menos que uno tenga ideas muy específicas sobre los nombres, desviándose de la babel
configuración de este idioma.
Dejar babel
hacer 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}