388

I have a document which requires many levels of sectioning. I have sections, subsections and subsubsections, but require one more level below that. I can't change the sections to be parts and move everything up a level, as this document will eventually be included in another document which has parts/chapters already.

I see that the \paragraph command is used for defining the section level below subsubsection, but that doesn't produce headings in the same way that subsection and subsubsection do. Is there any way to either (1) change the \paragraph command so that it works like subsubsection but just adds another number - ie. 1.2.3.4 or (2) create a \subsubsubsection command to do the same thing?

robintw
  • 7,362
  • Related: http://tex.stackexchange.com/questions/30997/more-section-headings – egreg Jun 17 '12 at 22:23
  • There seems to be a problem with the first answer below (see comment on that answer) - it doesn't quite work. Does anyone have any ideas? I really need to sort the numbering for my report out soon before it goes to my supervisor! – robintw Jul 03 '12 at 17:29
  • 8
    What kind of document requires this many levels of headings? – mrf Jul 03 '12 at 20:48
  • 2
    @mrf One example: Documents a person writes for themselves to keep track of what they've learned. My thesis (master's) adviser teaches so much so much so fast - or I'm that slow - that I forget it quickly. The best way for me to retain what he taught is to type it up - which is how he advised me to work with him when we started. To quickly locate parts of the document, my subsubsections have subsections and subsubsections. Then I use package hyperref to create an index so I can find them quickly. But note that I type really fast (and can learn LaTeX somewhat fast). – Jeff Aug 16 '13 at 17:00
  • 5
    This seems like something quite a few people are into; is there any plan to develop a command which copies the style of a section level to a different level.

    \newsectionstyle[\subsubsection][4]{\subsubsubsection} \renewsectionstyle[\subsubsection]{\paragraph}

    or something along those lines. I understand that more powerful commands exist, but this specific feature seems to have gotten a large number of views and may be something to which a very basic user would like easy access.

    – kando Jul 17 '15 at 14:24
  • Does a simple KOMA-Script answer exist for this? The answer seems like it would be in Section 3.16 of the KOMA-Script documentation but I can't make heads or tails if there is an answer. – kando Jul 17 '15 at 15:04
  • @kando I've added a KOMA-Script answer. – Schweinebacke Apr 06 '19 at 17:22

7 Answers7

352

You can use the titlesec package to change the way \paragraph formats the titles and set the secnumdepth counter to four to obtain numbering for the paragraphs:

\documentclass{article}
\usepackage{titlesec}

\setcounter{secnumdepth}{4}

\titleformat{\paragraph}
{\normalfont\normalsize\bfseries}{\theparagraph}{1em}{}
\titlespacing*{\paragraph}
{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}

\begin{document}

\section{Test Section}
test
\subsection{Test Subsection}
test
\subsubsection{Test Subsubsection}
test
\paragraph{Test Modified Paragraph}
test

\end{document}

enter image description here

If you want to define a new sectioning command, you can take a look at Defining custom sectioning commands.

If you want to define a fresh new sectional unit below \subsubsection, but above \paragraph, then you will have to do considerably more work: a new counter has to be created and its representation has to be appropriately defined; the sectional units \paragraph and \subparagraph will also have to be redefined, as well as they corresponding \l@... commands (controlling how the will be typeset in the ToC if the tocdepth value is increased); also, the toclevel (for eventual bookmarks) will have to be considered.

Here's an example showing how to obtain this new sectional unit giving you now the option to use \part, \section, \subsection, \subsubsection, \subsubsubsection, \paragraph, and \subparagraph:

\documentclass{article}
\usepackage{titlesec}
\usepackage{hyperref}

\titleclass{\subsubsubsection}{straight}[\subsection]

\newcounter{subsubsubsection}[subsubsection]
\renewcommand\thesubsubsubsection{\thesubsubsection.\arabic{subsubsubsection}}
\renewcommand\theparagraph{\thesubsubsubsection.\arabic{paragraph}} % optional; useful if paragraphs are to be numbered

\titleformat{\subsubsubsection}
  {\normalfont\normalsize\bfseries}{\thesubsubsubsection}{1em}{}
\titlespacing*{\subsubsubsection}
{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}

\makeatletter
\renewcommand\paragraph{\@startsection{paragraph}{5}{\z@}%
  {3.25ex \@plus1ex \@minus.2ex}%
  {-1em}%
  {\normalfont\normalsize\bfseries}}
\renewcommand\subparagraph{\@startsection{subparagraph}{6}{\parindent}%
  {3.25ex \@plus1ex \@minus .2ex}%
  {-1em}%
  {\normalfont\normalsize\bfseries}}
