22

I am a novice in LaTeX and I have one question.

Can someone tell me what if you want more heading's in LaTeX. Like 5 or 10 or even 15. Is there a package for that?

lockstep
  • 250,273
B. T.
  • 289
  • 1
  • 2
  • 5
  • 2
    Could you explain 'more headings'? If you mean more nested levels, there are good reasons why as standard only \chapter, \section, \subsection and \subsubsection are numbered. – Joseph Wright Oct 09 '11 at 08:52
  • You mean \section{A section title}? You can add as many of those as you want, no need for a package. If not, can you clarify what you're after. – Torbjørn T. Oct 09 '11 at 08:53
  • Sometimes I need only 3 headings, but sometimes I need more like 4 to 10 or more. All headings should be in the table of contents automatically and have their parent and children

    Heading 1 Heading 1.1 Heading 1.2 Heading 1.2.1 Heading 1.2.2 Heading 1.2.2.1 Heading 1.2.2.2 Heading 1.2.2.1

    and they all have to show in TOC:

    Heading 1..................1 Heading 1.1................1 Heading 1.2................2 Heading 1.2.1..............2 Heading 1.2.2..............3 Heading 1.2.2.1............3 Heading 1.2.2.2............4 Heading 1.2.2.1...........10

    – B. T. Oct 13 '11 at 15:44
  • 5
    @B.T.: Please do not ignore the remarks about style lightly. While MS Word will allow you to commit any kind of stylistic crime, having your headings nested so deeply is a sign for an underlying problem. Your document will not be easy to read, or find anything in. Unless you are looking at the type of write-only documentation sadly popular in the IT industry these days... I have written a RPG rulebook 250 pages long, and had no need whatsoever to go beyond "subsubsection". The Intel Architecture Software Manual doesn't go beyond that, at that's a behemoth. – DevSolar Oct 14 '11 at 09:12

3 Answers3

37

This shouldn't be taken too seriously, as deep numbering is not recommended. However, here's how one can have 100 sectional levels (all are modeled after \subsubsection after that level).

\documentclass[a4paper]{report}

