[Tex/LaTex] Adding section number after section title with titlesec package

numberingsectioningtitlesec

I am having difficulty adding the section number after the section title. Normally the order is section number followed by section title. I want it to be the opposite. I am looking for what I should add to the following .tex file code:

\usepackage[compact]{titlesec}
\titleformat{\section}[runin]{\large\bfseries}{\thesection}{}{}

Best Answer

You need to use the explicit package option with titlesec. It enables you to "explicitly" state where the section title is printed using #1 to denote the title. In this case, we place the title code in the <before code> section of \titleformat:

enter image description here

\documentclass{article}
\usepackage[compact,explicit]{titlesec}% http://ctan.org/pkg/titlesec
\begin{document}
\titleformat{\section}[runin]{\large\bfseries}{}{0pt}{#1\quad\thesection}
\section{First section}
\section{Second section}
\section{Last section}
\end{document}

The general structure of title formatting provided by titlesec is via

\titleformat{<command>}[<shape>]{<format>}{<label>}{<sep>}{<before-code>}[<after-code>]

Since the <label> is no longer needed, it is explicitly inserted in <before-code> together with (after) the section title #1. I've placed \quad between the two components, but you can modify this to your like, using (say) \hspace{<len>} where <len> is any recognized (La)TeX length.

I kept your other settings for the sake of convenience since you didn't provide a complete MWE.

Related Question