Titlesec 在 ToC 中顯示

Titlesec 在 ToC 中顯示

我用來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}

問題是,這將目錄視為一章,由於目錄沒有作者或翻譯者,因此它在目錄上方顯示了一個空的作者和翻譯者,如下所示:

    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} 

在此輸入影像描述 在此輸入影像描述

相關內容