我是 Latex 新手。我需要像这样的文档结构,并在页边空白处进行编号。
但是,即使我的代码中 secnumdepth 设置为 5,我也无法使 \paragraph 和 \subparagraph 正常工作。我使用的代码基于给出的答案在论坛里
我使用的代码如下:
\documentclass{article}
\usepackage{lipsum}
\usepackage{titlesec,titletoc}
\usepackage{etoolbox}
\usepackage{lipsum}
\setcounter{secnumdepth}{5}
\renewcommand\thesection{\arabic{section}}
% this length controls tha hanging indent for titles
% change the value according to your needs
\newlength\titleindent
\setlength\titleindent{2cm}
% counters for the new sectional units
\newcounter{subsectionwt}
\newcounter{subsubsectionwt}
% definition of the new sectional units with the representation of the counters
\titleclass{\subsectionwt}{straight}[\subsubsection]
\renewcommand{\thesubsectionwt}{\thesection.\arabic{subsectionwt}}
\titleclass{\subsubsectionwt}{straight}[\subsectionwt]
\renewcommand{\thesubsubsectionwt}{\thesubsection.\arabic{subsubsectionwt}}
% \subsection must increase the subsectionwt counter
% and \subsectionwt must increase the subsection counter
% Analogous treatment for \subsubsection and \subsubsectionwt
\pretocmd{\subsectionwt}{\stepcounter{subsection}}{}{}
\pretocmd{\subsection}{\stepcounter{subsectionwt}}{}{}
\pretocmd{\subsubsectionwt}{\stepcounter{subsubsection}}{}{}
\pretocmd{\subsubsection}{\stepcounter{subsubsectionwt}}{}{}
% format for the sectional units
\titleformat{\chapter}[block]
{\normalfont\huge\bfseries}{}{0pt}{\hspace*{-\titleindent}}
\titleformat{\section}
{\normalfont\Large\bfseries}{\llap{\parbox{\titleindent}{\thesection\hfill}}}{0em}{}
\titleformat{\subsection}
{\normalfont\large}{\llap{\parbox{\titleindent}{\thesubsection\hfill}}}{0em}{\bfseries}
\titleformat{\subsectionwt}[runin]
{\normalfont\large}{\llap{\parbox{\titleindent}{\thesubsectionwt\hfill}}}{0em}{\bfseries}
\titleformat{\subsubsection}
{\normalfont\normalsize}{\llap{\parbox{\titleindent}{\thesubsubsection}}}{0em}{\bfseries}
\titleformat{\subsubsectionwt}[runin]
{\normalfont\normalsize}{\llap{\parbox{\titleindent}{\thesubsubsectionwt}}}{0em}{\bfseries}
\titlespacing*{\chapter}{0pt}{0pt}{20pt}
\titlespacing*{\subsection}{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}
\titlespacing*{\subsectionwt}{0pt}{3.25ex plus 1ex minus .2ex}{0em}
\titlespacing*{\subsubsection}{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}
\titlespacing*{\subsubsectionwt}{0pt}{3.25ex plus 1ex minus .2ex}{0em}
% numbered entries without title shouldn't go to the ToC
\titlecontents{subsectionwt}[]{}{}{}{}
\titlecontents{subsubsectionwt}[]{}{}{}{}
\begin{document}
\section{Section One}
Section One Text
\subsection{Sub Section One One}
Sub Section One One Text
\subsectionwt{}
Sub Section One One Text without title
\subsubsection{Sub Sub Section One One One}
Sub Sub Section One One OneText
\subsubsectionwt{}
Sub Sub Section One One OneText without title
\paragraph{} Lorem ipsum dolor sit amet, sed ea nihil clita adolescens, sea tation libris ad, sed an omittam comprehensam.
\subparagraph{}Id summo invenire his. Paulo posidonium honestatis ei cum, per cu aliquid salutatus consequuntur.
\end{document}
使用 David Carlisle 在其回答中建议的代码,我修改了我的代码以及示例文档文本(以使其在结构和可读性上更有意义)。修改后的代码如下:
\documentclass{article}
\setcounter{secnumdepth}{5}
\renewcommand\thesection{\arabic{section}}
\makeatletter
\renewcommand\paragraph{\@startsection{paragraph}{4}{\z@}%
{3.25ex \@plus1ex \@minus.2ex}%
{-1sp}%
{\normalfont\normalsize\bfseries}}
\renewcommand\subparagraph{\@startsection{subparagraph}{5}{\z@}%
{3.25ex \@plus1ex \@minus .2ex}%
{-1sp}%
{\normalfont\normalsize\bfseries}}
% like section and subsection but inline not display (-sp) and with empty title supplied ({})
\newcommand\subsectionwd{\@startsection{subsection}{2}{\z@}%
{-3.25ex\@plus -1ex \@minus -.2ex}%
{-1sp}%
{\normalfont\large\bfseries}{}}
\newcommand\subsubsectionwd{\@startsection{subsubsection}{3}{\z@}%
{-3.25ex\@plus -1ex \@minus -.2ex}%
{-1sp}%
{\normalfont\normalsize\bfseries}{}}
\def\@seccntformat#1{\makebox[0pt][r]{\csname the#1\endcsname\quad}}
\makeatother
\begin{document}
\tableofcontents
\newpage
\section{Section 1}
Section 1 Text with title
\section{Section 2}
\subsectionwd
Sub Section 2-1 Text without title
\subsectionwd
Sub Section 2-2 Text without title
\section{Section 3}
\subsection{Sub Section 3-1}
Sub Section 3-1 text with title
\subsection{Sub Section 3-2}
Sub Section 3-2 text with title
\subsubsectionwd
Sub Sub Section 3-2-1 without title
\subsubsectionwd
Sub Sub Section 3-2-2 without title
\subsection{Sub Section 3-3}
Sub Section 3-3 text with title
\subsubsection{Sub Sub Section 3-3-1}
Sub Sub Section 3-3-1 text with title
\paragraph{} Lorem ipsum dolor sit amet, sed ea nihil clita adolescens, sea tation libris ad, sed an omittam comprehensam.
\subparagraph{}Id summo invenire his. Paulo posidonium honestatis ei cum, per cu aliquid salutatus consequuntur.
\end{document}
我现在得到的输出在目录中出现了无标题的部分,这是不想要的。目录的输出如下所示
如何从目录中删除这些内容,同时保留正文中的编号?
答案1
您的图像显示了居中的数字(我认为),您可以通过改变\makebox
用法来实现,但右对齐似乎更自然。
这不使用任何包,只是对从中复制的定义进行微小的调整article.cls
\documentclass{article}
\setcounter{secnumdepth}{5}
\renewcommand\thesection{\arabic{section}}
\makeatletter
\renewcommand\paragraph{\@startsection{paragraph}{4}{\z@}%
{3.25ex \@plus1ex \@minus.2ex}%
{-1sp}%
{\normalfont\normalsize\bfseries}}
\renewcommand\subparagraph{\@startsection{subparagraph}{5}{\z@}%
{3.25ex \@plus1ex \@minus .2ex}%
{-1sp}%
{\normalfont\normalsize\bfseries}}
% like section and subsection but inline not display (-sp) and with empty title supplied ({})
\newcommand\subsectionwd{\@startsection{subsection}{2}{\z@}%
{-3.25ex\@plus -1ex \@minus -.2ex}%
{-1sp}%
{\normalfont\large\bfseries}{}}
\newcommand\subsubsectionwd{\@startsection{subsubsection}{3}{\z@}%
{-3.25ex\@plus -1ex \@minus -.2ex}%
{-1sp}%
{\normalfont\normalsize\bfseries}{}}
\def\@seccntformat#1{\makebox[0pt][r]{\csname the#1\endcsname\quad}}
\makeatother
\begin{document}
\section{Section One}
Section One Text
\subsection{Sub Section One One}
Sub Section One One Text
\subsectionwd
Sub Section One One Text without title
\subsubsection{Sub Sub Section One One One}
Sub Sub Section One One OneText
\subsubsectionwd
Sub Sub Section One One OneText without title
\paragraph{} Lorem ipsum dolor sit amet, sed ea nihil clita adolescens, sea tation libris ad, sed an omittam comprehensam.
\subparagraph{}Id summo invenire his. Paulo posidonium honestatis ei cum, per cu aliquid salutatus consequuntur.
\end{document}