[Tex/LaTex] How to put a star (or other symbol) in sections that are “special” (difficult, optional, etc)

decorationsenvironmentsexercisessectioningtheorems

I'm currently writing a book, and I'd like some of the sections to be clearly marked as "difficult" or "optional". Not only sections, but also exercises (I'm using the exercise package) and theorems (that I am defining with newtheorem from amsthm)). Something that would work for arbitrary sectioning levels, environments, etc would be great…

edit: I think I should have also said — I'd like something like a star at the margin, close to the section/theorem/etc, and, if it's a section or chapter, then there should be a star before it in the text and at the table of contents:

1 Introduction
    1.1 blah
    2.3 blah
2 Some Chapter
    2.1 blah
  * 2.2 this is an optional section 
    2.3 blah blah

edit: it would be nice if the star (or a configurable symbol of course) was placed before the section number.

Best Answer

The titletoc manual introduces an interesting method to do what you are after. It's illustrated best with an example.

\documentclass[11pt,a4paper,english]{book}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage[pagestyles,raggedright]{titlesec}
\usepackage{titletoc}
\usepackage{blindtext}

\title{A document with stuff for advanced or optional reading}
\author{Jay}

\newcommand{\secmark}{}
\newcommand{\marktotoc}[1]{\renewcommand{\secmark}{#1}}
\newenvironment{advanced}{
  \renewcommand{\secmark}{*}%
  \addtocontents{toc}{\protect\marktotoc{*}}
}
{\addtocontents{toc}{\protect\marktotoc{}}}

\titleformat{\section}{\normalfont\Large}{\makebox[1.5em][l]{\llap{\secmark}\thesection}}{0.4em}{}
\titlecontents{section}[3.7em]{}{\contentslabel[\llap{\secmark}\thecontentslabel]{2.3em}}{\hspace*{-2.3em}}{\titlerule*{}\bfseries\contentspage}

\newpagestyle{front}{%
  \headrule
  \sethead[\thepage][][\subsectiontitle]{\sectiontitle}{}{\thepage}
  \setfoot[][][]{}{}{}
}

\newpagestyle{main}{%
  \headrule
  \sethead[\thepage][][\thesubsection\quad\subsectiontitle]{\llap{\secmark}\thesection\quad\sectiontitle}{}{\thepage}
  \setfoot[][][]{}{}{}
}
\newpagestyle{back}{%
  \headrule
  \sethead[\thepage][][\subsectiontitle]{\sectiontitle}{}{\thepage}
  \setfoot[][][]{}{}{}
}


\begin{document}
  \frontmatter
  \pagestyle{front}
  \maketitle
  \tableofcontents\newpage

  \mainmatter
  \pagestyle{main}
  \blinddocument
  \begin{advanced}
    \blinddocument
  \end{advanced}
  \blinddocument

  \backmatter
  \appendix
  \pagestyle{back}
\end{document}

This example starts with marking selected sections within a chapter. For marked chapters it needs to be modified accordingly.

As always, the blindtext package is only for creating dummy text, thus is not part of the solution.