5

enter image description here

I want to add some coloured symbols before page number in my content as shown above, how does it work?

By the help of Steven B. Segletes, my problem solved, here is my tex :

\documentclass[a4paper,oneside,openany,11pt]{ctexbook}

\usepackage[explicit]{titlesec}
\usepackage{titletoc}
\usepackage{xpatch,xcolor,tocloft}
\usepackage{pifont}

\usepackage{geometry}
\geometry{top=2cm,bottom=3cm,left=2cm,right=2cm}
\setlength{\textheight}{660pt}
\setlength{\footskip}{45pt}

% SET STAR DEFINITIONS
\def\blackstar{\textcolor{black}{\Pifont{pzd}{\ding{110}}}}
\def\redstar{\textcolor{red}{\Pifont{pzd}{\ding{110}}}}
\def\orangestar{\textcolor{orange}{\Pifont{pzd}{\ding{110}}}}
\def\yellowstar{\textcolor{yellow}{\Pifont{pzd}{\ding{110}}}}
% MAGIC TO EXTRACT SUBSECTION NUMBER AND SET IT IN \tmp

\renewcommand\thesection{\arabic{section}.}
\renewcommand\thesubsection{\arabic{section}.\arabic{subsection}}

\CTEXsetup[name={,},number={}]{chapter}

\titlecontents{chapter}[0pt]{\addvspace{2pt}\Large\bf\color{black}\filright}{\contentspush{\thecontentslabel}}{}{}
\titlecontents{section}[3em]{}{\contentslabel{2em}}{}{\titlerule*[0.5pc]{$\cdot$}\contentspage}

\makeatletter
\xpatchcmd{\l@subsection}{\ifnum}{\extract#1\relax\ifnum}{}{}
\def\extract#1#2#3\relax{\xdef\tmp{#2}}
\makeatother

% CONFIGURE tocloft PARAMETERS TO USE \csname stars\tmp\endcsname AS PAGE FONT
\cftsetpnumwidth{3em}


\renewcommand\cftsubsecpagefont{\csname symbol\tmp\endcsname\,}
% MACRO \setstars TO DEFINE STARS IN GIVEN SUBSECTION TITLE
\newcommand\setsecstars[2]{\expandafter\gdef\csname symbol#1\endcsname{#2}}
\newcommand\setstars[1]{\protect\setsecstars{\thesubsection}{#1}}

\begin{document}

\renewcommand{\contentsname}{\hspace*{\fill}Contents\hspace*{\fill}}

\begin{center}
\tableofcontents
\end{center}
\clearpage

\chapter{Chapter}
\clearpage

\section{Section}
\clearpage
\subsection{subsectionA\setstars{\orangestar\orangestar\blackstar}}
\clearpage
\subsection{subsectionB\setstars{\yellowstar\yellowstar\blackstar}}
\clearpage
\subsection{subsectionC\setstars{\redstar\blackstar\blackstar}}
\end{document}

But there is a problem of the contents that the dotted line in section stops before reaching the page number. enter image description here So can I somehow get the dots until the section page number starts?

3
  • since the creation of you table of contents is a build-in macro, it won't be super easy to modify it (at least for me). A quick'n'dirty workaround could be to use this trick in your document: \subsection[Title in the ToC \macroThatDefinesTheStars{2}{yellow}]{Title in the text}. But stars will then appear before the dotted line.
    – ebosi
    Jun 3, 2016 at 7:37
  • Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format.
    – Seamus
    Jun 3, 2016 at 9:57
  • Just to be clear, you do not want this symbol on the actual page numbers themselves, only in the contents, is that right? Jun 3, 2016 at 10:32

2 Answers 2

6

You can try something like this. Be careful to reset the command \insertstars to relax as \addcontentsline is also used be figure etc. It is up-to-you to replace the * with the symbols you want.

\documentclass[]{article}

\usepackage[ngerman]{babel}          % deutschsprachig, silbentrennung
\usepackage[utf8]{inputenc}          % Eingabe von Sonderzeichen
\usepackage[T1]{fontenc}          % Schriftkodierung, Silbentrennung für Wörter mit Umlauten
\usepackage{xcolor}
\usepackage{xparse}
\usepackage{etoolbox}


\usepackage{hyperref}

%after hyperref
\patchcmd\addcontentsline{\thepage}{\insertstars\thepage}{}{}

\NewDocumentCommand\threestars{ } {\textcolor{yellow}{***}*\,}
\NewDocumentCommand\twostars  { } {\textcolor{yellow}{**}**\,}
\NewDocumentCommand\redstars  { } {\textcolor{red}{****}\,}

\newcommand\insertstars{}
\newcommand\setstarcommand[1]{\renewcommand\insertstars{#1}}

\makeatletter
\renewcommand\@pnumwidth{8ex}
\makeatother


\begin{document}
\tableofcontents
\listoffigures
\section{A}

\setstarcommand\threestars
\subsection{abc}
\setstarcommand\relax

\begin{figure}
\caption{abc}
\end{figure}

\setstarcommand\twostars
\subsection{abc}
\setstarcommand\relax

\setstarcommand\redstars
\subsection{abc}
\setstarcommand\relax

\end{document}

enter image description here

4

Here I use a tocloft solution. I patch \l@subsection to save the subsection number in \tmp. Then, I use \csname stars\tmp\endcsname as the font to set the subsection page number. But since \tmp is unique to each subsection, so is the "font" macro.

These font macros are defined in each \subsection argument, following the title itself, using the \setstars macro. The stars only show up in the toc page number, not the subsection title itself.

So the trick is to use the tocloft page font macro, \cftsubsecpagefont, unique to each subsection, to set the stars.

\documentclass{article}
\usepackage{xpatch,xcolor,tocloft}
% SET STAR DEFINITIONS
\def\blackstar{\textcolor{black}{$\star$}}
\def\redstar{\textcolor{red}{$\star$}}
\def\orangestar{\textcolor{orange}{$\star$}}
\def\yellowstar{\textcolor{yellow!80!red}{$\star$}}
% MAGIC TO EXTRACT SUBSECTION NUMBER AND SET IT IN \tmp
\makeatletter
\xpatchcmd{\l@subsection}{\ifnum}{\extract#1\relax\ifnum}{}{}
\def\extract#1#2#3\relax{\xdef\tmp{#2}}
\makeatother
% CONFIGURE tocloft PARAMETERS TO USE \csname stars\tmp\endcsname AS PAGE FONT 
\cftsetpnumwidth{2em}
\renewcommand\cftsubsecpagefont{\csname stars\tmp\endcsname\,}
% MACRO \setstars TO DEFINE STARS IN GIVEN SUBSECTION TITLE
\newcommand\setsecstars[2]{\expandafter\gdef\csname stars#1\endcsname{#2}}
\newcommand\setstars[1]{\protect\setsecstars{\thesubsection}{#1}}
\begin{document}
\tableofcontents
\clearpage
\section{section}
\clearpage
\subsection{subsection A\setstars{\orangestar\orangestar\blackstar}}
\clearpage~\clearpage~\clearpage
\subsection{subsection B\setstars{\yellowstar\yellowstar\blackstar}}
\clearpage~\clearpage~\clearpage
\subsection{subsection C\setstars{\redstar\blackstar\blackstar}}
\end{document}

enter image description here

You must log in to answer this question.

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