在文本中显示所有级别的章节编号,但不在目录中显示?

在文本中显示所有级别的章节编号,但不在目录中显示?

我希望目录中的章节编号显示为

\renewcommand\thesection{\Roman{section}}
\renewcommand\thesubsection{\alph{subsection}}
\renewcommand\thesubsubsection{\arabic{subsubsection}}

但在文本中应该看到完整的编号:

\renewcommand\thesection{\Roman{section}}
\renewcommand\thesubsection{\Roman{section}.\alph{subsection}}
\renewcommand\thesubsubsection{\Roman{section}.\alph{subsection}.\arabic{subsubsection}}

使用 titlesec/titletoc 可以实现吗?

答案1

将目录中不需要的部分括在宏中,该宏通常仅定义为打印其内容,但是在文件中.aux为目录条目做注释时,它会被重新定义为吞噬其内容。

负责注释的宏\addtocontents已经做了类似的事情,通过重新定义\label\index吞噬\glossary它们的参数;所以我们只需要在列表中添加另一个宏。

\documentclass{article}

\usepackage{etoolbox}
\usepackage{xcolor}
\definecolor{Blue}{HTML}{2E3192}

% heading styles:
\usepackage{titlesec}
\titleformat{\section}
  {\color{teal}\large\sffamily}
  {\thetitle.\hspace{0.5cm}}
  {0cm}
  {}

\titleformat{\subsection}
  {\color{violet}\bfseries}
  {\hspace{0.75cm}\thetitle.\hspace{0.3cm}}
  {0cm}
  {}

\titleformat{\subsubsection}
  {\color{olive}\itshape}
  {\hspace{1.3cm}\thetitle.\hspace{0.3cm}}
  {0cm}
  {}

% ToC styles:
\usepackage[dotinlabels]{titletoc}
\contentsmargin{2.55em} % space for a page number

\titlecontents{section}
    [3.8em] % 1.5em + 2.3em
    {\vspace{0.3cm}\sffamily\bfseries\large}
    {\contentslabel{2.3em}}
    {\hspace*{-2.3em}}
    {\normalsize\normalfont\rmfamily\titlerule*[1pc]{.}\contentspage}
    [\vspace{0.1cm}]

\titlecontents{subsection}
    [5.2em] % 3.8em + 1.3em
    {\vspace{0.1cm}}
    {\contentslabel{1.4em}}
    {\hspace*{-1.4em}}
    {\titlerule*[1pc]{.}\contentspage}
    []

\titlecontents{subsubsection}
    [6.5em] % 5.2em + 1.3em
    {\vspace{0.1cm}}
    {\contentslabel{1.3em}}
    {\hspace*{-1.3em}}
    {\titlerule*[1pc]{.}\contentspage}
    []

\makeatletter
\let\perhapsprint\@firstofone
\patchcmd{\addtocontents}
  {\glossary\@gobble}
  {\glossary\@gobble\let\perhapsprint\@gobble}
  {}{}
%\newcommand{\perhapsprint}[1]{\ifprintprefix#1\fi}
%\newif\ifprintprefix
\renewcommand\thesection{\Roman{section}}
\renewcommand\thesubsection{%
  \perhapsprint{\Roman{section}.}%
  \alph{subsection}%
}
\renewcommand\thesubsubsection{%
  \perhapsprint{\Roman{section}.\alph{subsection}.}%
  \arabic{subsubsection}%
}

\usepackage{lipsum}

\begin{document}

\tableofcontents

\section{A section}\label{1}

References: \ref{1}, \ref{2}, \ref{3}, \ref{4}.

\lipsum[1]
\subsection{A subsection}\label{2}
\lipsum[2-4]
\subsection{Another subsection}\label{3}
\lipsum[5]
\subsubsection{A subsubsection}\label{4}
\lipsum[6]
\subsection{A subsection}\lipsum[7-8]
\subsection{Another subsection}\lipsum[9-10]
\section{Another section}\lipsum[1-2]
\subsection{A subsection}\lipsum[3-4]
\subsection{Another subsection}\lipsum[5-6]
\subsection{A subsection}\lipsum[7-8]
\subsection{Another subsection}\lipsum[9-10]
\end{document}

在此处输入图片描述

答案2

哦,我解决了。正在重新定义部分格式titleformat

\documentclass{article}

\usepackage{xcolor}
\definecolor{Blue}{HTML}{2E3192}

% heading styles:
\usepackage{titlesec}
\titleformat{\section}
  {\color{teal}\large\sffamily}
  {\thetitle.\hspace{0.5cm}}
  {0cm}
  {}

\titleformat{\subsection}
  {\color{violet}\bfseries}
  {\renewcommand\thesubsection{\Roman{section}.\alph{subsection}}\hspace{0.75cm}\thetitle.\hspace{0.3cm}}
  {0cm}
  {}

\titleformat{\subsubsection}
  {\color{olive}\itshape}
  {\renewcommand\thesubsubsection{\Roman{section}.\alph{subsection}.\arabic{subsubsection}}\hspace{1.3cm}\thetitle.\hspace{0.3cm}}
  {0cm}
  {}

% ToC styles:
\usepackage[dotinlabels]{titletoc}
\contentsmargin{2.55em} % space for a page number

\titlecontents{section}
    [3.8em] % 1.5em + 2.3em
    {\vspace{0.3cm}\sffamily\bfseries\large}
    {\contentslabel{2.3em}}
    {\hspace*{-2.3em}}
    {\normalsize\normalfont\rmfamily\titlerule*[1pc]{.}\contentspage}
    [\vspace{0.1cm}]

\titlecontents{subsection}
    [5.2em] % 3.8em + 1.3em
    {\vspace{0.1cm}}
    {\contentslabel{1.4em}}
    {\hspace*{-1.4em}}
    {\titlerule*[1pc]{.}\contentspage}
    []

\titlecontents{subsubsection}
    [6.5em] % 5.2em + 1.3em
    {\vspace{0.1cm}}
    {\contentslabel{1.3em}}
    {\hspace*{-1.3em}}
    {\titlerule*[1pc]{.}\contentspage}
    []

\renewcommand\thesection{\Roman{section}}
\renewcommand\thesubsection{\alph{subsection}}
\renewcommand\thesubsubsection{\arabic{subsubsection}}

\usepackage{lipsum}

\begin{document}

\tableofcontents

\section{A section}\lipsum[1]
\subsection{A subsection}\lipsum[2-4]
\subsection{Another subsection}\lipsum[5-6]
\subsection{A subsection}\lipsum[7-8]
\subsection{Another subsection}\lipsum[9-10]
\section{Another section}\lipsum[1-2]
\subsection{A subsection}\lipsum[3-4]
\subsection{Another subsection}\lipsum[5-6]
\subsection{A subsection}\lipsum[7-8]
\subsection{Another subsection}\lipsum[9-10]
\end{document}

给出:

在此处输入图片描述

相关内容