Titlesec, 4.3. Variants, p.12 给出了以下代码:
\newcommand{\secmark}{}
\newenvironment{advanced}
{\renewcommand{\secmark}{*}}
{}
\titleformat{\section}
{..}
{\thesection\secmark\quad}{..}{..}
要标记部分写入
\begin{advanced}
\section{...}
...
\end{advanced}
但我不知道这与现有的 titlesec 代码如何匹配。下面是一个 MWE,其中注释掉了序言中似乎阻止编译的三行:
\documentclass[11pt]{book}
\usepackage{titlesec}
\titleformat{\subsection}%formatting subsections
[runin]%
{\normalfont\bfseries}%
{\hspace{10mm}\arabic{subsection}}%format title number
{10mm}%Space between title number and title text
{}%???
{}%???
\newcommand{\secmark}{}
\newenvironment{advanced}
{\renewcommand{\secmark}{*}}
{}
%%%==============Begin code I think prevents compilation
% \titleformat{\section}
% {..}
% {\thesection\secmark\quad}{..}{..}
%%%==============End code I think prevents compilation
\begin{document}
\chapter{How to deal with Titlesec}
\section{Usual Stuff}
That's \emph{relevant} stuff I had forgotten and which I had got by trial and error.
\subsection{Formatting subsections.} This is the code which I used according to \textbf{3. Advanced Interface} (p. 3):
\textbackslash titleformat\{\textbackslash subsection\}\hspace{20mm}\% formatting subsection titles
[runin]\hspace{10mm}\%Forces the content to follow immediately upon the title.
\{\textbackslash normalfont\textbackslash bfseries\}%
\{\textbackslash hspace\{10mm\}/arabic\{subsection\}\}\hspace{5mm}\%Formats the title number
\{10mm\}\hspace{55.5mm}\%Space title number / text
\{\}\hspace{30mm}\%???
\{\}\hspace{30mm}\%???
\begin{advanced}
\section{Advanced Stuff}
I want to star \emph{this} section according to \textbf{4.3. Variants} (p. 12): "Let’s suppose we want to mark some sections as “advanced topics” with an asterisk after the label. The following code does the job:"
Since I can't make it work, I am commenting the part in the Preamble that prevents compilation.
\end{advanced}
\end{document}
答案1
在此解决方案中,当您想要将其加星标时,请\section
在其前面加上前缀。\advanced
此前缀设置\ifadvanced
为 true,因此当排版章节编号时,其后将跟有星号。无论如何,在排版章节标题之前,条件设置为 false。
\documentclass[11pt]{book}
\usepackage{titlesec}
\titleformat{\section}[block]
{\normalfont\bfseries}
{\thesection\advancedmark}
{1em}
{\global\advancedfalse}
\newif\ifadvanced
\newcommand{\advancedmark}{\ifadvanced\makebox[0pt][l]{*}\fi}
\newcommand{\advanced}{\global\advancedtrue}
\begin{document}
\chapter{How to deal with Titlesec}
\section{Usual Stuff}
That's \emph{relevant} stuff I had forgotten and which I had got by trial and error.
\advanced\section{Unusual Stuff}
This is mainly for demonstration purposes.
\section{Normal Stuff}
The missing asterisk means this is a normal section.
\end{document}
如果您希望星号也出现在交叉引用和目录中,您可以使用高级部分周围的环境,但这需要一点来回的反复。
\documentclass[11pt]{book}
\usepackage{titlesec,etoolbox}
\newenvironment{advanced}
{\appto\thesection{\addstar}}
{}
\newrobustcmd{\addstarintitle}{\makebox[0pt][l]{*}}
\newrobustcmd{\addstar}{*}
\titleformat{\section}[block]
{\normalfont\bfseries\let\addstar\addstarintitle}
{\thesection}
{1em}
{}
\begin{document}
\frontmatter
\begingroup
\let\addstar\addstarintitle
\tableofcontents
\endgroup
\mainmatter
\chapter{How to deal with Titlesec}
\section{Usual Stuff}
That's \emph{relevant} stuff I had forgotten and which I had got by trial and error.
\begin{advanced}
\section{Unusual Stuff}\label{unusual}
This is mainly for demonstration purposes. The reference~\ref{unusual} is correct.
\end{advanced}
\section{Normal Stuff}
The missing asterisk means this is a normal section.
\end{document}