\def\toclevel@subsubsubsection{4}
\def\toclevel@paragraph{5}
\def\toclevel@paragraph{6}
\def\l@subsubsubsection{\@dottedtocline{4}{7em}{4em}}
\def\l@paragraph{\@dottedtocline{5}{10em}{5em}}
\def\l@subparagraph{\@dottedtocline{6}{14em}{6em}}
\makeatother

\setcounter{secnumdepth}{4}
\setcounter{tocdepth}{4}

\begin{document}

\tableofcontents
\section{Test Section}
test
\subsection{Test Subsection}
test
\subsubsection{Test Subsubsection}
test
\subsubsubsection{Test Subsubsubsection}
test
\paragraph{Test Paragraph}
test
\subparagraph{Test Subparagraph}
test

\end{document}

enter image description here

Gonzalo Medina
  • 505,128
  • Thanks for this, I've been using it and have only just realised that the section numbering for \subsubsubsection doesn't seem to restart after every \subsubsection. That is, I get 1.1.1, 1.1.1.1, 1.1.1.2, 1.1.2, 1.1.2.3 - where the last one should be 1.1.2.1. Do you have any ideas how to fix this? – robintw Jul 02 '12 at 15:52
  • 7
    @robintw You have to say \newcounter{subsubsubsection}[subsubsection] so that the new counter is bound to the subsubsection counter. – egreg Jul 03 '12 at 17:34
  • I applied the first solution (with \paragraph) - it works great, only in the ToC, it looks like this - do you know how to make the alignment match? – Emanuel Berg Dec 31 '12 at 00:32
  • @GonzaloMedina Did you notice \subsubsubsection count not reset when starting a new section, subsection, etc? Seems also to have been thematized in comments above mine here. – Speravir Mar 05 '14 at 00:03
  • @Speravir thanks for the reminder. I've fixed it now. – Gonzalo Medina Mar 05 '14 at 13:17
  • 1
    I like the solution a lot! This should be a package with options, such as [compact] of the titlesec package (since it is a required package). How do I reference the new subsubsubsection? I use the cleveref package and it can't determine the label type ("section"), so it produces two ?? instead of "section 1.1.1.1". – Erik Jun 24 '14 at 14:03
  • Concerning the 'fresh new' \subsubsubsection, I think you forgot #1 in the last argument of \titleformat{\subsubsubsection}{...}{...}{1em}{}. – Bart Michels Feb 05 '15 at 21:23
  • @barto No, since i am not using the explicit option for titlesec. – Gonzalo Medina Feb 06 '15 at 00:16
  • @GonzaloMedina thanks a lot , my issue is that the subsubsection and subsubsubsection are aligned with the same margin , i'm using report as document class .... this worked fine when using article but i want it with report ... please view the image that describe my problem here : http://s3.postimg.org/wzzluctj7/margin_Section.png can you help me to fix that – The Beast Sep 12 '15 at 01:11
  • 2
    @Frankenstein Change the \def\l@subsubsubsection{...} lines to \def\l@subsubsubsection{\@dottedtocline{4}{9em}{4em}} \def\l@paragraph{\@dottedtocline{5}{12em}{5em}} \def\l@subparagraph{\@dottedtocline{6}{15em}{6em}} choose the second and third arguments appropriately. – Gonzalo Medina Sep 12 '15 at 01:52
  • @GonzaloMedina , perfect it's working now , can you explain what this line \def\l@subsubsubsection{@dottedtocline{4}{9em}{4em}} does , i think it defines the margin for each section , subsection etc. thnaks – The Beast Sep 12 '15 at 02:23
  • 2
    @Frankenstein \l@<name> is the command in charge of typesetting entries of type <name> in a list such as the ToC, the LoF, the LoT. \@dottedtocline is a kernel command producing the actual entry format (number, title/caption, leading dots, page number). First argument: level for <name>; second argument indentation from the left margin; third argument: space reserved for the number. – Gonzalo Medina Sep 12 '15 at 02:32
  • @GonzaloMedina is it just me or does the counter still not reset for numbered paragraphs? 3.1.3.2.4 does not reset to 3.1.3.3.1 Sadly because I got really excited that this code might be perfect for what I need. – skrat Jan 15 '16 at 21:34
  • 1
    I tried this, but it suppresses all section numbers throughout my document! Could it be related to the specific document class I'm using for a journal paper? It's one from Springer: \documentclass[smallextended]{svjour3}. – dinosaur Feb 13 '17 at 23:21
  • It seems to create a clash with the algorithm package, what can I do to deal with that as far as I need both such a subsubsubsection visible on the table of contents and some algorithms ? Will the titlesec option create subsubsubsection visible in it ? – Revolucion for Monica Jul 31 '17 at 15:49
  • @dinosaur same for me. The solution does not work as expected. – user1511417 Aug 05 '17 at 11:04
  • How to remove the indentation of the subparagraph in this solution? – jibo Apr 22 '18 at 13:00
  • My bookmark for paragraph and subparagraph are at the same level, I tried bookmarksdepth=7 in hyperref setup, doesn't have any effect. – Mzq Mar 25 '19 at 11:34
  • @Erik \crefname{subsubsubsection}{section}{sections} – Mzq May 20 '19 at 12:08
  • @GonzaloMedina Why is this code changing the space after the "section" "subsection" and others ? I think the line that modify that kind of format is \titlespacing* but you're only using that command with subsubsubsection... so I don't know why others are changing their vertical space after the title too – EmiliOrtega Jul 22 '20 at 15:55
