# How to have an amsart-like subsection style

If you use the amsart class, then it puts a period after the subsection title, but not if there is no title. For example, if I write

\subsection{Title}
Blah1
\subsection{}
Blah2


Then amsart produces

0.1. Title. Blah1

0.2. Blah2

I tried to achieve this in the article class using titlesec package, but so far I can make the period always appear or never appear. So, with the same coding as above, I get either

0.1. Title. Blah1

0.2. . Blah2

(notice the extra period after 0.2.) or

0.1. Title Blah1

0.2. Blah2

Does anyone know how to have the period appear after the subsection title only when there is actually a title?

-

In the following example I used the ifmtarg package to define a user command, with the help of \@ifmtarg, for testing for an empty macro argument (zero or more spaces will be handled); the explicit option for titlesec was used to have control over the title using #1; the test for emptyness is performed inside \titleformat:

\documentclass{article}
\usepackage{ifmtarg}
\usepackage[explicit]{titlesec}

\makeatletter
\newcommand\titleempty[1]{\@ifmtarg{#1}{}{#1.}}
\makeatother

\titleformat{\subsection}[runin]
{\normalfont\large\bfseries}{\thesubsection}{1em}{\titleempty{#1}}

\begin{document}

\subsection{Test Section} test text
\subsection{ } test text

\end{document}


-
Thank you, I never new there was a package like ifmtarg. Good to know! –  user1000 May 9 '12 at 20:16

The following is possible using titlesec's explicit option:

\documentclass{article}
\usepackage[explicit]{titlesec}% http://ctan.org/pkg/titlesec
\makeatletter
\titleformat{\subsection}[runin]{\large\bfseries}{}{0pt}{%
\setbox0=\hbox{\ignorespaces#1\unskip}%
}
\makeatother
\begin{document}
\section{Title}
\subsection{Title} Blah
\subsection{} Blah
\end{document}


explicit allows you to use #1 to place the argument to \subsection. However, it also removes easy use of the *-variant from the mix. Not sure whether that's a requirement.

The above sets the \subsection title in box 0, tests for its width (in order to see whether it's empty or not, and sets the title with a period if its not empty. As such, ending the title with a period will, of course, result in a double period. Here \@addpunct would help.

Here's another alternative using etoolbox:

\documentclass{article}
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\makeatletter
\patchcmd{\@sect}% <cmd>
{#8}% <search>
{\setbox0=\hbox{\ignorespaces#8\unskip}\usebox0\ifnum2=#2\ifdim\wd0>\z@.\fi\fi}% <replace>
{}{}% <success><failure>
\makeatother
\begin{document}
\section{Title}
\subsection{Title} Blah
\subsection{ } Blah
\end{document}


It follows the same approach as above, but directly fixes the ultimate sectioning call. The condition \ifnum#2=2 refers to the \subsection level (level 2). For all other sections, no punctuation is added.

-
If, for some reason, one has one or more blank spaces as the argument of \subsection, your code won't produce the desired result. –  Gonzalo Medina May 1 '12 at 2:43
@GonzaloMedina: Thanks, I hope I fixed it now. –  Werner May 1 '12 at 2:49
Thank you, it works! –  user1000 May 9 '12 at 20:14