TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 5 down vote accepted

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}
share|improve this answer
    
because the bracketed expression is read as one argument though it has spaces? – Lawrence Feb 9 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 at 14:19

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

share|improve this answer
    
Nice. can you also eliminate the double baselineskip? – Lawrence Feb 9 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 at 10:46

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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