2

I want to add an asterisk to some section labels. This is often used to indicate that a section is optional, like so:

1.1* Optional Section

Here is my solution :

\documentclass{report}
\usepackage{amsmath}
\begin{document}

\renewcommand*\thesection{\arabic{chapter}.\arabic{section}*}
\section{Optional Section}
\renewcommand*\thesection{\arabic{chapter}.\arabic{section}}
\section{Required Section}

\end{document}

Will this solution cause any problems? Is there a better way to do it?

For my document, thesection is only used in section headings, so it will not cause problems with theorem numbering, etc. However, others may want to know how to fix that.

epR8GaYuh
  • 2,432
  • Hope this link https://tex.stackexchange.com/questions/450666/a-modified-section-command may helps you... – MadyYuvi Oct 18 '19 at 07:31
  • 1
    You might be interested in the titlesec solution for ‘variants’: see §4.3 Variants, pp.12-13 of the documentation. – Bernard Oct 18 '19 at 09:33

3 Answers3

3

As Bernard mentioned in the comments, the solution is found in Section 4.3 of the titlesec documentation. Here is an example of the solution :

\documentclass{report}
\usepackage{amsmath}
\usepackage{titlesec}

\newcommand{\secmark}{}
\newenvironment{advanced}
  {\renewcommand{\secmark}{*}}
  {}

\titleformat{\section}
  {\large\bfseries} % Format of the title
  {\thesection\secmark} % Label
  {1em} % Separation between label and title body 
        % (default = horizontal space, display = vertical space)
  {} % Code preceding the title

\begin{document}

\begin{advanced}
\section{Optional Section}
Content.    
\end{advanced}

\section{Required Section}
Content. 

\end{document}
1

For people using KOMA-classes, one can do something like

\renewcommand{\sectionformat}{%
    \thesection%
    \texorpdfstring{\rlap{\ensuremath{^{\bstar}}}}{}%
    \autodot\enskip%
}%
  • Why the need for \texorpdfstring? Also, since this answer aims to be specific about some class, the question asks about the section number formatting, not subsection. You can definitely explain some code usage here. – Werner Dec 22 '22 at 17:07
1

This should work with all classes. The idea is to call

\advanced\section

for the special sectional units (any level, so long as it's numbered).

How is it done? When \advanced\section is followed, the meaning of \thesection is saved and modified by adding \advancedmarker at the end. After the title is typeset, the meaning of \thesection is restored.

THe \advancedmarker command prints a zero width asterisk when printing the sectional title and in the table of contents, but a normal asterisk in all other situations.

Bonus feature: if one doesn't want the asterisk when \ref is used, it's sufficient to change the definition of \advancedmarker to

\NewDocumentCommand{\advancedmarker}{}
 {
  \bool_if:NT \__advanced_killwidth_bool { \makebox[0pt][l]{*} }
 }

and \ref{test} will not print any asterisk.

The complete code.

\documentclass{report}

\ExplSyntaxOn \NewDocumentCommand{\advanced}{mO{#3}m} {% #1 is a sectioning command % save the current meaning of \the<level> \cs_set_eq:Nc __advanced_save: { the \cs_to_str:N #1 } % add \advancemarker \cs_set:cpn { the \cs_to_str:N #1 } { __advanced_save: \advancedmarker } \bool_set_true:N __advanced_killwidth_bool #1[#2]{#3} \bool_set_false:N __advanced_killwidth_bool % reset \the<level> to the previous meaning \cs_set_eq:cN { the \cs_to_str:N #1 } __advanced_save: } \NewDocumentCommand{\advancedmarker}{} { \bool_if:NTF __advanced_killwidth_bool { \makebox[0pt][l]{} } { } }

\bool_new:N __advanced_killwidth_bool

\AddToHook{cmd/@starttoc/begin}{\bool_set_true:N __advanced_killwidth_bool} \ExplSyntaxOff

\begin{document}

\tableofcontents

\chapter{Test}

Beware that \ref{test} is a difficult section.

\section{This is a normal section}

\advanced\section{This is an optional advanced section}\label{test}

\section{This is a normal section}

\subsection{With a normal subsection}

\advanced\subsection{And an advanced subsection}

\subsection{And a normal subsection}

\advanced\chapter{A difficult chapter}

\end{document}

The TOC

enter image description here

The first chapter (standard)

enter image description here

The second chapter (advanced)

enter image description here

egreg
  • 1,121,712