# Relative spacing in titlesec

I am trying to create a custom section and subsection format that consists of the index, followed by a vertical line, followed by the title. An example can be found here: http://texblog.org/2012/07/03/fancy-latex-chapter-styles/

I can't figure out how to properly align the vertical bars. I would like the whitespace between the index and the vertical bar to be relative to the length of the index. The whitespace should be larger for sections than for subsections, so that the vertical bars are aligned across the different section titles, and consequently, so would be the titles and subtitles themselves.

Here is my minimal working example. At the moment the vertical bar for subsections is positioned further away from the left margin, since the index contains two rather than one digit. If it is at all possible, I'd prefer a solution that works for any font size and family; i.e. solutions that depend on defining an absolute position from the margin will probably not carry over between different font styles and sizes.

\documentclass{article}
\usepackage{fontspec} % since I am using LuaTex
\usepackage{titlesec}

\titleformat{\section}[hang]{\normalfont\Large\scshape}{\thesection\vspace{0.5cm} | }{1em}{}
\titleformat{\subsection}{\normalfont\large\scshape}{\thesubsection\vspace{0.5cm} | }{1em}{}

\begin{document}
\section{Section 1}
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
\subsection{Subsection 1}
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
\end{document}


Like this?

\documentclass{article}
\usepackage{fontspec}
%\usepackage[T1]{fontenc}
\usepackage{titlesec}

\titleformat{\section}[hang]{\normalfont\Large\scshape}{\makebox[8mm][l]{\thesection}|  }{1em}{}
\titleformat{\subsection}{\normalfont\large\scshape}{\makebox[8mm][l]{\thesubsection}|  }{1em}{}

\begin{document}
\section{Section 1}
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
\subsection{Subsection 1}
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
\end{document}


Just put the section number inside a box of fixed width. Adjust 8mm suitably as you wish.

• Brilliant, this is exactly what I was looking for. Thanks! Sep 26, 2015 at 9:25