从目录中排除标题为空的部分

从目录中排除标题为空的部分

我从\section 标题为空。在第一段旁边显示数字,我使用\section{}空标题,这将用数字标记该部分的第一段。这对于我的用例来说是完美的,但是:

如何从目录中排除标题为空的部分?

到目前为止我得到的信息是这样的:

\documentclass{report}
\usepackage[a4paper, margin=1.3in]{geometry}

\usepackage{etoolbox}
\usepackage[explicit]{titlesec}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Set section numbers in the margin and center the chapter headings
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\setcounter{secnumdepth}{5}
\renewcommand\thesection{\arabic{section}}

% this length controls the hanging indent for titles
% change the value according to your needs
\newlength\titleindent
\setlength\titleindent{1.35cm}

\titleformat{\chapter}[block]
  {\normalfont\huge\bfseries\center}{}{0pt}{\hspace*{-\titleindent}#1}

\titleformat{\section}
  {\normalfont\Large\bfseries}
  {\llap{\parbox[t]{\titleindent}{\thesection\hfill}}}{0em}
  {\if\detokenize{#1}\relax\relax
      {\Large\bfseries\vspace*{-\dimexpr2.3ex+0.5\baselineskip\relax}}
    \else#1
    \fi
  }

\titleformat{\subsection}
  {\normalfont\large}{\llap{\parbox[t]{\titleindent}{\thesubsection\hfill}}}
  {0em}
  {\if\detokenize{#1}\relax\relax
      {\Large\bfseries\vspace*{-\dimexpr1.5ex+0.5\baselineskip\relax}}
    \else\bfseries#1
    \fi
  }

\titleformat{\subsubsection}
  {\normalfont\normalsize}{\llap{\parbox[t]{\titleindent}{\thesubsubsection}}}
  {0em}
  {\if\detokenize{#1}\relax\relax
      {\Large\bfseries\vspace*{-\dimexpr1.5ex+0.5\baselineskip\relax}}
    \else\bfseries#1
    \fi
  }

\titleformat{\paragraph}[runin]
  {\normalfont\large}{\llap{\parbox{\titleindent}{\theparagraph\hfill}}}{0em}{}

\titleformat{\subparagraph}[runin]
  {\normalfont\normalsize}{\llap{\parbox{\titleindent}{\theparagraph\hfill}}}{0em}{}

\titlespacing*{\chapter}{0pt}{0pt}{20pt}
\titlespacing*{\section}
{0pt}{3.5ex}{2.3ex}
\titlespacing*{\subsection}
{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}
\titlespacing*{\subsubsection}{0pt}{3.25ex}{1.5ex}
\titlespacing*{\paragraph}{0pt}{3.25ex plus 1ex minus .2ex}{0em}
\titlespacing*{\subparagraph}{0pt}{3.25ex plus 1ex minus .2ex}{0em}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Make chapters show up in TOC and NOT reset the section counters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\setcounter{chapter}{0}
\newcommand{\mychapter}[1]{
    \chapter*{#1}
    \addcontentsline{toc}{chapter}{#1}
}


\begin{document}

\tableofcontents
\cleardoublepage

\mychapter{INITIAL CLAUSES}

\section{A Section title}
\subsection{A subsection with a title} 
Some text

\subsection{} This subsection has no title, so the numbers are displayed next to this text instead.

\subsection{} These subsections should not be included in the TOC


\end{document}

含有空条目的目录

答案1

作为条件测试的一部分,查看标题是否为空,您可以添加更改 ToC 深度所需的代码:

\documentclass{report}
\usepackage[a4paper, margin=1.3in]{geometry}

\usepackage{etoolbox}
\usepackage[explicit]{titlesec}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Set section numbers in the margin and center the chapter headings
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\setcounter{secnumdepth}{5}
\renewcommand\thesection{\arabic{section}}

% this length controls the hanging indent for titles
% change the value according to your needs
\newlength\titleindent
\setlength\titleindent{1.35cm}


\titleformat{\chapter}[block]
  {\normalfont\huge\bfseries\center}{}{0pt}{\hspace*{-\titleindent}#1}

\titleformat{\section}
  {\normalfont\Large\bfseries}
  {\llap{\parbox[t]{\titleindent}{\thesection\hfill}}}{0em}
  {\if\detokenize{#1}\relax\relax
      {\Large\bfseries\vspace*{-\dimexpr2.3ex+0.5\baselineskip\relax}}
      {\addtocontents{toc}{\setcounter{tocdepth}{-3}}}
    \else#1
      \addtocontents{toc}{\setcounter{tocdepth}{3}}
    \fi
  }

\titleformat{\subsection}
  {\normalfont\large}{\llap{\parbox[t]{\titleindent}{\thesubsection\hfill}}}
  {0em}
  {\if\detokenize{#1}\relax\relax
      {\Large\bfseries\vspace*{-\dimexpr1.5ex+0.5\baselineskip\relax}}
      {\addtocontents{toc}{\setcounter{tocdepth}{-3}}}
    \else\bfseries#1
      \addtocontents{toc}{\setcounter{tocdepth}{3}}
    \fi
  }

\titleformat{\subsubsection}
  {\normalfont\normalsize}{\llap{\parbox[t]{\titleindent}{\thesubsubsection}}}
  {0em}
  {\if\detokenize{#1}\relax\relax
      {\Large\bfseries\vspace*{-\dimexpr1.5ex+0.5\baselineskip\relax}}
    \else\bfseries#1
    \fi
  }

\titleformat{\paragraph}[runin]
  {\normalfont\large}{\llap{\parbox{\titleindent}{\theparagraph\hfill}}}{0em}{}

\titleformat{\subparagraph}[runin]
  {\normalfont\normalsize}{\llap{\parbox{\titleindent}{\theparagraph\hfill}}}{0em}{}

\titlespacing*{\chapter}{0pt}{0pt}{20pt}
\titlespacing*{\section}
{0pt}{3.5ex}{2.3ex}
\titlespacing*{\subsection}
{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}
\titlespacing*{\subsubsection}{0pt}{3.25ex}{1.5ex}
\titlespacing*{\paragraph}{0pt}{3.25ex plus 1ex minus .2ex}{0em}
\titlespacing*{\subparagraph}{0pt}{3.25ex plus 1ex minus .2ex}{0em}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Make chapters show up in TOC and NOT reset the section counters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\setcounter{chapter}{0}
\newcommand{\mychapter}[1]{
    \chapter*{#1}
    \addcontentsline{toc}{chapter}{#1}
}


\begin{document}

\tableofcontents
\cleardoublepage

\mychapter{INITIAL CLAUSES}

\section{A section with title}
\subsection{A subsection with a title} 
Some text

\subsection{} This subsection has no title, so the numbers are displayed next to this text instead.

\subsection{} These subsections should not be included in the TOC

\subsection{Another subsection with a title} 
Some text

\section{} This section has no title
\section{} This section has no title

\section{Another section with title}
\subsection{Another subsection with a title} 

\end{document}

在此处输入图片描述

答案2

我并不是赞同你所做的,但事实就是如此。

您可以定义两个新命令,\emptysection并且\emptysubsection

\newcommand{\emptysection}[1]{%
    \begingroup\renewcommand\addcontentsline[3]{}%
    \section{#1}\endgroup%
}

\newcommand{\emptysubsection}[1]{%
    \begingroup\renewcommand\addcontentsline[3]{}%
    \subsection{#1}\endgroup%
}

当它们必须没有内容时,使用它们代替\section和。\subsection

梅威瑟:

\documentclass{report}
\usepackage[a4paper, margin=1.3in]{geometry}

\usepackage{etoolbox}
\usepackage[explicit]{titlesec}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Set section numbers in the margin and center the chapter headings
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\setcounter{secnumdepth}{5}
\renewcommand\thesection{\arabic{section}}

% this length controls the hanging indent for titles
% change the value according to your needs
\newlength\titleindent
\setlength\titleindent{1.35cm}

\titleformat{\chapter}[block]
  {\normalfont\huge\bfseries\center}{}{0pt}{\hspace*{-\titleindent}#1}

\titleformat{\section}
  {\normalfont\Large\bfseries}
  {\llap{\parbox[t]{\titleindent}{\thesection\hfill}}}{0em}
  {\if\detokenize{#1}\relax\relax
      {\Large\bfseries\vspace*{-\dimexpr2.3ex+0.5\baselineskip\relax}}
    \else#1
    \fi
  }

\titleformat{\subsection}
  {\normalfont\large}{\llap{\parbox[t]{\titleindent}{\thesubsection\hfill}}}
  {0em}
  {\if\detokenize{#1}\relax\relax
      {\Large\bfseries\vspace*{-\dimexpr1.5ex+0.5\baselineskip\relax}}
    \else\bfseries#1
    \fi
  }

\titleformat{\subsubsection}
  {\normalfont\normalsize}{\llap{\parbox[t]{\titleindent}{\thesubsubsection}}}
  {0em}
  {\if\detokenize{#1}\relax\relax
      {\Large\bfseries\vspace*{-\dimexpr1.5ex+0.5\baselineskip\relax}}
    \else\bfseries#1
    \fi
  }

\titleformat{\paragraph}[runin]
  {\normalfont\large}{\llap{\parbox{\titleindent}{\theparagraph\hfill}}}{0em}{}

\titleformat{\subparagraph}[runin]
  {\normalfont\normalsize}{\llap{\parbox{\titleindent}{\theparagraph\hfill}}}{0em}{}

\titlespacing*{\chapter}{0pt}{0pt}{20pt}
\titlespacing*{\section}
{0pt}{3.5ex}{2.3ex}
\titlespacing*{\subsection}
{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}
\titlespacing*{\subsubsection}{0pt}{3.25ex}{1.5ex}
\titlespacing*{\paragraph}{0pt}{3.25ex plus 1ex minus .2ex}{0em}
\titlespacing*{\subparagraph}{0pt}{3.25ex plus 1ex minus .2ex}{0em}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Make chapters show up in TOC and NOT reset the section counters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\setcounter{chapter}{0}
\newcommand{\mychapter}[1]{
    \chapter*{#1}
    \addcontentsline{toc}{chapter}{#1}
}

\newcommand{\emptysection}[1]{%
    \begingroup\renewcommand\addcontentsline[3]{}%
    \section{#1}\endgroup%
}

\newcommand{\emptysubsection}[1]{%
    \begingroup\renewcommand\addcontentsline[3]{}%
    \subsection{#1}\endgroup%
}

\begin{document}

\tableofcontents
\cleardoublepage

\mychapter{INITIAL CLAUSES}

\section{A Section title}
\subsection{A subsection with a title}
Some text

\emptysubsection{} This subsection has no title, so the numbers are displayed next to this text instead.

\emptysubsection{} These subsections should not be included in the TOC

\end{document} 

输出(目录):

在此处输入图片描述

相关内容