I’ve been using the following code snipped (I can’t remember where I found it) to put right-justified section numbers into the left margin:
\makeatletter
\def\@seccntformat#1{\protect\makebox[0pt][r]{\csname
the#1\endcsname\hspace{6pt}}} \makeatother
However, I've started using titlesec
to customise the layout of section headings, and the code above no longer works for any section heading styles I've defined using titlesec
. For example, here I define \titleformat
for subsections, but not sections or subsubsections:
This question shows how to put section numbers into the left margin using \titlesec
, but with left-justified number placement. How do I do this while retaining the right-justified format of the \@seccntformat
definition above?
\documentclass{article}
\usepackage{titlesec}
\usepackage{lipsum}
\makeatletter
\def\@seccntformat#1{\protect\makebox[0pt][r]{\csname
the#1\endcsname\hspace{6pt}}} \makeatother
\titleformat{\subsection}{\normalfont\bfseries}{\thesubsection}{1em}{}
\titlespacing*{\subsection}{0pt}{*3.25}{*1.5}%
\renewcommand*{\thesubsection}{\arabic{subsection}}
\begin{document}
\section{A section}
\subsection{A subsection}
\subsubsection{A subsubsection}
\lipsum[1]
\end{document}