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}
Roly
  • 4,221
  • 4
  • 26
  • 56

3 Answers3

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

egreg
  • 1,121,712
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

Bernard
  • 271,350
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}
Roly
  • 4,221
  • 4
  • 26
  • 56