# titlesec: right-justified section number in margin

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}

• You should maybe specify for which levels you want this to apply. – Skillmon likes topanswers.xyz Jan 5 at 13:20
• I’d like to apply this to all levels. – Roly Jan 5 at 13:24

Just for the example, I took the standard definitions for sectional titles from the documentation of titlesec (section 9.2) and changed 1em to 0pt, adding \marginsecnumber in front of each counter representation command.

\documentclass{article}
\usepackage{titlesec}
\usepackage{lipsum}

\newcommand{\marginsecnumber}[1]{%
\makebox[0pt][r]{#1\hspace{6pt}}%
}

\titleformat{\section}
{\normalfont\Large\bfseries}
{\marginsecnumber\thesection}
{0pt}
{}
\titleformat{\subsection}
{\normalfont\large\bfseries}
{\marginsecnumber\thesubsection}
{0pt}
{}
\titleformat{\subsubsection}
{\normalfont\normalsize\bfseries}
{\marginsecnumber\thesubsubsection}
{0pt}
{}
\titleformat{\paragraph}[runin]
{\normalfont\normalsize\bfseries}
{\marginsecnumber\theparagraph}
{0pt}
{}
\titleformat{\subparagraph}[runin]
{\normalfont\normalsize\bfseries}
{\marginsecnumber\thesubparagraph}
{0pt}
{}

\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}


• Looks good to me! – Roly Jan 5 at 15:56

I propose this variant layout, where all ((sub)sub)sections numbers are in the margin and have the same vertical axis, with the eqparbox package:

\documentclass{article}
\usepackage{titlesec}
\usepackage{eqparbox}
\usepackage{lipsum}

\titleformat{\section}{\Large\bfseries}{\llap{\eqmakebox[S]{\thesection}}}{\marginparsep}{}
\titlespacing*{\section}{-\marginparsep}{*3.25}{*1.5}%
\renewcommand*{\thesubsection}{\arabic{subsection}}

\titleformat{\subsection}{\large\bfseries}{\llap{\eqmakebox[S]{\thesubsection}}}{\marginparsep}{}
\titlespacing*{\subsection}{-\marginparsep}{*3.5}{*2.3}%

\titleformat{\subsubsection}{\normalsize\bfseries}{\llap{\eqmakebox[S]{\thesubsubsection}}}{\marginparsep}{}
\titlespacing*{\subsubsection}{-\marginparsep}{*3.25}{*1.5}%

\begin{document}

\section{A section}
\subsection{A subsection}
\subsubsection{A subsubsection}
\lipsum[1]

\end{document}


Use \llap, taken from this question:

If you want the section and subsubsection numbers to align properly with the subsection numbers, define \titleformat for those headings as well, and drop the use of \@seccntformat.

\documentclass{article}
\usepackage{titlesec}
\usepackage{lipsum}

\makeatletter
\def\@seccntformat#1{\protect\makebox[0pt][r]{\csname
the#1\endcsname\hspace{6pt}}} \makeatother

\titleformat{\subsection}{\large\bfseries}{\llap{\thesubsection\hskip 9pt}}{0em}{}
\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}

• Note that by default \subsection uses a \large font (your answer changes that to the normal size). – Skillmon likes topanswers.xyz Jan 5 at 13:21
• @Skillmon Fixed, thanks! – Roly Jan 5 at 13:25