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.
Asked
Active
Viewed 9,301 times
3 Answers
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}

Similar redefinitions can be done for the other sectional units, if required.
Gonzalo Medina
- 505,128
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

Aditya
- 62,301
-
5After 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. – Gonzalo Medina Jun 02 '12 at 17:08
-
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.
Sveinung
- 20,355
-
-
1Code for
\sectionis added. Much more work than I anticipated. Use ofsecstyortitlesecis recommended. (Or useKOMA-scriptormemoir.) – Sveinung Jun 04 '12 at 13:07
subsubsectioninstead, while keepingsectionunchanged (assuming you don't otherwise use subsubsection). In the above,\sectionbecomes\subsubsectionand\thesectionbecomes\thesubsubsection. – PatrickT Aug 05 '20 at 00:21