我正在尝试对段落进行编号:
\documentclass{article}
\newcounter{para}
\setcounter{secnumdepth}{4}
\begin{document}
\section{Lorum ipsum}
\subsection{Lorum ipsum}
\paragraph{} Lorum ipsum
\end{document}
但是,我在编号中确实出现了不必要的“0” - 因此示例中的段落是 1.1.0.1。我该如何避免这种情况?
答案1
下面给出的代码实现了 OP 想要的功能。
我不得不说,在使用段落级标题时跳过小节级标题是一种非常糟糕且粗心的排版做法。一定有办法创建一个虚拟类型的小节级标题,不是吗?
\documentclass{article}
\setcounter{secnumdepth}{4} % default: 3
% Suppress value of subsubsection counter if equal to 0:
\renewcommand{\theparagraph}{%
\ifnum\value{subsubsection}=0 \thesubsection.\arabic{paragraph}
\else \thesubsubsection.\arabic{paragraph}
\fi}
\usepackage{lipsum} % filler text
\begin{document}
\section{Lorem ipsum}
\subsection{Lorem ipsum}
%\subsubsection{Lorem ipsum} % commented out
\paragraph{Lorem ipsum}
\lipsum[1][1-3] % filler text
\section{Nam dui}
\subsection{Nam dui}
\subsubsection{Nam dui} % *not* commented out
\paragraph{Nam dui}
\lipsum[2][1-3] % more filler text
\end{document}
答案2
修改Mico 的回答,出于以下几个原因:
- 这是更简单的代码并且更容易定制;
- 您似乎使用了
\paragraph
空参数,所以我们需要修复间距。
\documentclass{article}
\usepackage{lipsum} % filler text
\setcounter{secnumdepth}{4} % default: 3
% Suppress value of subsubsection counter if equal to 0:
\renewcommand{\thesubsubsection}{%
\thesubsection
\ifnum\value{subsubsection}>0
.\arabic{subsubsection}%
\fi
}
\makeatletter
% by default, the fifth argument is -1em, but you don't want this amount
\renewcommand{\paragraph}{%
\@startsection{paragraph}% level name
{4}% level number
{\z@}% indentation
{3.25ex \@plus 1ex \@minus .2ex}% space before
{-1sp}% space after (negative for inline)
{\normalfont\normalsize\bfseries}% formatting
}
\makeatother
\begin{document}
\section{A first section title}
\subsection{This has no third level sections}
\paragraph{} \lipsum[1][1-3]
\section{A second section title}
\subsection{This has a third level section}
\subsubsection{Whatever you want}
\paragraph{} \lipsum[2][1-3]
\end{document}
如果你将代码改为
% Suppress value of subsubsection counter if equal to 0
% and make subsubsections letter numbered
\renewcommand{\thesubsubsection}{%
\thesubsection
\ifnum\value{subsubsection}>0
.\alph{subsubsection}%
\fi
}
你会得到一些有助于区分级别的东西。