特定の条件に従って、目次の章タイトルの先頭にテキストを追加したいです。例: (これは機能しません):
\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}
知っているこれは重複ですしかし、いくつかの条件をテストする必要があるため、このようなことが可能かどうかを知りたいのです。具体的には、
- これは目次の最初の章ですか?
- このタイトルは「foo」と同じですか
- 等。
答え1
が明示的に使用されているため、名前の変更に関する情報がまったくない場合は、\@chapapp
でそれを再定義するだけでは不十分です。これは、変更に関する情報が忘れ去られた時点で が書き込まれているためです ;-)\cftchappresnum
ToC
\@chapapp
ToC
この情報は、に書き込むことをお勧めします\addtocontents{toc}{\protect\renewcommand{...}}
(コード内の関連行を参照)。
\appendixname
またはの明示的な単語をいじる代わりに、この言語の設定から逸脱する名前について非常に具体的な考えがない限り、変更を気にする方\chaptername
がはるかに適切です。babel
babel
変更を加えるとbabel
、コードは定義されたどの言語でも機能するようになります。
\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}