# How to make different command for subparagraph (or any other sectioning) with empty and nonempty title?

I want to make a sectioning command (for example subparagraph) that will have a different behavior for command with empty title.

This is my MWE, showing somewhat about i want to get:

\documentclass{article}
\setcounter{secnumdepth}{7}

\makeatletter
\renewcommand\subparagraph{\@startsection{subparagraph}{5}{\parindent}%
{3.25ex \@plus1ex \@minus .2ex}%
{1.5ex \@plus .2ex}%
{\normalfont\normalsize\bfseries}}
\makeatother

\begin{document}

\subparagraph{Subparagraph with title}

Some random text

\makeatletter
\renewcommand\subparagraph{\@startsection{subparagraph}{5}{\parindent}%
{3.25ex \@plus1ex \@minus .2ex}%
{-1em}%
{\normalfont\normalsize\bfseries}}
\makeatother

\subparagraph{} Some random text for subparagraph without title

\end{document}


I looked at the answer for this question: \section with empty title. Show number next to first paragraph

It does the same thing as I want, but can I get the same result without titlesec?

• Without titlesec? But you didn't load it. – Bernard Aug 18 '19 at 13:30
• @Bernard Edited the mistake in the question. – Shalom Alecheim Aug 18 '19 at 14:15

As far as I know, \subparagraph{} isn't the normal way of using LaTeX's \subparagraph command, so it may be a good idea to give your command a different name. I am going to answer with the \subparagraph name anyway, as it is what was asked in the question. I propose two methods, both using xparse in order to easily grab and analyze all possible arguments of \subparagraph (with or without star, with or without the optional argument inside square brackets).

# Using xparse and etoolbox (“LaTeX2e style”)

This method uses xparse and etoolbox. Additionally, it exploits the fact that \@startsection's fifth argument will necessarily be expanded when TeX uses it. If you want a blank mandatory argument of your \subparagraph to be handled the same way as an empty one, just replace \ifstrempty with \ifblank; then, \subparagraph{ } and \subparagraph{} will have same behavior.

\documentclass{article}
\usepackage{xparse}
\usepackage{etoolbox}
\usepackage{lipsum}

\setcounter{secnumdepth}{7}

\makeatletter

\newcommand*{\alecheim@subparagraph}[1]{%
\@startsection{subparagraph}{5}{\parindent}%
{3.25ex \@plus1ex \@minus .2ex}%
% The following argument will be expanded during <glue> assignments
{\ifstrempty{#1}{-1em}{1.5ex \@plus .2ex}}
{\normalfont\normalsize\bfseries}%
}

\RenewDocumentCommand{\subparagraph}{sO{#3}m}{%
\IfBooleanTF{#1}
{\alecheim@subparagraph{#3}*{#3}}
{\alecheim@subparagraph{#3}[#2]{#3}}%
}

\makeatother

\begin{document}

\subparagraph{Subparagraph with title}

\lipsum[1][1-2]

\subparagraph{} Some random text for subparagraph without title.

\end{document}


# Using xparse and expl3 (“expl3 style”)

The following method uses techniques from expl3, the language from the LaTeX3 project. It could but doesn't rely on the fact that the fifth argument of \@startsection will automatically be expanded at some point; instead, we do the required expansion ourselves in the following line, which stores the result in \l_tmpa_tl:

\tl_set:Nx \l_tmpa_tl { \tl_if_empty:nTF {#3} { -1em } { 1.5ex \@plus .2ex } }


Then we use a V argument type with \alecheim_subparagraph:V \l_tmpa_tl in order to pass \alecheim_subparagraph:n the value of \l_tmpa_tl. Here is the full example:

\documentclass{article}
\usepackage{xparse}
\usepackage{lipsum}

\setcounter{secnumdepth}{7}

\makeatletter
\ExplSyntaxOn

\cs_new_protected:Npn \alecheim_subparagraph:n #1
{
\@startsection { subparagraph } { 5 } { \parindent }
{ 3.25ex \@plus 1ex \@minus .2ex }
{#1}
{ \normalfont \normalsize \bfseries }
}

\cs_generate_variant:Nn \alecheim_subparagraph:n { V }

\RenewDocumentCommand { \subparagraph } { s O{#3} m }
{
% You may want to replace \tl_if_empty:nTF with \tl_if_blank:nTF here.
\tl_set:Nx \l_tmpa_tl
{ \tl_if_empty:nTF {#3} { -1em } { 1.5ex \@plus .2ex } }
\IfBooleanTF {#1}
{ \alecheim_subparagraph:V \l_tmpa_tl * {#3} }
{ \alecheim_subparagraph:V \l_tmpa_tl [#2]{#3} }
}

\ExplSyntaxOff
\makeatother

\begin{document}

\subparagraph{Subparagraph with title}

\lipsum[1][1-2]

\subparagraph{} Some random text for subparagraph without title.

\end{document}


The output is the same as above.