titlesec:如何访问 ToC 输入的 \chapter 宏的可选参数?

titlesec:如何访问 ToC 输入的 \chapter 宏的可选参数?

titlesec对于不同的处理,我需要使用/包为目录创建两个独立的主文件titletoc(我已经使用 .ptc 文件来存储部分目录)。

为了在这两个文件中写入章节的 ToC-titles 条目,可以简单地\ttl@addcontentslinetitlesec包中重新定义宏(参见 MWE),但这对章节不起作用。

根据 egreg 的建议(此处某处),我不使用显式选项,而是单独定义布局宏。因此,我能够为章节添加目录行,但这只能通过强制参数来完成。

为了进一步格式化,我需要访问 的可选参数\chapter[<ToC-title>]{<Title>}。包的内部机制titlesec对我来说太晦涩了(对内部机制知之甚少的新手),我失败了(这里是否必须考虑\ttl@top@i\ttl@top@ii和/或宏?)。\ttl@select

此外(可能是另一个问题?),我知道可以使用titlesec为正文、标题和目录设置单独的章节标题,以便分行),比如有不同的标题(下面的注释代码)。这是否也可以为章节实现,以便有类似\chapter[<ToC-title>]{<Title>}[<Header-title>]?(参见如何在 \section 命令后设置右标记?

平均能量损失

\documentclass{book}
\usepackage[margin=2cm,a4paper,showframe]{geometry}
\usepackage{titlesec,titletoc}
%\usepackage{lipsum}
\usepackage{xparse}

\makeatletter
\def\ttl@addcontentsline#1#2{% From titlesec: Works for sections but not for chapters
    \addcontentsline{toc}{#1}{\ifttl@toclabel\ttl@a\fi#2}%
    \addcontentsline{tdm}{#1}{\ifttl@toclabel\ttl@a\fi#2}%
    \nobreak}

\newcommand\secondtableofcontents{% From book.cls
    \if@twocolumn
        \@restonecoltrue\onecolumn
    \else
        \@restonecolfalse
    \fi
    \chapter*{\contentsname \ two
        \@mkboth{\MakeUppercase\contentsname}{\MakeUppercase\contentsname}}%
    \@starttoc{tdm}% TDM stands for "Table des matières" in French
    \if@restonecol\twocolumn\fi
    }

%\RenewDocumentCommand{\ttl@straight@i}{m R[]{} o m}{%
%  \def\@currentlabelname{#2}% for nameref
%  \gdef\ttl@savemark{\csname#1mark\endcsname{#4}}%
%  \let\ttl@savewrite\@empty
%  \IfNoValueTF{#3}
%    {\def\ttl@savetitle{#4}}% Optional #3 argument NOT supplied
%    {\def\ttl@savetitle{#3}}% Optional #3 argument supplied
%  \gdef\thetitle{\csname the#1\endcsname}%
%  \if@noskipsec \leavevmode \fi
%  \par
%  \ttl@labelling{#1}{#2}%
%  \ttl@startargs\ttl@straight@ii{#1}{#4}}
\makeatother

% Layout commands
\newcommand{\numberedChapterLayout}[1]{% Original complex code
    \Huge\bfseries #1
    \addcontentsline{tdm}{chapter}{\protect\numberline{\thechapter}\protect#1}
}
\newcommand{\numberlessChapterLayout}[1]{% Original complex code
    \Huge\bfseries #1}
\newcommand{\sectionLayout}[1]{\Large\bfseries #1}

\titleformat{\chapter}[display]{}{}{0pt}{\numberedChapterLayout}[]
\titleformat{name=\chapter,numberless}[display]{}{}{0pt}{\numberlessChapterLayout}[]
\titleformat{\section}
  [hang]{}{\thesection}{16pt}{\sectionLayout}% #1 implicit
  []% after the title body

\begin{document}

\tableofcontents
\secondtableofcontents

\chapter[First chapter title]{Title of the first chapter}
\section[Section A]{Section A, first chapter}
\section[Section B]{Section B, first chapter}

\chapter[Second chapter title]{Title of the second chapter}
\section[Section A]{Section A, second chapter}
\section[Section B]{Section B, second chapter}

\end{document}

答案1

与其重新定义,不如\ttl@addcontentsline重新定义原来的\addcontentsline

\let\oldaddcontentsline\addcontentsline
\renewcommand{\addcontentsline}[3]{%
\oldaddcontentsline{#1}{#2}{#3}%
\def\tmp{toc}\def\ttmp{#1}%
\ifx\tmp\ttmp
\oldaddcontentsline{tdm}{#2}{#3}%
\fi}

这样它就适用于章节和节。

2)可以修补\@chapter命令以访问

\usepackage{etoolbox}
\patchcmd\@chapter{\chaptermark{#1}}{\chaptermark{#1}\def\@currentlabelname{#1}}{}{}

\documentclass{book}
\usepackage[margin=2cm,a4paper,showframe]{geometry}
\usepackage{titlesec,titletoc}
%\usepackage{lipsum}
\usepackage{xparse}

\let\oldaddcontentsline\addcontentsline
\renewcommand{\addcontentsline}[3]{%
\def\tmp{toc}\def\ttmp{#1}%
\oldaddcontentsline{#1}{#2}{#3}%
\ifx\tmp\ttmp
\oldaddcontentsline{tdm}{#2}{#3}%
\fi}

\makeatletter
\newcommand\secondtableofcontents{% From book.cls
    \if@twocolumn
        \@restonecoltrue\onecolumn
    \else
        \@restonecolfalse
    \fi
    \chapter*{\contentsname \ two
        \@mkboth{\MakeUppercase\contentsname}{\MakeUppercase\contentsname}}%
    \@starttoc{tdm}% TDM stands for "Table des matières" in French
    \if@restonecol\twocolumn\fi
    }
\makeatother

% Layout commands
\newcommand{\numberedChapterLayout}[1]{% Original complex code
    \Huge\bfseries #1
    %\addcontentsline{tdm}{chapter}{\protect\numberline{\thechapter}\protect#1}
}
\newcommand{\numberlessChapterLayout}[1]{% Original complex code
    \Huge\bfseries #1}
\newcommand{\sectionLayout}[1]{\Large\bfseries #1}

\titleformat{\chapter}[display]{}{}{0pt}{\numberedChapterLayout}[]
\titleformat{name=\chapter,numberless}[display]{}{}{0pt}{\numberlessChapterLayout}[]
\titleformat{\section}
  [hang]{}{\thesection}{16pt}{\sectionLayout}% #1 implicit
  []% after the title body

\begin{document}
\tableofcontents
\secondtableofcontents

\chapter[First chapter title]{Title of the first chapter}
\section[Section A]{Section A, first chapter}
\section[Section B]{Section B, first chapter}

\chapter[Second chapter title]{Title of the second chapter}
\section[Section A]{Section A, second chapter}
\section[Section B]{Section B, second chapter}

\end{document}

相关内容