Anexe condicionalmente o texto ao título do capítulo no sumário

Anexe condicionalmente o texto ao título do capítulo no sumário

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 \cftchappresnumnã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 \@chapappa 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 \appendixnameou \chapternameé muito melhor deixar babelde se preocupar com a mudança, a menos que se tenha ideias muito específicas sobre os nomes, desviando-se das babelconfigurações para este idioma.

Deixando babelfazer 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} 

insira a descrição da imagem aqui

informação relacionada