21

The default style is section number and then section title, however I want it the other way around. Haven't been able to find a solution that doesn't give me strange errors, so any help would be appreciated.

3 Answers 3

23

You can use the titlesec package:

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

\titleformat{\section}{\normalfont\Large\bfseries}{}{0em}{#1\ \thesection}

\begin{document}

\section{A Test Section} 
\section{Another Test Section} 

\end{document}

enter image description here

Similar redefinitions can be done for the other sectional units, if required.

3
  • Exactly what i was looking for! Thank you all so much :)
    – olav
    Commented Jun 3, 2012 at 22:56
  • 1
    Omitting a section from numbering with * doesn't work using this method, is there an alternative to achieve this? Commented Oct 18, 2018 at 20:00
  • 1
    @JordanMackie, a quick workaround is to use Gonzalo's hack for subsubsection instead, while keeping section unchanged (assuming you don't otherwise use subsubsection). In the above, \section becomes \subsubsection and \thesection becomes \thesubsubsection.
    – PatrickT
    Commented Aug 5, 2020 at 0:21
8

In ConTeXt you can use

\setuphead[alternative=command, command=\swap]
\unexpanded\def\swap#1#2{#2\space#1}

\starttext
\startTEXpage[offset=5mm]
\chapter{A chapter}
\section{A section}
\subsection{A subsection}
\stopTEXpage
\stoptext

which gives

enter image description here

2
  • 5
    After seeing some of the answers that you have given using ConTeXt which I've also answered but using LaTeX, I am increasingly tempted to start studying ConTeXt seriously. Commented Jun 2, 2012 at 17:08
  • @GonzaloMedina: Definitely give it a shot.
    – Aditya
    Commented Jun 2, 2012 at 17:11
5

Here is an example (MWE) for the standard report-class. The code is copied from Vincent Zoonekynd and modified. The code for the book-class will be little different:

\documentclass{report}

\makeatletter
\def\@makechapterhead#1{%
  \vspace*{50\p@}%
  {\parindent \z@ \raggedright
    \normalfont
    \interlinepenalty\@M
    \Huge \bfseries #1 \quad
    \ifnum \c@secnumdepth >\m@ne
      \Huge\bfseries
      \thechapter
      \par\nobreak
    \fi
    \vskip 40\p@
  }}
\def\@makeschapterhead#1{%
  \vspace*{50\p@}%
  {\parindent \z@ \raggedright
    \normalfont
    \interlinepenalty\@M
    \Huge \bfseries  #1\par\nobreak
    \vskip 40\p@
  }}
\makeatother

\begin{document}
\chapter*{Test}
\chapter{Introduction}
\end{document}

And if you use article-class and only needs \section, here more code from the same source, slightly modified to suit your needs:

\documentclass{article}
\usepackage{lipsum}

\makeatletter
\def\section{\@ifstar\unnumberedsection\numberedsection}
\def\numberedsection{\@ifnextchar[%]
  \numberedsectionwithtwoarguments\numberedsectionwithoneargument}
\def\unnumberedsection{\@ifnextchar[%]
  \unnumberedsectionwithtwoarguments\unnumberedsectionwithoneargument}
\def\numberedsectionwithoneargument#1{\numberedsectionwithtwoarguments[#1]{#1}}
\def\unnumberedsectionwithoneargument#1{\unnumberedsectionwithtwoarguments[#1]{#1}}
\def\numberedsectionwithtwoarguments[#1]#2{%
  \ifhmode\par\fi
  \removelastskip
  \vskip 3ex\goodbreak
  \refstepcounter{section}%
  \hbox to \hsize{%
    \vtop{\parindent=0pt \leavevmode\Large\bfseries\raggedright #2\quad\thesection\par}%
    }
  \vskip 2ex\nobreak\noindent%
  \addcontentsline{toc}{section}{%
    \protect\numberline{\thesection}%
    #1}%
  \ignorespaces}
\def\unnumberedsectionwithtwoarguments[#1]#2{%
  \ifhmode\par\fi
  \removelastskip
  \vskip 3ex\goodbreak
  \hbox to \hsize{%
    \vtop{\parindent=0pt\leavevmode\Large\bfseries\raggedright #2\par}%
    }
  \vskip 2ex\nobreak\noindent%
  \addcontentsline{toc}{section}{%
    #1}%
  \ignorespaces}
\makeatother

\pagestyle{empty}
\begin{document}
\lipsum[1-2]
\section*{Introduction}
\lipsum[3-4]
\section{Introduction}
\lipsum[5-6]
\end{document}

In any case, it demonstrates clearly how much simpler life is due to titlesec and secstyle.

2
  • The question was about sections.
    – egreg
    Commented Jun 3, 2012 at 16:02
  • 1
    Code for \section is added. Much more work than I anticipated. Use of secsty or titlesecis recommended. (Or use KOMA-script or memoir.)
    – Sveinung
    Commented Jun 4, 2012 at 13:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .