5

I want to have subsections appear like "Article 1: Foo" when I enter \subsection{Foo}, but like "Article 1" (without the colon) when I enter \subsection{} (without a title). Is there a way to do this?

Here's a MWE of what I have now:

\documentclass{article}

\usepackage{titlesec}

\newcounter{artikel}
\setcounter{artikel}{1}

\renewcommand{\thesubsection}{\arabic{artikel}}
\titleformat{\subsection}{\large\bfseries}{}{0pt}{Article \thesubsection: \refstepcounter{artikel}}

\begin{document}

\subsection{Foo}
\subsection{}

\end{document}

Which produces

Article 1: Foo

Article 2:

But I want it to produce:

Article 1: Foo

Article 2

  • Should be possible, but the implementation depends on the document class – user31729 Feb 9 '16 at 16:00
  • @ChristianHupfer I am using the article class. I added a MWE for clarity. – LSchoon Feb 9 '16 at 16:09
  • The usual subsection numbers (in front of the subsection title) should vanish and be replaced by Article ....? – user31729 Feb 9 '16 at 16:33
  • @ChristianHupfer That's right, with the number after Article being the subsection number (or rather another predefined counter, as I want the subsection nubers to increase throughout several sections). I just realized this last point might be important for the answer, will edit the MWE. – LSchoon Feb 9 '16 at 16:37
  • Perhaps titlesec is the easier option here – user31729 Feb 9 '16 at 16:49
4

You can do that withetoolbox and the explicitoption of titlesec. Also note that if you want to use starred subsections, you can have a different formatting with the numberless key:

\documentclass{article}

\usepackage[explicit]{titlesec}
\usepackage{etoolbox}
\newcounter{artikel}
\setcounter{artikel}{1}

\renewcommand{\thesubsection}{\arabic{artikel}}
\titleformat{\subsection}{\large\bfseries}{}{0pt}{Article \thesubsection\notblank{#1}{:~}{}#1\refstepcounter{artikel}}

\begin{document}

    \subsection{Foo}
    \subsection{ }

\end{document} 

enter image description here

  • Thanks for your answer! This nearly works, but now the subsection title ('Foo' in the example) drops out. It can be replaced by changing the last argument of \titleformat to Article \thesubsection\notblank{#1}{:}{} #1\refstepcounter{artikel} – LSchoon Feb 9 '16 at 18:43
  • Oops! You're right, I had forgotten to add #1, as required by the explicit option. It's corrected now. Thanks for pointing it. – Bernard Feb 10 '16 at 1:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.