0

There is this question Add an asterisk in front of section label in TOC asking to find a way to put an asterisk (*) in front of the title of a specific section in the table of contents.

The goal is to have something like this enter image description here

For example 4.58 is not a hard problem, 4.60 is a hard problem and 4.65 is a very hard problem.

Alann Munn replied with this solution https://tex.stackexchange.com/a/16341/270421 but clarified that the asterisk also appears in the title of the section in the document (that would not be a real issue actually) and aslo in the marks in the headers:

enter image description here

Personally, I find it unattractive and want a solution to delete it.

So far, I tried to create a copy of the section environment by tweaking things like in Where can I find help files or documentation for commands like \@startsection for LaTeX? and by redefining a copy of the definition of the \part environment.

3 Answers 3

1

Try this

\documentclass{book}

\newcommand\maybeast{}% check definable

\let\maybeast\relax

\renewcommand\thesection{\maybeast\thechapter.\arabic{section}}

\newcommand\nextonewithast{%
   \addtocontents{toc}
   {\def\maybeast{\global\let\maybeast\relax\string\llap{*}}}%
}

\newcommand\nextonewithtwoasts{%
   \addtocontents{toc}
   {\def\maybeast{\global\let\maybeast\relax\string\llap{**}}}%
}


\begin{document}
\tableofcontents

\chapter{Test}
\section{section without asterisk}

\nextonewithast
\section{section with asterisk}
\section{section without asterisk}
\section{section without asterisk}

\chapter{Test 2}

\nextonewithast
\section{section with asterisk}

\nextonewithtwoasts
\section{section with two asterisks}
\section{section without asterisk}

\nextonewithast
\section{section with asterisk}

\end{document}

table of contents with asterisks

1
  • advantage of such an approach is that it patches no class level commands such as \numberline or even \section.
    – user691586
    Jun 17 at 16:22
0

You can try the following code, which does not effect the section numbers in the section headings or the page headers.

\documentclass{book}

\usepackage{xpatch}
\xpatchcmd{\numberline}{#1}{\tocasts#1}{}{\PatchFailure}
\newcommand*{\tocasts}{}
\newcommand*{\nexttocasts}[1][1]{%
  \addtocontents{toc}{\activatetocasts{#1}}%
}
\makeatletter
\DeclareRobustCommand*{\activatetocasts}[1]{%
  \renewcommand*{\tocasts}{%
    \global\let\tocasts\@empty
    \makebox[0pt][r]{\@tempcnta=#1\relax\@whilenum\@tempcnta>\z@\do{%
        *\advance\@tempcnta\m@ne
      }}%
  }%
}
\makeatother

\begin{document}
\tableofcontents
\chapter{Test}
\section{One section without asterisk}
\nexttocasts
\newpage
Empty page
\newpage
As you can see here there is not problem with the page header.
\section{One section with one asterisk}
\nexttocasts[2]
\section{One section with two asterisks}
\section{Another section without asterisk}
\newpage

\end{document}

table of contents

section heading and page header

Note: The code could fail, e.g., if you are using a package or class, that changes the definition of \numberline. If so, please ask a new question with a proper minimal working example.

0

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}

table of contents with asteriks

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}

table of contents with one or two asterisks

4
  • inconvenient of this approach is that if one inserts a section one has to update the list of asterisked one ; I can propose a quite different approach where the trigger is made part of \thesection in a way which is active only in TOC, and then eithe use special mark-up \asteriskedsection in document or precede \section with \nextsectionwithasterisk which will influence what ends up in TOC (in a way not affecting headers).
    – user691586
    Jun 16 at 16:21
  • I salute your efforts. You can propose your different approach, I'm interested.
    – Flowt
    Jun 16 at 17:29
  • added another answer
    – user691586
    Jun 16 at 17:47
  • the answer here is more appropriate for an existing document which will not evolve as you don't have to do any change to document body apart from adding the list of sections to be decorated with asterisks (one or two) right before \tableofcontents and, naturally the extra code in preamble ; my other answer is more suited to new document where you add a bit of mark-up, either now or later, to dynamically decide if a section inherits or not an asterisk. Any change in mark-up requires two compilations in order for TOC in pdf to get its update.
    – user691586
    Jun 16 at 17:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .