语境
例如,对于书籍类,奇数页标题默认包含目录或文本当前章节标题,具体取决于是否\section
使用可选参数。
这有时不够高效:假设文本部分标题很长,但如果缩写,意义就会大大减弱。在这种情况下,应避免在目录中使用缩写标题,而应仅使用文本标题。但因此,在奇数页标题中,此文本标题可能太长,因此被截断。
为了避免这个缺陷,我尝试\section
用两个可选参数重新定义命令,第二个参数用于可能与目录和文本不同的标题,具体如下:
|----------------------------|
| Text | ToC | Header |
|-----------------------------------+--------+--------+----------|
| \section{⟨text⟩} | ⟨text⟩ |
|----------------------------------------------------------------|
| \section[⟨ToC⟩]{⟨text⟩} | ⟨text⟩ | ⟨ToC⟩ |
|----------------------------------------------------------------|
| \section[][⟨header⟩]{⟨text⟩} | ⟨text⟩ | ⟨header⟩ |
|----------------------------------------------------------------|
| \section[⟨ToC⟩][⟨header⟩]{⟨text⟩} | ⟨text⟩ | ⟨ToC⟩ | ⟨header⟩ |
|----------------------------------------------------------------|
尝试解决方案
在我下面的尝试中:
- ⟨text⟩ 和 ⟨ToC⟩ 标题(得益于
titleps
's 的 显示\sectiontitle
)是在正确的时间显示(不在相应命令插入的页面之前\section
),如 PDF 生成的文件的第 5 页和第 11 页所示:参见下面的前两张图片, - ⟨header⟩ 标题是显示过早(在插入相应命令的页面之前的页面上
\section
),如 PDF 生成的文件的第 17 页和第 23 页所示:请参见下面的最后两张图片。
问题
您是否发现我的尝试有什么错误,以及如何使 ⟨header⟩ 标题在正确的时间显示?
(好吧,我可以使用memoir
提供此功能的类,但无论如何......)
\documentclass{book}
\usepackage{xparse}
\usepackage[pagestyles]{titlesec}
\usepackage[papersize=10cm]{geometry}
\usepackage{lipsum}
%
\ExplSyntaxOn
%
% Creation of the token lists
\tl_new:N \g__db_section_title_tl
\tl_new:N \g__db_toc_section_title_tl
\tl_new:N \g__db_header_section_title_tl
\tl_new:N \g__db_even_header_tl
\tl_new:N \g__db_odd_header_tl
%
% Copy the original section command
\cs_set_eq:NN \__db_original_section \section
%
% Redefinition of the section command (without star in order to keep it simple):
% - 1st optional argument: section title in the ToC
% - 2nd optional argument: section title in the header
% - mandatory argument: section title in the text
\RenewDocumentCommand \section { o o m } {%
%
% Clear the ToC and header titles possibly previously defined
\tl_gclear:N \g__db_toc_section_title_tl
\tl_gclear:N \g__db_header_section_title_tl
%
% If a ToC section title is specified, store it in \g__db_toc_section_title_tl
\IfNoValueF{#1}{
\tl_gset:Nn \g__db_toc_section_title_tl {#1}
}
%
% The text section title is store in \g__db_header_section_title_tl
\tl_gset:Nn \g__db_section_title_tl {#3}
%
% If no ToC section title is specified, make it the same as the text one
\tl_if_empty:NT \g__db_toc_section_title_tl {%
\tl_gset_eq:NN \g__db_toc_section_title_tl \g__db_section_title_tl
}
%
% Use the original section command with the previous text and ToC titles
\__db_original_section[\g__db_toc_section_title_tl]{\g__db_section_title_tl}
%
% If a header section title is specified, store it in \g__db_header_section_title_tl
% that will be used in the header of the page
\IfNoValueF {#2}{ \tl_gset:Nn \g__db_header_section_title_tl {#2} } }
%
% The header of a even pages contains the current chapter' title (with its
% number if any)
\tl_gset:Nn \g__db_even_header_tl {
\ifthechapter{%
\thechapter.~%
}{%
}
\scshape\chaptertitle
}
%
% The header of a odd pages contains the current section' title (with its
% number if any) which is:
% - the \sectiontitle macro if the header section title isn't specified
% - the header section title if it is specified
\tl_gset:Nn \g__db_odd_header_tl {
\ifthesection{%
\thesection.~%
}{%
}
\tl_if_empty:NTF \g__db_header_section_title_tl {%
\sectiontitle
}{
\g__db_header_section_title_tl
}
}%
%
% Create a custom pagestyle with the previous headers
\newpagestyle{_db_pagestyle}[]{%
\sethead[\g__db_even_header_tl][][]{\g__db_odd_header_tl}{}{}%
\setfoot{}{\thepage}{}%
\headrule%
}
\ExplSyntaxOff
%
% Use the custom previous pagestyle
\pagestyle{_db_pagestyle}
%
% Create a new command for the test
\newcommand{\test}[1]{%
\chapter{Test}
\lipsum[1-2]
#1
\lipsum[1]
}
%
\begin{document}
\tableofcontents
\test{\section {Section Title 1}}
\test{\section[Toc Title 2] {Section Title 2}}
\test{\section[] [Header Title 3]{Section Title 3}}
\test{\section[Toc Title 4][Header Title 4]{Section Title 4}}
\end{document}