4

enter image description here

enter image description here

I'm trying to produce a document with section numbers in the page margin. I found a pseudo-solution which llaps the section numbering to mimic the result. I'm not happy with the way the number and the section align. In fact, I kind of think they don't align at all (see above pictures).

Normally, my go-to method of solving this is by issuing the \strut command, but I couldn't figure out a way to do this within the \titleformat command.

How can I align them anyway?

MWE:

\documentclass[]{article}


\usepackage{titlesec}
\usepackage{xcolor}
\usepackage{showframe}

\newlength\titleindent
\setlength\titleindent{.25in}


\titleformat{\section}% 
  {\normalfont}% 
  {}%
  {0pt}%
  {\llap{\parbox{\titleindent}{\large\thesection\strut\hfill}}\large}%
  []% 



\begin{document}

\section{MMMMmmm}


\end{document}
5

Why a \parbox?

\documentclass[]{article}

\usepackage{titlesec}
\usepackage{showframe}

\newlength\titleindent
\setlength\titleindent{.25in}

\titleformat{\section}
  {\normalfont\large}
  {\makebox[0pt][r]{\thesection\hspace{\titleindent}}}
  {0pt}
  {}

\begin{document}

\section{MMMMmmm}

\end{document}

Note that the number should go in the third argument to \titleformat, so numberless sections will behave correctly. The global font size should go in the second argument.

enter image description here

What's the problem with \parbox? That its default vertical alignment is c; you should have used \parbox[t], but it's a waste of resources: the section number doesn't make a paragraph.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.