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} 

在此处输入图片描述 在此处输入图片描述

相关内容