\makeatletter
\newcommand\level[1]{%
  \ifcase#1\relax\expandafter\chapter\or
    \expandafter\section\or
    \expandafter\subsection\or
    \expandafter\subsubsection\else
    \def\next{\@level{#1}}\expandafter\next
  \fi}
\newcommand{\@level}[1]{%
  \@startsection{level#1}
    {#1}
    {\z@}%
    {-3.25ex\@plus -1ex \@minus -.2ex}%
    {1.5ex \@plus .2ex}%
    {\normalfont\normalsize\bfseries}}

\newcounter{level4}[subsubsection]
\@namedef{thelevel4}{\thesubsubsection.\arabic{level4}}
\@namedef{level4mark}#1{}
\count@=4
\loop\ifnum\count@<100
  \begingroup\edef\x{\endgroup
    \noexpand\newcounter{level\number\numexpr\count@+1\relax}[level\number\count@]
    \noexpand\@namedef{thelevel\number\numexpr\count@+1\relax}{%
      \noexpand\@nameuse{thelevel\number\count@}.\noexpand\arabic{level\number\numexpr\count@+1\relax}}
    \noexpand\@namedef{level\number\numexpr\count@+1\relax mark}####1{}}
  \x
  \advance\count@\@ne
\repeat
\makeatother
\setcounter{secnumdepth}{100}

\begin{document}

\level{0}{abc}
\level{1}{abc}
\level{2}{abc}
\level{3}{abc}
\level{4}{abc}
\level{5}{abc}
\level{6}{abc}
\level{7}{abc}
\level{8}{abc}
\level{9}{abc}
\level{10}{abc}
\level{11}{abc}
\level{12}{abc}
\end{document}

The commands \level{0}, \level{1}, \level{2}, and \level{3} are synonymous with \chapter, \section, \subsection, and \subsubsection.

enter image description here

ADDED

In case you want also to arrange for the table of contents, here's a way.

\documentclass[a4paper]{report}

\makeatletter
\newcommand\level[1]{%
  \ifcase#1\relax\expandafter\chapter\or
    \expandafter\section\or
    \expandafter\subsection\or
    \expandafter\subsubsection\else
    \def\next{\@level{#1}}\expandafter\next
  \fi}
\newcommand{\@level}[1]{%
  \@startsection{level#1}
    {#1}
    {\z@}%
    {-3.25ex\@plus -1ex \@minus -.2ex}%
    {1.5ex \@plus .2ex}%
    {\normalfont\normalsize\bfseries}}

\newdimen\@leveldim
\newdimen\@dotsdim
{\normalfont\normalsize
 \sbox\z@{0}\global\@leveldim=\wd\z@
 \sbox\z@{.}\global\@dotsdim=\wd\z@
}

\newcounter{level4}[subsubsection]
\@namedef{thelevel4}{\thesubsubsection.\arabic{level4}}
\@namedef{level4mark}#1{}
\def\l@section{\@dottedtocline{1}{0pt}{\dimexpr\@leveldim*4+\@dotsdim*1+6pt\relax}}
\def\l@subsection{\@dottedtocline{2}{0pt}{\dimexpr\@leveldim*5+\@dotsdim*2+6pt\relax}}
\def\l@subsubsection{\@dottedtocline{3}{0pt}{\dimexpr\@leveldim*6+\@dotsdim*3+6pt\relax}}
\@namedef{l@level4}{\@dottedtocline{4}{0pt}{\dimexpr\@leveldim*7+\@dotsdim*4+6pt\relax}}

\count@=4
\def\@ncp#1{\number\numexpr\count@+#1\relax}
\loop\ifnum\count@<100
  \begingroup\edef\x{\endgroup
    \noexpand\newcounter{level\@ncp{1}}[level\number\count@]
    \noexpand\@namedef{thelevel\@ncp{1}}{%
      \noexpand\@nameuse{thelevel\@ncp{0}}.\noexpand\arabic{level\@ncp{1}}}
    \noexpand\@namedef{level\@ncp{1}mark}####1{}%
    \noexpand\@namedef{l@level\@ncp{1}}%
      {\noexpand\@dottedtocline{\@ncp{1}}{0pt}{\the\dimexpr\@leveldim*\@ncp{5}+\@dotsdim*\@ncp{0}\relax}}}%
  \x
  \advance\count@\@ne
\repeat
\makeatother
\setcounter{secnumdepth}{100}
\setcounter{tocdepth}{100}
\begin{document}
\tableofcontents

\level{0}{abc}\thispagestyle{empty}
\level{1}{abc}
\level{2}{abc}
\level{3}{abc}
\level{4}{abc}
\level{5}{abc}
\level{6}{abc}
\level{7}{abc}
\level{8}{abc}
\level{9}{abc}
\level{10}{abc}
\level{11}{abc}
\level{12}{abc}
\end{document}

However I can't stand looking at the result.

egreg
  • 1,121,712
  • Nice that's what i want :) – B. T. Oct 13 '11 at 15:32
  • Are those heading included in table of content also? – B. T. Oct 13 '11 at 15:51
  • Also headings with \level{3} or more (4,5,6) is written on top of each other in \tableofcontents :(

    Help please :)

    – B. T. Oct 13 '11 at 17:02
  • 5
    @B.T. It's hard to understand the need of 10 numbered levels and harder to think about including them in the table of contents. – egreg Oct 13 '11 at 20:13
  • I was writing some technical documentation in MS Word, and there are 10 levels of headings. I'm trying to "convert" some documents to Latex and i need more than 3- levels. And TOC is also a MUST :( – B. T. Oct 13 '11 at 20:44
  • @B.T. I provided the macros for the toc; the spacing will be right if only chapters and sections go to double figures. – egreg Oct 13 '11 at 20:47
  • There's a problem with higher-level levels. Try: \level{1}{abc} \level{2}{abc} \level{3}{abc} \level{4}{abc} \level{5}{abc} \level{5}{abc} \level{5}{abc} – Spook Mar 22 '12 at 14:10
  • How should I modify this macro to add an indent the deeper you go ? Like chapter wouldn't be indented, section would have 1 indent, and so on ? –  Oct 08 '14 at 17:04
  • @NNzz As the contents will surely not continue with that indentation (I hope, at least), the numbers are sufficient to show the level. – egreg Oct 08 '14 at 17:37
  • I meant indenting the title of the section/subsection/... , not the content. –  Oct 08 '14 at 21:20
  • 1
    @NNzz And I meant to say it's not good style: a fourth level title would be almost centered. – egreg Oct 08 '14 at 21:22
  • Even with an indent size of, like, a space? –  Oct 08 '14 at 21:23
  • @NNzz Just to experiment, change \z@ in the definition of \@level with #1\dimexpr.3333em\relax; the first three levels won't be affected, but it should suffice to give a flavor. Add some text between the section titles and you'll see what I mean. – egreg Oct 08 '14 at 21:30
  • @egreg Any way to make it work with the tocloft package ? Below the subsubsection level, entries are not indented anymore. –  Dec 23 '14 at 14:08
  • @EddaSnorra No, no way with tocloft; this would require very extensive work. And such a TOC would be almost as long as the document (and pretty uniformative). – egreg Dec 23 '14 at 14:45
  • why I copy the code and run in Overleaf, main.tex file, the error said: Undefined control sequence, main.tex, line 1. – aan Aug 12 '19 at 17:10
  • @aan No idea; what undefined control sequence? – egreg Aug 12 '19 at 17:22
  • @egreg It refer to line 1 which is \documentclass[a4paper]{report} – aan Aug 13 '19 at 09:21
  • @aan You should typeset with LaTeX and probably you chose the wrong compile option. – egreg Aug 13 '19 at 09:32
  • @ egreg, i am using Overleaf and only have one compile. Sorry, i am very new to Latex – aan Aug 13 '19 at 10:11
7

If you are a novice to LaTeX with demands for complex sectioning, you might want to take a look at ConTeXt. Unless you are using MikTeX or an old TeX Live, it should work out of the box, just run context yourfile.tex or texexec yourfile.tex on the example below:

\setupcolors[state=start]
\usemodule[subsub]
\setuphead[section][color=red,style=\bfc]
\setuphead[subsection][color=blue,style=\bfb]
\setuphead[subsubsection][style=\bfa]
\setuphead[subsubsubsection][style=bold]
\starttext
\section{first section}
\subsection{a}
\subsubsection{b}
\subsubsubsection{c}
\subsubsubsubsection{d}
\subsubsubsubsubsection{e}
\subsubsubsubsubsubsection{f}
\subsubsubsubsubsubsubsection{g}
\subsubsubsubsubsubsubsubsection{h}
\subsubsubsubsubsubsubsubsubsection{i}
\stoptext

The \usemodule[subsub] defines some additional levels, but you can just as well easily define and configure your own (there is a slight difference between pdfTeX and LuaTeX-based ConTeXt as far as sectioning is concerned, so I didn't provide any more specific example, but I can provide more details if needed). If you want to provide headings different from \section, you can use

\definehead[myniceheading][section]
\setuphead[myniceheading][color=darkgreen,style=\bib,alternative=middle,number=no]

\myniceheading{Some heading}

See ConTeXt documentation at Pragma ADE and reference for \setuphead.

0

I found a possible solution - for anyone having the same trouble as I:

http://www-mobile.ecs.soton.ac.uk/scripts/viewvc.cgi/yja02r/thesis/packages/subsections.sty?view=co

http://web.archive.org/web/20100303003122/http://www-mobile.ecs.soton.ac.uk/scripts/viewvc.cgi/yja02r/thesis/packages/subsections.sty?revision=1.1.1.1

new .sty file with possible unlimited number of headings

%% How can I use \subsubsubsection in Latex, so that I can refer to it and also it appears in the table of contents?
% Here is a style file that you will need to use. It has entries to create subsubsubsection and subsubsubsubsection. If you need more then you should add whatever you need by taking the previous level of sub and adding a "sub" everywhere and change the level indicator {5} to {6}.
% I call this file subsections.sty:

% Here is an example file that worked with this style file:
%
% \documentstyle[subsections]{article}
% \setcounter{secnumdepth}{5}
% \setcounter{tocdepth}{5}
% \begin{document}
% \tableofcontents
% \section{section}
% line 1.
% \subsection{subsection}
% line 2.
% \subsubsection{subsub}
% line 3.
% \subsubsubsection{subsubsub}
% line 4.
% \subsubsubsubsection{subsubsubsub}
% line 5.
% \subsubsubsubsection{subsubsubsub}
% line 6.
% \subsubsubsection{subsubsub}
% line 7.
% \subsubsubsubsection{subsubsubsub}
% line 8.
% \subsubsubsubsection{testing}
% line 9.
% \end{document}
%

\newcounter{subsubsubsection}[subsubsection]
\def\subsubsubsectionmark#1{}
\def\thesubsubsubsection {\thesubsubsection
     .\arabic{subsubsubsection}}
\def\subsubsubsection{\@startsection
     {subsubsubsection}{4}{\z@} {-3.25ex plus -1
     ex minus -.2ex}{1.5ex plus .2ex}{\normalsize\bf}}
% mj02r: original:
%\def\l@subsubsubsection{\@dottedtocline{4}
%     {4.8em}{4.2em}}
% mj02r: for VCE reports:
%\def\l@subsubsubsection{\@dottedtocline{4}
%     {7em}{3.8em}}
% mj02r, 29/12/2004: for thesis:
\def\l@subsubsubsection{\@dottedtocline{4}
     {11.1em}{4.6em}}

\newcounter{subsubsubsubsection}[subsubsubsection]
\def\subsubsubsubsectionmark#1{}
\def\thesubsubsubsubsection {\thesubsubsubsection
     .\arabic{subsubsubsubsection}}
\def\subsubsubsubsection{\@startsection
     {subsubsubsubsection}{5} {\z@} {-3.25ex plus -1
     ex minus -.2ex}{1.5ex plus .2ex}{\normalsize\bf}}
% mj02r: original:
%\def\l@subsubsubsubsection{\@dottedtocline{5}
%     {5.8em}{5.2em}}
% mj02r: for VCE reports:
\def\l@subsubsubsubsection{\@dottedtocline{5}
     {10.7em}{4.5em}}

I will try to look at it later.

p.s. I hope it works :)

NichtJens
  • 103
  • 4
B. T.
  • 41