TOC의 장 제목에 조건부로 텍스트 추가

TOC의 장 제목에 조건부로 텍스트 추가

특정 조건에 따라 TOC의 장 제목 앞에 일부 텍스트를 추가하고 싶습니다. 예를 들어(이것은 작동하지 않습니다):

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

알아요이건 중복이야, 하지만 여러 조건을 테스트해야 하기 때문에 이와 같은 것이 가능한지 알고 싶습니다. 즉:

  • 이것이 TOC의 첫 번째 장입니까?
  • 이 제목이 "foo"와 같은가요?
  • 등.

답변1

는 명시적으로 사용되므로 이름 변경에 대한 정보가 전혀 없으면 \@chapapp재정의하는 것만으로는 충분하지 않습니다. 변경에 대한 정보가 망각된 시점에 작성되기 때문입니다 ;-)\cftchappresnumToC\@chapapp

ToC이 정보를 , with \addtocontents{toc}{\protect\renewcommand{...}}(코드의 관련 줄 참조) 에 작성하는 것이 좋습니다 .

\appendixname또는 에 대한 명시적인 단어를 조작하는 대신 이 언어의 설정 에서 벗어나 이름에 대한 매우 구체적인 아이디어가 없는 한 변경 사항에 신경쓰는 것이 \chaptername훨씬 좋습니다 .babelbabel

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} 

여기에 이미지 설명을 입력하세요

관련 정보