87

Here's a solution that doesn't require the use of a specialized package such as titlesec or sectsty. (There's nothing wrong per se, obviously, with using packages to achieve a certain goal; nevertheless, I think it can be instructive at times to see how one can manipulate some of LaTeX's built-in commands directly.)

If you use the article document class, the default appearance of the output of the commands \subsubsection and \paragraph is set up as follows:

\newcommand\subsubsection{\@startsection{subsubsection}{3}{\z@}%
                {-3.25ex\@plus -1ex \@minus -.2ex}%
                {1.5ex \@plus .2ex}%
                {\normalfont\normalsize\bfseries}}
\newcommand\paragraph{\@startsection{paragraph}{4}{\z@}%
                {3.25ex \@plus1ex \@minus.2ex}%
                {-1em}%
                {\normalfont\normalsize\bfseries}}

To make the \paragraph command behave more like the \subsubsection command, but with less vertical spacing above and below the sectioning header line(s), you could modify the \paragraph command to make its output behave as if it were a "subsubsubsection". The following MWE illustrates a possible setup.

\documentclass{article}
\makeatletter
\renewcommand\paragraph{\@startsection{paragraph}{4}{\z@}%
            {-2.5ex\@plus -1ex \@minus -.25ex}%
            {1.25ex \@plus .25ex}%
            {\normalfont\normalsize\bfseries}}
\makeatother
\setcounter{secnumdepth}{4} % how many sectioning levels to assign numbers to
\setcounter{tocdepth}{4}    % how many sectioning levels to show in ToC

\begin{document}
\tableofcontents
\section{A}
\subsection{B}
\subsubsection{C1}
\paragraph{D1}
\paragraph{D2}
\subsubsection{C2}
\end{document}

enter image description here

Mico
  • 506,678
  • Could you please tell us how we can make the font, say, italic? – Herpes Free Engineer Mar 14 '18 at 20:36
  • 1
    @KutalmisBercin - Should it be "just" italic, or should it be bold and italic? Please advise. – Mico Mar 14 '18 at 20:42
  • Just italic, if possible. – Herpes Free Engineer Mar 14 '18 at 20:45
  • 3
    @KutalmisBercin - In the \renewcommand instruction, change \bfseries to \itshape. – Mico Mar 14 '18 at 20:55
  • 1
    Don't how to make an extra vote up; hence, many thanks. – Herpes Free Engineer Mar 14 '18 at 21:03
  • I wish we could give more than one vote for an extraordinary answer. Thank you so much for solving this issue. – Jonathan E. Landrum Jul 28 '20 at 17:57
  • 1
    @JonathanLandrum - Many thanks for your very nice compliment! It's much appreciated. :-) – Mico Jul 28 '20 at 18:04
  • This answer does not work. It gives an undefined control sequence error. – Toma Jul 18 '21 at 08:55
  • @Toma - I apologize profusely for the fact that my mind-reading skills are simply non-existent. Unless you tell me which control sequence is undefined, I am in no position to offer any kind of advice on how to fix the issue you say you've encountered. FWIW, if I compile the code above, which I posted more than 9 years ago, with a current TeX distribution (MacTeX2021, to be specific), I experience no problems whatsoever. – Mico Jul 18 '21 at 09:04
  • No need, the answer below works fine. Your answer might well be dated. Just letting people know it is. – Toma Jul 18 '21 at 09:16
  • @Toma - Your assertions that my answer "does not work" and "might well be dated" are both dead wrong. Just letting people know how weak and unfounded your assertions are. – Mico Jul 18 '21 at 09:31
