4

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:

enter image description here

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}
2
  • You should maybe specify for which levels you want this to apply.
    – Skillmon
    Commented Jan 5, 2020 at 13:20
  • I’d like to apply this to all levels.
    – Roly
    Commented Jan 5, 2020 at 13:24

3 Answers 3

5

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.

Add your own customizations.

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

enter image description here

1
  • Looks good to me!
    – Roly
    Commented Jan 5, 2020 at 15:56
1

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} 

enter image description here

0

Use \llap, taken from this question:

enter image description here

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}
2
  • 1
    Note that by default \subsection uses a \large font (your answer changes that to the normal size).
    – Skillmon
    Commented Jan 5, 2020 at 13:21
  • @Skillmon Fixed, thanks!
    – Roly
    Commented Jan 5, 2020 at 13:25

You must log in to answer this question.

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