6

Consider

\documentclass{article}
\usepackage{titlesec}

\def\bl#1 #2 {#1\textsuperscript{#2}\,}
\titleformat{\section}[runin]{}{}{0pt}{}[. ]
%\titleformat{\section}[runin]{}{}{0pt}{\bl}[. ]

\begin{document}
\section{3 a 21}
main text.
\end{document}

The intention of the line commented out was to produce the æquivalent of \section{\bl 3 a 21} without having to type \bl for each section; but it produced an error. Can something be done to achieve the intended result?

5

You have to capture the argument before passing it to \bl. Below we capture it using \@bl before passing it to \bl:

enter image description here

\documentclass{article}
\usepackage{titlesec}

\makeatletter
\def\@bl#1{\bl#1}
\def\bl#1 #2 {#1\textsuperscript{#2}\,}
\titleformat{\section}[runin]{}{}{0pt}{\@bl}[. ]
\makeatother

\begin{document}
\section{3 a 21}
main text.
\end{document}
  • because the bracketed expression is read as one argument though it has spaces? – Toothrot Feb 9 '16 at 7:49
  • 1
    @Lawrence: It's read as one argument because it is placed inside a group {..}. So using an auxiliary function like \@bl allows you to grab the entire group and pass that to another function \bl, stripping the group, so it can use your delimited requirement. – Werner Feb 9 '16 at 14:19
4

You can do it also without any package. The trick is that the argument to \section is passed as a braced group to the final part of the code, so if that part ends with a macro taking an argument, it will see the section title; but you first need to remove the braces. Note that defining the inner macro with three arguments we can easily add the final period.

\documentclass{article}
\usepackage{showframe}% just for the example

\makeatletter
\renewcommand{\section}{%
  \@startsection{section}{1}{\z@}%
    {-3.5ex \@plus -1ex \@minus -.2ex}%
    {-1em}%
    {\normalfont\process@section@title}%
}
\newcommand{\process@section@title}[1]{\process@section@title@aux#1\@nil}
\def\process@section@title@aux#1 #2 #3\@nil{%
  #1\textsuperscript{#2} #3.%
}
\makeatother

\setcounter{secnumdepth}{0}

\begin{document}

\section{3 a 21}
Some text for the section.

\section{4 a 42}
Some text for the section.

\end{document}

enter image description here

  • Nice. can you also eliminate the double baselineskip? – Toothrot Feb 9 '16 at 10:45
  • 1
    @Lawrence The space above the section title is governed by the -3.5ex \@plus -1ex \@minus -.2ex part; change it to your liking. – egreg Feb 9 '16 at 10:46
2

This might qualify as "dirty":

\documentclass{article}
\usepackage{titlesec}

\def\bl#1 #2 {#1\textsuperscript{#2}\,}
\titleformat{\section}[runin]{}{}{0pt}{}[. ]
\let\oldsect=\section
\def\section#1{\oldsect{\bl#1}}
\begin{document}
\section{3 a 21}
main text.
\end{document}

3a 21. main text.

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.