23

I know this is an old question, but I found it with Google and I think the solutions are too complicated.

For me this is the easiest way for a subsubsubsection:

\newcommand{\subsubsubsection}[1]{\paragraph{#1}\mbox{}\\}
\setcounter{secnumdepth}{4}
\setcounter{tocdepth}{4}

After this it's possible to use

\subsubsubsection{Navigator}
Daniel
  • 375
  • 13
    That's not really a good solution. It does not provide an optional argument for \subsubsubection but allows a page break immediately after the heading. Do not use this! Instead use one of the solutions from the answers above, if your class does not provide its own interface for defining such commands. The solutions are less complicated than you may think. – Schweinebacke Apr 13 '17 at 10:59
  • 3
    Nevertheless, for me this solution worked better than either of those above. The solution by Gonzalo Medina caused side effects like removing numbering from the PDF table of contents. I was already using paragraphs and wanted them to retain the default formatting, so Mico's solution was not ideal. Furthermore the mentioned deficiencies of Daniel's solution didn't apply: I wasn't using the optional argument of \subsubsubsection, and I was already having to work around the fact that the default heuristics to avoid page breaks after headings don't work reliably. – Daira-Emma Hopwood Jul 13 '17 at 10:10
  • 1
    @Daira I think if you simply change \setcounter{tocdepth}{6}, paragraphs will be numbered in the table of contents. I don't think this is a flaw of Gonzalo Medina's solution, you just need to change a few numbers in his code. – Mzq Feb 13 '19 at 07:21
  • Maybe not most elegant, but practical. Thanks – DataBach Mar 11 '20 at 14:28
  • 1
    @Horbaje - Any purported solution that permits serious typographic malpractice -- such as allowing a page break immediately after the sectioning header, without issuing even a whimper of a warning -- should not be labelled "practical". "Dangerous" and "asking for trouble" would be more suitable adjectives, for sure. – Mico Jul 28 '20 at 18:08
  • That works for me. Tnx. – Adel Apr 09 '21 at 06:07
  • It works, when the other answers don't. I don't know what the problem of the people commenting against this solution is. Many thanks – Toma Jul 18 '21 at 09:00
  • 2
    @Toma - As with the comment you left below my answer, your claim that "it works, when the other answers don't" is not helpful. Your condescending addition, "I don't know what the problem of the people commenting against this solution is", merely demonstrates that you've made no noticeable effort to understand the serious drawbacks of the \newcommand{\subsubsubsection}[1]{\paragraph{#1}\mbox{}\\} approach. – Mico Jul 18 '21 at 09:23
15

The are two good answers to show how to add a new level section or modify an existing one. But both are assuming some basic knowledge of LaTeX and typography. Maybe these remarks can help to new users to decide when these or a similar approaches are the best solution.

requires many levels of sectioning

The best solution could be reconsider that premise. Is it really true? Sometimes (e.g., legal documents, huge technical reports), but often is not an imperative requirement but the insane decision of mimic this or that monstrous thesis. Defaults levels are more than enough in most documents.

I have sections, subsections and subsubsections ...

I see that the \paragraph command is used ...

It seems that you are using the article class, because you mention only this four heading levels, so the first question is
How many levels of nested subsections can the article class support? Short anwser: there are six, not four levels of sectioning.

Moreover, the book-like classes than allow one more level (\chapter), so you can have at least seven levels (-1 to 5, not 1 to 7) without effort. Using the memoir class you have also the option that chapters behave as sections:

\documentclass[article,oneside]{memoir}
\setcounter{secnumdepth}{5} % Note that part is -1 level !
\setcounter{tocdepth}{5}
\begin{document}
\begingroup
\let\clearpage\relax
\let\newpage\relax
\tableofcontents*
\part{Part}
\endgroup
\chapter{Chapter} Text. 
\section{Section} Text.
\subsection{Subsection} Text.
\subsubsection{Subsubsection} Text.
\paragraph{Paragraph} Text.
\subparagraph{Subparagraph} Text.
\end{document}

Need more? For a deeper structuring of your contents you can use also the starred versions of sectioning commands (\subsection*, etc.), environment lists (enumerate, itemize,description, or a custom list) and a judicious use of blank lines (\par) to remark the content structure (often some people break paragraphs only to avoid long chunks of texts).

Still need More section headings? Well, it's up to you. Then go to other answers, or follow the last link for a ridiculously high number of sectional levels.

Fran
  • 80,769
  • 4
    Just to offer an actual example that I have laying around in my computer, please refer to section 17.5.16.3.2.2.1. (Continuous Phase) of the ANSYS Fluent Theory Guide =) – JorgeGT Mar 27 '18 at 20:20
12

This very excellent answer can also very easily be expanded to add \subsubsubsection and \subsubsubsubsection commands:

screenshot

\documentclass{article}

\setcounter{secnumdepth}{5} \setcounter{tocdepth}{5}

\makeatletter \newcommand\subsubsubsection{@startsection{paragraph}{4}{\z@}{-2.5ex@plus -1ex @minus -.25ex}{1.25ex @plus .25ex}{\normalfont\normalsize\bfseries}} \newcommand\subsubsubsubsection{@startsection{subparagraph}{5}{\z@}{-2.5ex@plus -1ex @minus -.25ex}{1.25ex @plus .25ex}{\normalfont\normalsize\bfseries}} \makeatother

\begin{document}

\tableofcontents

\section{section} \subsection{subsection} \subsubsection{subsubsection} \subsubsubsection{subsubsubsection} \subsubsubsubsection{subsubsubsubsection}

\end{document}

finefoot
  • 529
  • 5
  • 15
11

With KOMA-Script classes you do not need nor should use an extra package to redefine sectioning commands or define new sectioning levels. And you don't need and should not use low level code.

These classes provide command \RedeclareSectionCommand to reconfigure existing sectioning commands. For example to have a \paragraph that does not produce a title at the beginning of the paragraph (a so called catch phrase or runin title) but a displayed title, you can use:

\RedeclareSectionCommand[runin=false,afterskip=1ex,afterindent=false]{paragraph}

Option runin=false is used to make it a displayed instead of a runin title. afterskip=1ex makes a vertical skip of 1ex after the heading. afterindent=false prevents the following paragraph to be indented.

So the result would be something like:

enter image description here

If you need a paragraph number, just add

\setcounter{secnumdepth}{\paragraphnumdepth}

very ease with KOMA-Script. You don't need to remember the numeric value.

You can do a similar change for \subparagraph:

\RedeclareSectionCommand[indent=0pt,runin=false,afterskip=1ex,afterindent=false]{subparagraph}

Here additionally indent=0pt is used to remove the default indent of subparagraph titles.

If this is not enough, you can even define a \subsubparagraph very easily:

\DeclareNewSectionCommand[style=section,level=\numexpr \subparagraphnumdepth+1\relax,runin=false,beforeskip=1ex,afterskip=0pt,afterindent=false]{subsubparagraph}

enter image description here

See the KOMA-Script manual for the meaning of all the options and how to use them and all the other available options.

BTW: After defining \subsubparagraph there is also a \subsubparagraphnumdepth (and even a \subsubparagraphtocdepth) that can be used to change secnumdepth.

Schweinebacke
  • 26,336
  • If you're using the KOMA class, you could use the jura package which provides for great flexibility in sub-sectioning. – user26732 Aug 14 '21 at 08:53
  • @user26732 Which jura package do you mean. I know the jura class. I also know scrjura, but that one does not support sub-sectioning but clauses. – Schweinebacke Aug 23 '23 at 09:11
  • Jura class, not package. – user26732 Aug 24 '23 at 16:20
  • @user26732 But jura class is not a KOMA-Script class nor a class from KoMa. And it also does not provide most of the features of a KOMA-Script class. So I still don't understand your comment. Maybe you should just add one more answer showing, why using jura class could be a suggestion. – Schweinebacke Sep 01 '23 at 09:36
1

This solution might help other folks looking for a quick walkaround:

\documentclass[12pt,letterpaper]{report}

\usepackage{titlesec}
\setcounter{secnumdepth}{4}

\titleformat{\chapter}[hang]
{\normalfont\bf\large\centering\singlespace}{\chaptertitlename\ \thechapter}{14pt}{}
\titlespacing{\chapter}{0pt}{-35pt}{0pt}{}

\titleformat{\section}[hang] {\normalfont\bf\em}{\thesection}{12pt}{} \titlespacing{\section}{0pt}{0pt}{0pt}{}

\titleformat{\subsection}[hang] {\normalfont\em}{\thesubsection}{12pt}{} \titlespacing{\subsection}{0pt}{0pt}{0pt}{}

\titleformat{\subsubsection}[hang] {\normalfont\em}{\thesubsubsection}{12pt}{} \titlespacing{\subsubsection}{0pt}{0pt}{0pt}{}

\end{document}

Baraa
  • 11