[Tex/LaTex] Subsubsection: remove the newline (like paragraph)

formattingsectioningsections-paragraphs

Using the article document class, how can one remove the newline at the end of a subsubsection title?

I am using \documentclass{article}; I do not currently use titlesec or any of the alternatives; I do use fancyhdr.

Best Answer

Without any additional packages, you need to update the fifth argument to \@startsection and make it negative - implicitly passed with a call to \subsubsection:

enter image description here

\documentclass{article}
\begin{document}
\section{A section} Some text
\subsection{A subsection} Some text
\subsubsection{A subsubsection} Some text

\makeatletter
\renewcommand\subsubsection{\@startsection{subsubsection}{3}{\z@}%
                                     {-3.25ex\@plus -1ex \@minus -.2ex}%
                                     {-1.5ex \@plus -.2ex}% Formerly 1.5ex \@plus .2ex
                                     {\normalfont\normalsize\bfseries}}
\makeatother

\section{A section} Some text
\subsection{A subsection} Some text
\subsubsection{A subsubsection} Some text
\end{document}

This may seem like magic... that is, why the fifth argument, and why does it have to be negative. To see why, you have to follow the chain of execution within the LaTeX kernel. The relevant code addressing this is contained within \@startsection and \@sect:

\def\@startsection#1#2#3#4#5#6{%
  \if@noskipsec \leavevmode \fi
  \par
  \@tempskipa #4\relax
  \@afterindenttrue
  \ifdim \@tempskipa <\z@
    \@tempskipa -\@tempskipa \@afterindentfalse
  \fi
  \if@nobreak
    \everypar{}%
  \else
    \addpenalty\@secpenalty\addvspace\@tempskipa
  \fi
  \@ifstar
    {\@ssect{#3}{#4}{#5}{#6}}%
    {\@dblarg{\@sect{#1}{#2}{#3}{#4}{#5}{#6}}}}
\def\@sect#1#2#3#4#5#6[#7]#8{%
  \ifnum #2>\c@secnumdepth
    \let\@svsec\@empty
  \else
    \refstepcounter{#1}%
    \protected@edef\@svsec{\@seccntformat{#1}\relax}%
  \fi
  \@tempskipa #5\relax
  \ifdim \@tempskipa>\z@
    \begingroup
      #6{%
        \@hangfrom{\hskip #3\relax\@svsec}%
          \interlinepenalty \@M #8\@@par}%
    \endgroup
    \csname #1mark\endcsname{#7}%
    \addcontentsline{toc}{#1}{%
      \ifnum #2>\c@secnumdepth \else
        \protect\numberline{\csname the#1\endcsname}%
      \fi
      #7}%
  \else
    \def\@svsechd{%
      #6{\hskip #3\relax
      \@svsec #8}%
      \csname #1mark\endcsname{#7}%
      \addcontentsline{toc}{#1}{%
        \ifnum #2>\c@secnumdepth \else
          \protect\numberline{\csname the#1\endcsname}%
        \fi
        #7}}%
  \fi
  \@xsect{#5}}

In the above code, keep the focus on the fifth argument, denoted by #5 in \@startsection. Although nothing happens to it inside \@startsection, it passes #5 also as the fifth argument to \@sect. So, inside \@sect, #5 is the length stored in \@tempskipa and subsequently evaluated using

\ifdim \@tempskipa>\z@
%... \@tempskipa (or #5) is positive
\else
%... \@tempskipa (or #5) is negative
\fi

The evaluation references \z@ or 0pt (see What does \z@ do?) and therefore designates a choice between \@tempskipa being positive or negative. The construction inside \else is what you're after, since it does not include a \@@par (or line/paragraph break). As a comparison, see the definition of \paragraph:

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

Note that argument #5 is negative (-1em).

A brief summary of the above discussion is included in the kernel documentation, also covered in Where can I find help files or documentation for commands like \@startsection for LaTeX?

Using packages like titlesec removes this magic in a more user-friendly way. For an example, see Subsection starting in same line as subsection name.