如何在页码前添加一些符号?

如何在页码前添加一些符号?

在此处输入图片描述

我想在内容中的页码前添加一些彩色符号,如上所示,该怎么做?

在 Steven B. Segletes 的帮助下,我的问题解决了,这是我的文本:

\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}

但存在内容部分虚线在到达页码前就停止的问题。 在此处输入图片描述 那么我能以某种方式获取直到章节页码开始为止的点吗?

答案1

您可以尝试类似这样的操作。小心重置命令\insertstars以放松,因为\addcontentsline图形等也使用它。您可以自行决定将 * 替换为您想要的符号。

\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}

在此处输入图片描述

答案2

这里我使用了一个tocloft解决方案。我把\l@subsection小节编号保存在 中\tmp。然后,我使用\csname stars\tmp\endcsname作为字体来设置小节页码。但由于\tmp每个小节都是唯一的,因此“字体”宏也是唯一的。

这些字体宏在每个参数中定义\subsection,位于标题本身之后,使用\setstars宏。星号仅显示在目录页码中,而不显示在子部分标题本身中。

因此,诀窍是使用每个小节所独有的tocloft页面字体宏\cftsubsecpagefont来设置星星。

\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}

在此处输入图片描述

相关内容