1

I'm writting some math notes and I want to highlight some sections (analogously chapters, subsections,...) in the ToC (and as well in the regular text).

By "highlight" I mean, put some symbol next to the section title (or near) so the reader can notice (maybe by a footnote) that these parts of the text are, one way or another, special.

Thanks!

  • Look at this answer: tex.stackexchange.com/a/6325/101651, I think it could be useful for you. – CarLaTeX Mar 1 '17 at 17:50
  • This is rather something for tocloft and \cftpresecnum etc. ... for special cases, however – user31729 Mar 1 '17 at 17:57
3

Here is a method that appends a small symbol after the page number to the ToC if the switch \importantstuff has been used. This persists until \unimportantstuff is applied.

\documentclass{book}

\usepackage{tocloft}
\usepackage{graphicx}% For scalebox
\usepackage[tikz]{bclogo} % For 'funny' logos

\newif\ifimportantstuff
\newlength\pnumcontentskip

\setlength{\pnumcontentskip}{1.5em}% control the distance after the page number

\DeclareRobustCommand{\importantstuffsymbol}{\hskip\pnumcontentskip\scalebox{0.4}{\bcattention}}

\newcommand{\afterpnumcontent}{\ifimportantstuff\importantstuffsymbol\else\phantom{\importantstuffsymbol}\fi}

\makeatletter
\g@addto@macro\cftchapafterpnum\afterpnumcontent
\g@addto@macro\cftsecafterpnum\afterpnumcontent
\g@addto@macro\cftsubsecafterpnum\afterpnumcontent
\g@addto@macro\cftsubsubsecafterpnum\afterpnumcontent
\g@addto@macro\cftparaafterpnum\afterpnumcontent
\g@addto@macro\cftsubparaafterpnum\afterpnumcontent
\makeatother


% Switch the usage on 
\newcommand{\importantstuff}{%
  \addtocontents{toc}{\string\global\string\importantstufftrue}%
}

% Switch the usage off
\newcommand{\unimportantstuff}{%
  \addtocontents{toc}{\string\global\string\importantstufffalse}%
}

\begin{document}
\tableofcontents
\chapter{Foo}

\importantstuff
\section{Foo section}

\section{Foobar section}

\subsection{Foobar subsection}


\unimportantstuff

\section{More foobar, but not important}

\importantstuff
\chapter{More important content}

\end{document}

enter image description here

**Update -- with some configurable interface.

Use \RedeclareStructureCommands to enable or \RedeclareStructureCommands* to disable the feature.

The default state of an entry is 'unimportant', the symbols can be changed with tocsymbol and the note is given with tocsymbolnote.

Scale the symbols size with any other value than 0.4 in the \tocsymbol, \inlinesymbol and \emptytocsymbol macros.

\documentclass{book}

\usepackage{tocloft}
\usepackage{graphicx}% For scalebox
\usepackage[tikz]{bclogo} % For 'funny' logos


\usepackage{xparse}

\providecommand{\localtocsymbol}{}
\providecommand{\localtocsymbolcommand}{}


\makeatletter
\ExplSyntaxOn


\seq_new:N \l_alvaro_structurecommands_seq 

\prop_new:N \l_alvaro_highlight_prop

\seq_set_from_clist:Nn \l_alvaro_structurecommands_seq  { part, chapter, section, subsection, subsubsection, paragraph , subparagraph }


