I propose an approach where you declare right before \tableofcontents
which section numbers you want with an asterisk, as a comma separated list. The code uses only the old LaTeX kernel "\in@
" technique, although I used for convenience \expanded
tool which is available since 2019 only (except for lualatex which had it earlier), and one could easily reformat this.
Notice that in this interface \ActivateAsterisksFor
is a one-shot command, each new use erases previous data, one could configure a cumulative one rather if you prefer.
Caveat: do not insert spaces in its argument; if you need to wrap on next line, use %
to not create a space from end of line.
If you redefine \thesection
, for example to use letters in place of digits, you need then to adjust argument of \ActivateAsterisksFor
to be kept in sync.
Edited: initially I had overlooked you wanted also double asterisks for some. So here is original answer and below it edited answer.
method for only one or no asterisk
\documentclass{book}
\newtoks\asteriskedsections
\newcommand\ActivateAsteriskFor{\asteriskedsections}
\let\originalnumberline\numberline
\makeatletter
\renewcommand\numberline[1]{%
\expanded{\noexpand\in@{,#1,}{,\the\asteriskedsections,}}%
\ifin@\originalnumberline{\llap{*}#1}\else\originalnumberline{#1}\fi
}
\makeatother
\begin{document}
\ActivateAsteriskFor{1.2,2.1,2.2,2.4}
\tableofcontents
\chapter{Test}
\section{section without asterisk}
\section{section with asterisk}
\section{section without asterisk}
\section{section without asterisk}
\chapter{Test 2}
\section{section with asterisk}
\section{section with asterisk}
\section{section without asterisk}
\section{section with asterisk}
\end{document}

method for none, one, or two asterisks
\documentclass{book}
\newtoks\asteriskedsections
\newcommand\ActivateAsteriskFor{\asteriskedsections}
\newtoks\doublyasteriskedsections
\newcommand\ActivateTwoAsterisksFor{\doublyasteriskedsections}
\let\originalnumberline\numberline
\makeatletter
\renewcommand\numberline[1]{%
\expanded{\noexpand\in@{,#1,}{,\the\doublyasteriskedsections,}}%
\ifin@\originalnumberline{\llap{**}#1}\else
\expanded{\noexpand\in@{,#1,}{,\the\asteriskedsections,}}%
\ifin@\originalnumberline{\llap{*}#1}\else
\originalnumberline{#1}\fi\fi
}
\makeatother
\begin{document}
\ActivateAsteriskFor{1.2,2.1,2.4}
\ActivateTwoAsterisksFor{2.2}
\tableofcontents
\chapter{Test}
\section{section without asterisk}
\section{section with asterisk}
\section{section without asterisk}
\section{section without asterisk}
\chapter{Test 2}
\section{section with asterisk}
\section{section with asterisk}
\section{section without asterisk}
\section{section with asterisk}
\end{document}
