Latex:创建几个新的子层会导致一些计数错误

Latex:创建几个新的子层会导致一些计数错误

我尝试创建一种扩展 Latex 中切片层数的方法。当前的 Layers\section和不够,并且与其他库一起出现问题。因此,我创建了一个可以包含七个层的切片系统,包括\subsection、、、、...... 。代码主要来自\subsubsection\paragraph\subparagraph\section\subsection\twosubsection\threesubsection\foursubsection这个答案

但是当我在多个二分节中实现三分节时,计数器不会重置(见下面的示例)。此外,附录的章节编号方式错误。第一个子节应该编号为 A.1,而不是 1.1。

你能想到解决这个问题的方法吗?

\documentclass{article}
\usepackage{titlesec}
\usepackage{appendix}

%% Section-Titles ---------------------------------

% Deklaration of the section functions
\newcommand{\twosubsection}[1]{\subsubsection{#1}}
\titleclass{\threesubsection}{straight}[\subsubsection]
\titleclass{\foursubsection}{straight}[\threesubsection]
\titleclass{\fivesubsection}{straight}[\foursubsection]
\titleclass{\sixsubsection}{straight}[\fivesubsection]

% Counters
\newcounter{threesubsection}
\newcounter{foursubsection}
\newcounter{fivesubsection}
\newcounter{sixsubsection}

% Format of the section titles
\titleformat{\section}
  {\normalfont\Large\bfseries}
  {\thesection}
  {1em}
  {}
\titleformat{\subsection}
  {\normalfont\large\bfseries}
  {\thesubsection}
  {1em}
  {}
\titleformat{\subsubsection}
  {\normalfont\normalsize\bfseries}
  {\thesubsubsection}
  {1em}
  {}
\titleformat{\threesubsection}
  {\normalfont\normalsize\bfseries}
  {\thethreesubsection}
  {1em}
  {}
\titleformat{\foursubsection}
  {\normalfont\normalsize\bfseries}
  {\thefoursubsection}
  {1em}
  {}
\titleformat{\fivesubsection}
  {\normalfont\normalsize\bfseries}
  {\thefivesubsection}
  {1em}
  {}
\titleformat{\sixsubsection}
  {\normalfont\normalsize\bfseries}
  {\thesixsubsection}
  {1em}
  {}

% Spacing of the section titles
\titlespacing*{\twosubsection}
  {0pt}{2.25ex plus 1ex minus .2ex}{0em}
\titlespacing*{\threesubsection}
  {0pt}{2.25ex plus 1ex minus .2ex}{0.5em}
\titlespacing*{\foursubsection}
  {0pt}{1.25ex plus 1ex minus .2ex}{0em}
\titlespacing*{\fivesubsection}
  {0pt}{1.25ex plus 1ex minus .2ex}{0em}
\titlespacing*{\sixsubsection}
  {0pt}{1.25ex plus 1ex minus .2ex}{0em}

% Configuration of the Counters
\renewcommand\thesection{\arabic{section}}
\renewcommand\thesubsection{\arabic{section}.\arabic{subsection}}
\renewcommand\thesubsubsection{\arabic{section}.\arabic{subsection}.\arabic{subsubsection}}
\renewcommand\thethreesubsection{\arabic{section}.\arabic{subsection}.\arabic{subsubsection}.\arabic{threesubsection}}
\renewcommand\thefoursubsection{\arabic{section}.\arabic{subsection}.\arabic{subsubsection}.\arabic{threesubsection}.\arabic{foursubsection}}
\renewcommand\thefivesubsection{\arabic{section}.\arabic{subsection}.\arabic{subsubsection}.\arabic{threesubsection}.\arabic{foursubsection}.\arabic{fivesubsection}}
\renewcommand\thesixsubsection{\arabic{section}.\arabic{subsection}.\arabic{subsubsection}.\arabic{threesubsection}.\arabic{foursubsection}.\arabic{fivesubsection}.\arabic{sixsubsection}}

\makeatletter

% ToC counters
\setcounter{secnumdepth}{7}
\setcounter{tocdepth}{7}
\def\toclevel@threesubsection{4}
\def\toclevel@foursubsection{5}
\def\toclevel@fivesubsection{6}
\def\toclevel@sixsubsection{7}

% ToC format
\renewcommand*\l@subsection{\@dottedtocline{2}{1.5em}{2em}}
\renewcommand*\l@subsubsection{\@dottedtocline{3}{3.5em}{3em}}
\newcommand*\l@threesubsection{\@dottedtocline{4}{6.5em}{4em}}
\newcommand*\l@foursubsection{\@dottedtocline{4}{10.5em}{5em}}
\newcommand*\l@fivesubsection{\@dottedtocline{4}{15.5em}{6em}}
\newcommand*\l@sixsubsection{\@dottedtocline{4}{21.5em}{7em}}

\makeatother

%% == Document ===========================================
\begin{document}

\tableofcontents

\section{Section}

\subsection{Subsection}

\twosubsection{Subsubsection}

\threesubsection{Subsubsubsection}

\section{Section}

\subsection{Subsection}

\twosubsection{Subsubsection}

\threesubsection{Subsubsubsection with wrong counter}

\appendix

\section{Appendix Section}

\subsection{Appendix Subsection}

\end{document}

代码输出

答案1

chngcntr这可以通过使用包和命令轻松完成\counterwithin{threesubsection}{subsubsection}

对于附录中的小节重新编号,您可以包括以下内容

\preto{\appendix}{\renewcommand{\thesubsection}{\Alph{section}. \arabic{subsection}}}

\documentclass{article}
\usepackage{titlesec}
\usepackage{appendix}

%% Section-Titles ---------------------------------

% Deklaration of the section functions
\newcommand{\twosubsection}[1]{\subsubsection{#1}}
\titleclass{\threesubsection}{straight}[\subsubsection]
\titleclass{\foursubsection}{straight}[\threesubsection]
\titleclass{\fivesubsection}{straight}[\foursubsection]
\titleclass{\sixsubsection}{straight}[\fivesubsection]

% Counters
\newcounter{threesubsection}
\newcounter{foursubsection}
\newcounter{fivesubsection}
\newcounter{sixsubsection}

% Format of the section titles
\titleformat{\section}
{\normalfont\Large\bfseries}
{\thesection}
{1em}
{}
\titleformat{\subsection}
{\normalfont\large\bfseries}
{\thesubsection}
{1em}
{}
\titleformat{\subsubsection}
{\normalfont\normalsize\bfseries}
{\thesubsubsection}
{1em}
{}
\titleformat{\threesubsection}
{\normalfont\normalsize\bfseries}
{\thethreesubsection}
{1em}
{}
\titleformat{\foursubsection}
{\normalfont\normalsize\bfseries}
{\thefoursubsection}
{1em}
{}
\titleformat{\fivesubsection}
{\normalfont\normalsize\bfseries}
{\thefivesubsection}
{1em}
{}
\titleformat{\sixsubsection}
{\normalfont\normalsize\bfseries}
{\thesixsubsection}
{1em}
{}

% Spacing of the section titles
\titlespacing*{\twosubsection}
{0pt}{2.25ex plus 1ex minus .2ex}{0em}
\titlespacing*{\threesubsection}
{0pt}{2.25ex plus 1ex minus .2ex}{0.5em}
\titlespacing*{\foursubsection}
{0pt}{1.25ex plus 1ex minus .2ex}{0em}
\titlespacing*{\fivesubsection}
{0pt}{1.25ex plus 1ex minus .2ex}{0em}
\titlespacing*{\sixsubsection}
{0pt}{1.25ex plus 1ex minus .2ex}{0em}

% Configuration of the Counters
\renewcommand\thesection{\arabic{section}}
\renewcommand\thesubsection{\arabic{section}.\arabic{subsection}}
\renewcommand\thesubsubsection{\arabic{section}.\arabic{subsection}.\arabic{subsubsection}}
\renewcommand\thethreesubsection{\arabic{section}.\arabic{subsection}.\arabic{subsubsection}.\arabic{threesubsection}}
\renewcommand\thefoursubsection{\arabic{section}.\arabic{subsection}.\arabic{subsubsection}.\arabic{threesubsection}.\arabic{foursubsection}}
\renewcommand\thefivesubsection{\arabic{section}.\arabic{subsection}.\arabic{subsubsection}.\arabic{threesubsection}.\arabic{foursubsection}.\arabic{fivesubsection}}
\renewcommand\thesixsubsection{\arabic{section}.\arabic{subsection}.\arabic{subsubsection}.\arabic{threesubsection}.\arabic{foursubsection}.\arabic{fivesubsection}.\arabic{sixsubsection}}

\makeatletter

% ToC counters
\setcounter{secnumdepth}{7}
\setcounter{tocdepth}{7}
\def\toclevel@threesubsection{4}
\def\toclevel@foursubsection{5}
\def\toclevel@fivesubsection{6}
\def\toclevel@sixsubsection{7}

% ToC format
\renewcommand*\l@subsection{\@dottedtocline{2}{1.5em}{2em}}
\renewcommand*\l@subsubsection{\@dottedtocline{3}{3.5em}{3em}}
\newcommand*\l@threesubsection{\@dottedtocline{4}{6.5em}{4em}}
\newcommand*\l@foursubsection{\@dottedtocline{4}{10.5em}{5em}}
\newcommand*\l@fivesubsection{\@dottedtocline{4}{15.5em}{6em}}
\newcommand*\l@sixsubsection{\@dottedtocline{4}{21.5em}{7em}}

\makeatother

\usepackage{chngcntr}
\counterwithin{threesubsection}{subsubsection}

\usepackage{etoolbox}
\preto{\appendix}{\renewcommand{\thesubsection}{\Alph{section}. \arabic{subsection}}}

%% == Document ===========================================
\begin{document}
    
    \tableofcontents
    
    \section{Section}
    
    \subsection{Subsection}
    
    \twosubsection{Subsubsection}
    
    \threesubsection{Subsubsubsection}
    
    \section{Section}
    
    \subsection{Subsection}
    
    
    \twosubsection{Subsubsection}
    
    \threesubsection{Subsubsubsection with correct counter}
    
    \appendix
    
    \section{Appendix Section}
    
    \subsection{Appendix Subsection}
    
\end{document}

相关内容