ToC의 Titlesec 쇼

ToC의 Titlesec 쇼

나는 titlesec내 책의 각 장의 작가와 번역자를 다음과 같이 표시합니다.

\documentclass[10pt,a4paper]{book}
\usepackage[utf8]{inputenc}
\usepackage{fontspec}
\usepackage{titlesec}
\newcommand{\chapterauthor}{}
\newcommand{\chaptertranslator}{}

\titleformat{\chapter}% command to format the chapter titles
[hang]% shape/type of title
{\LARGE\bfseries}% formatting commands applied to both label and title
{\makebox[0.5in][l]{\thechapter}}% chapter number; here set inside an invisible box with a constant width
{0em}% separation between number and chapter title; we've already covered this with the box
{}% additional formatting command for title itself not applied to number
[% everything inside [...] below comes after the title
\hfill% 
\normalsize\normalfont% reset font formatting
\vspace{0.5\baselineskip}% add a half-space of vertical space before author
\hspace*{0.5in}% indent author name width of chapter number box 
\begin{tabular}{rl} %
    Author: & \kern-0.5em\chapterauthor \\
    Translator: & \kern-0.5em\chaptertranslator\\
\end{tabular}%
]% end of what comes after title
\titlespacing*{\chapter}% spacing commands for chapter titles; the star unindents first line afterwards
{0em}% spacing to left of chapter title
{0ex}% vertical space before title 
{3\baselineskip}% vertical spacing after title; here set to 3 lines 
\begin{document}
\tableofcontents
\renewcommand{\chapterauthor}{Alphonse Lamartine}
\renewcommand{\chaptertranslator}{Ine}

\chapter{New chapter}
Here it is. 
\renewcommand{\chapterauthor}{Heinrich Böll}
\renewcommand{\chaptertranslator}{Llöb Chirnieh}
\chapter{Another chapter}
Here again.


\end{document}

매 장마다 나는 저자와 번역자를 다음과 같이 재정의합니다.

\renewcommand{\chapterauthor}{Heinrich Böll}
\renewcommand{\chaptertranslator}{Llöb Chirnieh}

문제는 이것이 목차를 하나의 장으로 취급하고 목차의 작성자나 번역자가 없기 때문에 다음과 같이 ToC 바로 위에 빈 작성자와 번역자가 표시된다는 것입니다.

    Author:
Translator:

이를 어떻게 방지해야 합니까?

\boolfalse{withauthor}===편집: 님 의 Bernard댓글이 충분하지 않음 을 보여주기 위해 ===

\documentclass[10pt,a4paper]{book}
\usepackage[utf8]{inputenc}
\usepackage{fontspec}
\usepackage{lipsum}
\usepackage{titlesec}
\usepackage{etoolbox}
\newbool{withauthor}

\newcommand{\chapterauthor}{}
\newcommand{\chaptertranslator}{}

\titleformat{\chapter}% command to format the chapter titles
[hang]% shape/type of title
{\LARGE\bfseries}% formatting commands applied to both label and title
{\makebox[0.5in][l]{\thechapter}}% chapter number; here set inside an invisible box with a constant width
{0em}% separation between number and chapter title; we've already covered this with the box
{}% additional formatting command for title itself not applied to number
[% everything inside [...] below comes after the title
\ifbool{withauthor}{\hfill% 
\normalsize\normalfont% reset font formatting
\vspace{0.5\baselineskip}% add a half-space of vertical space before author
\hspace*{0.5in}% indent author name width of chapter number box 
\begin{tabular}{rl} %
    Author: & \kern-0.5em\chapterauthor \\
    Translator: & \kern-0.5em\chaptertranslator\\
\end{tabular}}{}%
\boolfalse{withauthor}
]% end of what comes after title
\titlespacing*{\chapter}% spacing commands for chapter titles; the star unindents first line afterwards
{0em}% spacing to left of chapter title
{0ex}% vertical space before title 
{3\baselineskip}% vertical spacing after title; here set to 3 lines 
\begin{document}
\tableofcontents
\renewcommand{\chapterauthor}{Alphonse Lamartine}
\renewcommand{\chaptertranslator}{Ine}
\booltrue{withauthor}
\chapter{New chapter}
Here it is.
%\renewcommand{\chapterauthor}{Heinrich Böll}
%\renewcommand{\chaptertranslator}{Llöb Chirnieh}
%\boolfalse{withauthor}
\chapter{Another chapter}
Here again.


\end{document}

답변1

번호가 매겨지지 않은 모든 장에는 저자나 번역자가 없을 것이며, 번호가 매겨진 모든 장에도 그럴 것이라고 가정합니다.

\documentclass[10pt,a4paper]{book}
\usepackage[utf8]{inputenc}
\usepackage{fontspec}
\usepackage[showframe, nomarginpar]{geometry}
\usepackage{lipsum}
\usepackage{array}
\usepackage{titlesec}
\usepackage{etoolbox}
\newbool{withauthor}

\newcommand{\chapterauthor}{}
\newcommand{\chaptertranslator}{}

\titleformat{\chapter}% command to format the chapter titles
[hang]% shape/type of title
{\LARGE\bfseries}% formatting commands applied to both label and title
{\makebox[0.5in][l]{\thechapter}}% chapter number; here set inside an invisible box with a constant width
{0em}% separation between number and chapter title; we've already covered this with the box
{}% additional formatting command for title itself not applied to number
[% everything inside [...] below comes after the title
\ifbool{withauthor}{\hfill%
\normalsize\normalfont% reset font formatting
\vspace{0.5\baselineskip}% add a half-space of vertical space before author
\hspace*{0.5in}% indent author name width of chapter number box
\begin{tabular}{ll} %
    Author: & \kern-0.5em\chapterauthor \\
    Translator: & \kern-0.5em\chaptertranslator\\
\end{tabular}}{}%
\global\boolfalse{withauthor}
]% end of what comes after title
\titlespacing*{\chapter}% spacing commands for chapter titles; the star unindents first line afterwards
{0em}% spacing to left of chapter title
{0ex}% vertical space before title
{3\baselineskip}% vertical spacing after title; here set to 3 lines
\begin{document}
\tableofcontents
\renewcommand{\chapterauthor}{Alphonse de Lamartine}
\renewcommand{\chaptertranslator}{Ine}
\booltrue{withauthor}
\chapter{New chapter}
Here it is.
%\renewcommand{\chapterauthor}{Heinrich Böll}
%\renewcommand{\chaptertranslator}{Llöb Chirnieh}
%\boolfalse{withauthor}
\chapter{Another chapter}
Here again.


\end{document} 

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

관련 정보