0

I want to prepend some text to chapter titles on the TOC according to certain conditions. For example (this doesn't work):

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

I know this is a duplicate, but I want to know if something like this is possible because I have to test for a number of conditions. Namely:

  • Is this the first chapter of the TOC?
  • Is this title equal to "foo"
  • Etc.
lockstep
  • 250,273
Federico
  • 199
  • 1
  • 1
  • 9
  • If you used memoir instead, then the example would be defunct. There it is just \renewcommand\cftchaptername{\chaptername~}\renewcommand\cftappendixname{\appendixname~}. Don't know why this has not been added to tocloft. – daleif Feb 18 '15 at 12:29
  • The ToC is set at a different time than the actual document content. So a conditional based on \@chapapp would only work if it changed during the setting of the ToC, which it doesn't. Of course, one could do that, but there are other ways of doing this - by writing to the ToC to change the definition of \@chapapp at the appropriate moment. – Werner Feb 21 '15 at 00:32
  • Is there any news here? – Johannes_B May 09 '16 at 09:06

1 Answers1

1

Since \@chapapp is explicitly used, just redefining it in \cftchappresnum is not sufficient if there is no information about the change in name at all, since the ToC is written at a time the information about \@chapapp change has gone to oblivion ;-)

It is better to write this information to the ToC, with \addtocontents{toc}{\protect\renewcommand{...}} (see the relevant line in the code.

Instead of fiddling with the explicit words for the \appendixname or \chaptername it is much better to let babel care about the change, unless one has very specific ideas about the names, deviating from the babel settings for this language.

Letting babel do the changes the code would work for any defined language.

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

enter image description here