
Quero acrescentar algum texto aos títulos dos capítulos no sumário de acordo com certas condições. Por exemplo (isso não 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}
Eu seiisso é uma duplicata, mas quero saber se algo assim é possível porque tenho que testar uma série de condições. Nomeadamente:
- Este é o primeiro capítulo do TOC?
- Este título é igual a "foo"
- Etc.
Responder1
Como \@chapapp
é usado explicitamente, apenas redefini-lo \cftchappresnum
não é suficiente se não houver nenhuma informação sobre a mudança de nome, já que ToC
é escrito no momento em que a informação sobre \@chapapp
a mudança caiu no esquecimento ;-)
É melhor escrever esta informação no ToC
, com \addtocontents{toc}{\protect\renewcommand{...}}
(veja a linha relevante no código.
Em vez de mexer nas palavras explícitas para o \appendixname
ou \chaptername
é muito melhor deixar babel
de se preocupar com a mudança, a menos que se tenha ideias muito específicas sobre os nomes, desviando-se das babel
configurações para este idioma.
Deixando babel
fazer as alterações o código funcionaria para qualquer linguagem definida.
\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}