我正在准备一本书,其中将列出我使用该titlesec
软件包解决的常见计算机科学问题的解决方案。
\documentclass[openany,14pt,oneside]{book}
\usepackage[bf,sf]{titlesec} % I've got those bf, sf from titlesec package's author
% Don't know what they mean but they must be sthg good
\titleformat{\chapter}[display]
{\Huge} % chapter font size? Seems so.
{\filright\Large\thechapter} % \MakeUppercase{\chaptertitlename}
{1ex}
{\titlerule\vspace{1ex}\filright}
[\vspace{1ex}] % \titlerule ?
\begin{document}
\chapter{Solution to the Traveling Salesman Problem}[22.August.2019]
We'll try intuitive solution
\chapter{Solution to the Josephus problem}[17.March.2020]
We'll try the direct solution
\end{document}
每个解决方案都有一个日期。我想将日期显示为与主标题分开的单独文本,并将其定位在章节号行,如图所示右对齐。
为了做到这一点,我想我需要添加一个额外的参数,\titleformat
但当然也可能有其他方法。怎么做?
笔记:我使用的是 Mac mini 2007,上面装有 macOS Lion (10.7.5)。Lion 10.7.5 是 Mac mini 2007 支持的最高操作系统,而 TexLive 2020 是支持 Lion 10.7.5 的最高版本,所以我无法使用 TeXLive 2022。因此,我无法使用 TexLive 2022\NewDocumentCommand
支持的那些新功能。
答案1
您需要重新定义\chapter
。
\documentclass[openany,12pt,oneside]{book}
\usepackage{titlesec}
\usepackage{kantlipsum} % for mock text
\titleformat{\chapter}[display]
{\filright\Huge} % chapter font size? Seems so.
{{\Large\thechapter}\hfill{\normalsize\chapterdate}}
{1ex}
{\titlerule\vspace{1ex}\filright}
\NewDocumentCommand{\extendedchapter}{sO{#3}mo}{%
\IfValueTF{#4}{\renewcommand{\chapterdate}{#4}}{\renewcommand{\chapterdate}{}}%
\IfBooleanTF{#1}{%
\latexchapter*{#3}%
}{%
\latexchapter[#2]{#3}%
}%
}
\newcommand{\chapterdate}{}
\AtBeginDocument{%
\NewCommandCopy{\latexchapter}{\chapter}%
\RenewCommandCopy{\chapter}{\extendedchapter}%
}
\begin{document}
\chapter{Solution to the Traveling Salesman Problem}[22.August.2019]
We'll try intuitive solution
\kant
\chapter{Solution to the Josephus problem}[17.March.2020]
We'll try the direct solution
\kant
\end{document}
titlesec
重新定义\chapter
为具有标准语法,因此需要采用不同的方法来添加尾随可选参数。因此我定义\extendedchapter
以支持它,并在开始文档时,我保存\chapter
为\latexchapter
并重新定义\chapter
为\extendedchapter
。
与 TeX Live 2020 兼容的版本(已测试)
\documentclass[openany,12pt,oneside]{book}
\usepackage{titlesec}
\usepackage{xparse,letltxmacro}
\usepackage{kantlipsum} % for mock text
\titleformat{\chapter}[display]
{\filright\Huge} % chapter font size? Seems so.
{{\Large\thechapter}\hfill{\normalsize\chapterdate}}
{1ex}
{\titlerule\vspace{1ex}\filright}
\NewDocumentCommand{\extendedchapter}{somo}{%
\IfValueTF{#4}{\renewcommand{\chapterdate}{#4}}{\renewcommand{\chapterdate}{}}%
\IfBooleanTF{#1}{%
\latexchapter*{#3}%
}{%
\IfNoValueTF{#2}{\latexchapter{#3}}{\latexchapter[#2]{#3}}%
}%
}
\newcommand{\chapterdate}{}
\AtBeginDocument{%
\LetLtxMacro{\latexchapter}{\chapter}%
\LetLtxMacro{\chapter}{\extendedchapter}%
}
\begin{document}
\chapter{Solution to the Traveling Salesman Problem}[22.August.2019]
We'll try intuitive solution
\kant
\chapter{Solution to the Josephus problem}[17.March.2020]
We'll try the direct solution
\kant
\end{document}