책 클래스의 ToC(챕터용)에서 제목 줄 텍스트의 접두어를 지정하고 줄 바꿈하는 방법은 무엇입니까?

책 클래스의 ToC(챕터용)에서 제목 줄 텍스트의 접두어를 지정하고 줄 바꿈하는 방법은 무엇입니까?

책 클래스의 ToC에서 장 번호 매기기의 접두어로 "Chapter" 텍스트를 포함하고 장 제목을 ToC 목록의 가장 왼쪽 가장자리에 배치하는 방법을 찾고 있습니다. 나는 이것을 현재 두 부분으로 된 공통 리소스로 구축하고 있습니다. 하나는 패키지만 포함된 클래스 파일이고 다른 하나는 프리앰블의 \input에 있는 설정 .tex 파일입니다. 설정 파일 내에서 두 가지 다른 접근 방식을 시도했습니다. 아래 첫 번째는 필요한 패치를 올바르게 캡처하지 않습니다.

\documentclass{book}

\usepackage{xpatch}
\usepackage{hyperref}

\makeatletter
\xpatchcmd{\@chapter}
    {\addcontentsline{toc}{chapter}{\numberline {#1}}}
    {\addcontentsline{toc}{chapter}{\numberline {Chapter~\thechapter{}.~#1}}}
    {\typeout{Success}}
    {\typeout{Failed!}}
\makeatother

\begin{document}

\tableofcontents

\chapter{This is First}

Some day this will work.

\chapter{This is Second}

Perhaps soon!

\end{document}

이 두 번째 접근 방식은 Chapter 단어를 ToC에 배치하지만 더 긴 Chapter 2 제목의 텍스트가 ToC 줄에서 요구되는 대로 줄바꿈되지 않습니다.

\documentclass{book}

\usepackage{tocloft}

\begin{document}

\newlength{\tocchapwidth}
\settowidth{\tocchapwidth}{\chaptername\ }
\addtolength{\cftchapnumwidth}{\tocchapwidth}
\renewcommand{\cftchappresnum}{\chaptername\ }
\renewcommand{\cftchapaftersnum}{.}
\renewcommand{\cftchapaftersnumb}{~}
\renewcommand{\cftchapdotsep}{\cftdotsep}

\tableofcontents

\chapter{This is First}

Some day this will work.

\chapter{This is Second, a Rather Long and Windy Road Leading to a Door Going to Nowhere Fast}

Perhaps soon!

\end{document}

이는 논문/논문 문서의 형식 요구 사항을 충족하려는 시도입니다. 현재 대략적인 해결 방법이 있습니다. 접두사를 포함하여 장 제목을 입력합니다(예: \chapter{Chapter 2.~This is the Second ...})(그리고 titlesec 패키지를 사용하여 ToC에서 장 번호 출력을 억제합니다).

또한 두 번째 예에는 포함하지 않았지만 문서에는 hyperref 패키지가 필요합니다. 별도의 게시물에서 Hy@... 장 명령을 패치해야 할 수도 있다고 추론했지만 이를 수행하려는 시도조차 실패했습니다.

나는 LaTeX3에 대해 충분히 잘 알지 못하지만 그러한 수정 사항을 포함하게 되어 기쁩니다. 마지막으로, 제공되는 옵션이 무엇이든 간에 패키지를 사용할 많은 사람들이 작업을 수행할 가능성이 높은 Overleaf에서 쉽게 작동할 수 있기를 바랍니다.

답변1

첫 번째 접근 방식에는 몇 가지 문제가 있습니다.

우선, 의 원래 정의에도 다음이 book.cls없습니다.

\addcontentsline{toc}{chapter}{\numberline {#1}}

의 정의에서는 \@chapter다음과 같습니다.

\addcontentsline{toc}{chapter}{\protect\numberline{\thechapter}#1}

따라서 \protect에 누락된 올바른 인수가 있으며 \numberline첫 번째 인수는 \@chapter중괄호 없이 사용됩니다.

hyperref두 번째 문제는 변경 정의를 로드한 후입니다 \@chapter. 따라서 패치는 다음과 같아야 합니다.~ 전에로딩 hyperref.

참고: 원래 정의를 보려면 \show\@chapterbefore 를 추가하세요 \xpatchcmd. -file \@chapter에 패치하기 전에 현재 정의가 출력됩니다 log.

세 번째 문제는 교체가 다음과 같다는 것입니다.

\addcontentsline{toc}{chapter}{\protect\numberline {Chapter~\thechapter{}.~#1}}

Chapter~<number>.숫자 인수뿐만 아니라 제목도 입력합니다 . 따라서 최소한 다음과 같아야 합니다.

\addcontentsline{toc}{chapter}{\numberline {Chapter~\thechapter{}.}#1}

\l@chapter그러나 이 정의의 경우 숫자에 예약된 공간을 늘리려면 을 변경해야 합니다 . 어쩌면 대신에 숫자 공간을 예약하고 싶지 않고 대신 다음을 사용하고 싶을 수도 있습니다.

\addcontentsline{toc}{chapter}{Chapter~\thechapter{}.~#1}

그래서:

\documentclass{book}

\usepackage{xpatch}

\makeatletter
\xpatchcmd{\@chapter}
  {\addcontentsline{toc}{chapter}{\protect\numberline{\thechapter}#1}}
  {\addcontentsline{toc}{chapter}{Chapter~\thechapter{}.~#1}}
  {\typeout{Success}}
  {\typeout{Failed!}}
\makeatother

\usepackage{hyperref}

\begin{document}

\tableofcontents

\chapter{This is First}

Some day this will work.

\chapter{This is Second}

Perhaps soon!

\end{document}

당신은 다음을 얻습니다:

1장. 이것은…

Chapter <number>.인수로 사용하는 대안 은 \numberline다음과 같습니다.

\documentclass{book}

\usepackage{xpatch}

\makeatletter
\xpatchcmd{\@chapter}
  {\addcontentsline{toc}{chapter}{\protect\numberline{\thechapter}#1}}
  {\addcontentsline{toc}{chapter}{\protect\numberline{Chapter~\thechapter{}.}#1}}
  {\typeout{\string\@chapter\space patch success}}
  {\typeout{\string\@chapter\space patch failed!}}
\xpatchcmd{\l@chapter}
  {\setlength\@tempdima{1.5em}}
  {\setlength\@tempdima{6em}}% increase reserved number width
  {\typeout{\string\l@chapter\space patch success}}
  {\typeout{\string\l@chapter\space patch failed!}}
\makeatother

\usepackage{hyperref}

\begin{document}

\tableofcontents

\chapter{This is First}

Some day this will work.

\chapter{This is Second}

Perhaps soon!

\end{document}

한 줄 항목에 대한 결과는 첫 번째 제안과 유사합니다. 차이점을 보려면 두 줄 이상으로 항목을 입력해 보세요. 예:

\chapter{This is Second Chapter with very long Titel Needing more than one
  Line in the Table of Contents}

그래서 IMHO 내 두 번째 접근 방식은 당신이 원하는 것입니다.


두 번째 접근 방식의 문제점은 증가할 때 숫자 뒤의 점 .과 공백을 고려하지 않는다는 것입니다 . 설정을 수정할 수 있는 방법은 다음과 같습니다.~\cftchapnumwidth

\documentclass{book}

\usepackage{tocloft}

\begin{document}

\newlength{\tocchapwidth}
\renewcommand{\cftchappresnum}{\chaptername\ }
\settowidth{\tocchapwidth}{\cftchappresnum}
\renewcommand{\cftchapaftersnum}{.~}
\newlength{\tocchapafternumwidth}
\settowidth{\tocchapafternumwidth}{\cftchapaftersnum}
\addtolength{\cftchapnumwidth}{\tocchapwidth}
\addtolength{\cftchapnumwidth}{\tocchapafternumwidth}
\renewcommand{\cftchapdotsep}{\cftdotsep}

\tableofcontents

\chapter{This is First}

Some day this will work.

\chapter{This is Second, a Rather Long and Windy Road Leading to a Door Going to Nowhere Fast}

Perhaps soon!

\end{document}

토클로프트를 사용하여

그러나 이 제안을 자세히 살펴보면 장 항목에 사용된 글꼴이 아닌 현재 문서 글꼴로 텍스트를 측정하고 있기 때문에 여전히 정확하지 않습니다. 따라서 이전 코드는 올바른 것처럼 보이지만 그렇지 않습니다. 올바른 코드를 얻으려면 \cftchapfont텍스트 너비를 측정할 때 고려해야 합니다 . (이제 접두사 Chapter와 접미사가 없는 숫자 너비가 이미 충분하다면 공백만큼 너비를 늘릴 필요가 없습니다 ..) 따라서 더 나은 코드는 다음과 같습니다.

\documentclass{book}

\usepackage{tocloft}

\newlength{\tocchapwidth}
\newlength{\tocchapafternumwidth}
\renewcommand{\cftchappresnum}{\chaptername\ }
\renewcommand{\cftchapaftersnum}{.}
\renewcommand{\cftchapdotsep}{\cftdotsep}
\AtBeginDocument{% final font setup is done in \begin{document} so we delay all text measuring
  \settowidth{\tocchapwidth}{\cftchapfont\cftchappresnum}%
  \settowidth{\tocchapafternumwidth}{\cftchapfont\cftchapaftersnum}%
  \addtolength{\cftchapnumwidth}{\tocchapwidth}%
  \addtolength{\cftchapnumwidth}{\tocchapafternumwidth}%
}

\begin{document}
\tableofcontents

\chapter{This is First}

Some day this will work.

\chapter{This is Second, a Rather Long and Windy Road Leading to a Door Going to Nowhere Fast}

Perhaps soon!

\end{document}

부록 문제

참고: 두 예 모두 부록이 있는 문서에는 적합하지 않습니다. 왜냐하면 이 경우 Chapter부록의 올바른 용어가 아니기 때문입니다.

부록이 있는 문서에는 적합 하지 않은 추천 제안을 만드는 것이 tocloft쉬울 것입니다. 하드 코딩 Chapter된 코드를 대체 \@chapapp하고 너비를 늘리면 됩니다 .

\documentclass{book}

\usepackage{xpatch}

\makeatletter
\xpatchcmd{\@chapter}
  {\addcontentsline{toc}{chapter}{\protect\numberline{\thechapter}#1}}
  {\addcontentsline{toc}{chapter}{\protect\numberline{\@chapapp~\thechapter{}.}#1}}
  {\typeout{\string\@chapter\space patch success}}
  {\typeout{\string\@chapter\space patch failed!}}
\xpatchcmd{\l@chapter}
  {\setlength\@tempdima{1.5em}}
  {\setlength\@tempdima{7em}}% increase reserved number width
  {\typeout{\string\l@chapter\space patch success}}
  {\typeout{\string\l@chapter\space patch failed!}}
\makeatother

\usepackage{hyperref}

\begin{document}

\tableofcontents

\chapter{This is First}

Some day this will work.

\chapter{This is Second}

Perhaps soon!

\appendix
\chapter{This is a Rather Long and Windy Road Leading to a Door Going
  to Nowhere Fast}

\end{document}

솔루션 의 경우 tocloft다음과 같은 것을 사용할 수 있습니다.

\documentclass{book}

\usepackage{tocloft}

\newlength{\tocchapwidth}
\newlength{\tocappwidth}
\newlength{\tocchapafternumwidth}
\renewcommand{\cftchappresnum}{\chaptername\ }
\renewcommand{\cftchapaftersnum}{.}
\renewcommand{\cftchapdotsep}{\cftdotsep}
\AtBeginDocument{%
  \settowidth{\tocchapwidth}{\cftchapfont\chaptername}%
  \settowidth{\tocappwidth}{\cftchapfont\appendixname}%
  \ifdim\tocappwidth>\tocchapwidth
    \addtolength{\cftchapnumwidth}{\tocappwidth}%
  \else
    \addtolength{\cftchapnumwidth}{\tocchapwidth}%
  \fi
  \settowidth{\tocchapafternumwidth}{\cftchapfont\cftchapaftersnum}%
  \addtolength{\cftchapnumwidth}{\tocchapafternumwidth}%
}
\AddToHook{cmd/appendix/after}{%
  \addtocontents{toc}{%
    \protect\renewcommand{\protect\cftchappresnum}{\protect\appendixname{} }%
  }%
}
\begin{document}
\tableofcontents

\chapter{This is First}

Some day this will work.

\chapter{This is Second, a Rather Long and Windy Road Leading to a Door Going to Nowhere Fast}

Perhaps soon!

\appendix
\chapter{A Rather Long Appendix Heading and Windy Road Leading to a Door Going to Nowhere Fast}


\end{document}

관련 정보