\keys_define:nn {alvarohighlight}{%
  tocsymbol .code:n={ \cs_set_nopar:Npn \localtocsymbolcommand {#1}
    \bool_set_false:N\l_alvaro_unimportant_bool
  }
  ,
  tocsymbolnote .code:n= { \prop_put:Nnn \l_alvaro_highlight_prop {note} {#1}},
  unimportant .code:n={ \cs_gset_nopar:Npn \localtocsymbolcommand {}
    \bool_set_true:N\l_alvaro_unimportant_bool
    \prop_remove:Nn \l_alvaro_highlight_prop {note} 
  }
}



\NewDocumentCommand{\SetupHighlighting}{}{%
% For later purposes
}

\NewDocumentCommand{\SetupHighlightingOptions}{+O{}}{%
  \bool_set_true:N \l_alvaro_unimportant_bool
  \keys_set:nn {alvarohighlight} {unimportant,#1}
  \SetupHighlighting
}

\cs_new:Nn \alvaro_pretoc_entry:n {
  \bool_if:NTF \l_alvaro_unimportant_bool { %  
    % Write dummy to toc
    \addtocontents{toc}{\begingroup\string\renewcommand{\string\localtocsymbolcommand}{\bccrayon}\string\renewcommand{\string\localtocsymbol}{\emptytocsymbol\protect\phantom{1}}}}
  {%
    \prop_if_in:NnTF \l_alvaro_highlight_prop {note} {%
      \addtocontents{toc}{\begingroup\string\renewcommand{\string\localtocsymbolcommand}{#1}\string\renewcommand{\string\localtocsymbol}{\tocsymbol\protect\footnote{\prop_item:Nn \l_alvaro_highlight_prop {note}}}}
    }{%
      \addtocontents{toc}{\begingroup\string\renewcommand{\string\localtocsymbolcommand}{#1}\string\renewcommand{\string\localtocsymbol}{\tocsymbol\protect\phantom{1}}}
    }
  }% End of \bool_if
}

\cs_new:Nn \alvaro_posttoc_entry:n {
  \addtocontents{toc}{\endgroup}%
}

\NewDocumentCommand{\RedeclareStructureCommands}{s}{%
  \seq_map_inline:Nn \l_alvaro_structurecommands_seq {%
    \cs_set_eq:cc { latex@@##1 } {##1} 
    \expandafter\RenewDocumentCommand\csname ##1\endcsname{somO{}}{%
      \group_begin:
      \keys_set:nn {alvarohighlight} {unimportant,####4}
      \SetupHighlighting
      \IfBooleanTF{####1}{%
        \use:c{latex@@##1*}{####3}
      }{%
        \IfBooleanTF{#1}{%
          \IfValueTF{####2}{%
            \use:c{latex@@##1}[####2]{####3}
          }{%
            \use:c{latex@@##1}{####3}
          }%
        }{%
          \alvaro_pretoc_entry:n { \localtocsymbolcommand }
          \IfValueTF{####2}{%
            \use:c{latex@@##1}[####2]{\texorpdfstring{####3\inlinesymbol}{####3}}
          }{%
            \use:c{latex@@##1}[####3]{\texorpdfstring{####3\inlinesymbol}{####3}}
          }%
          \alvaro_posttoc_entry:n {}
        }
      }
      \group_end:
    }
  }% End of \seq_map_inline
}




\ExplSyntaxOff


\newif\ifimportantstuff
\newlength\pnumcontentskip

\setlength{\pnumcontentskip}{1.5em}% control the distance after the page number

\newcommand{\afterpnumcontent}{\localtocsymbol}

\makeatletter
\g@addto@macro\cftchapafterpnum\afterpnumcontent
\g@addto@macro\cftsecafterpnum\afterpnumcontent
\g@addto@macro\cftsubsecafterpnum\afterpnumcontent
\g@addto@macro\cftsubsubsecafterpnum\afterpnumcontent
\g@addto@macro\cftparaafterpnum\afterpnumcontent
\g@addto@macro\cftsubparaafterpnum\afterpnumcontent
\makeatother


%%% User code!

% Use the starred version to switch off the symbols and notes
%\RedeclareStructureCommands*

% Use the unstarred version to enable the symbols and notes
\RedeclareStructureCommands


\SetupHighlightingOptions[tocsymbol={\bcbombe},unimportant]

\DeclareRobustCommand{\tocsymbol}{\hskip\pnumcontentskip\scalebox{0.4}{\protect\localtocsymbolcommand}}
\DeclareRobustCommand{\inlinesymbol}{\marginpar{\scalebox{0.4}{\localtocsymbolcommand}}}
\DeclareRobustCommand{\emptytocsymbol}{\hskip\pnumcontentskip\scalebox{0.4}{\phantom\localtocsymbolcommand}}




\usepackage[linktocpage]{hyperref}


\begin{document}
\tableofcontents
\chapter{Foo}[tocsymbol={\bcbombe},tocsymbolnote={Dangerous additions}]

\section{Foo section}[unimportant]

\section{Foobar section}[tocsymbol={\bccrayon}, tocsymbolnote={Edited}]

\subsection{Foobar subsection}
\section{More foobar, but not important}

\chapter{More important content}[tocsymbol={\bcrecyclage},tocsymbolnote={Reworked}]

\section{Added}[tocsymbol={\bctrombone},tocsymbolnote={Added}]

\end{document}

enter image description here

  • Wow! Amazing! I just have one little problem with the answer, and is that there is that no symbol is displayed in the regular text (only in the ToC). In addition (not really important), the solution doesn't provides a way to insert a footnote (or similar) in the ToC so the reader cand understand the meaning of the symbol. – Álvaro G. Tenorio Mar 1 '17 at 21:14
  • @ÁlvaroG.Tenorio: I'll try to update – user31729 Mar 1 '17 at 21:31
  • @ÁlvaroG.Tenorio: See a possible update ... not perfect, but better than before – user31729 Mar 2 '17 at 10:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.