Page LayoutProperly Align Sections and Subsections

Information and discussion about page layout specific issues (e.g. header and footer lines, page formats, page numbers).
Post Reply
denadul
Posts: 2
Joined: Fri Jun 06, 2014 4:11 pm

Properly Align Sections and Subsections

Post by denadul »

Hello,

I'm fairly new to TeX and I'm still migrating from LibreOffice. I've encountered following problem: The space between the section numbering and the section text is always at a fixed value. Meaning that one the counter goes from "1.9" to "1.10", the section (or subsection) name will be displaced to the right by one character. Also, the section and subsection texts are displaced.
In LibreOffice there was a way to set a tab-with in between the section numbering and the section text leading to neatly aligned text. If uploaded a picture. The red line represents where I want all the text to be lined up.
Image
Is that in any way feasible? I've been trying to achieve this using the titlesec package. As of now without proper results.

Thanks in advance!

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
alainremillard
Posts: 45
Joined: Fri Mar 16, 2012 8:22 pm

Properly Align Sections and Subsections

Post by alainremillard »

titlesec has a way to do this. Its documentation give the definition of the normal classes in latex

Code: Select all

\titleformat{\section} 
{\normalfont\Large\bfseries}{\thesection}{1em}{} 
\titleformat{\subsection} 
{\normalfont\large\bfseries}{\thesubsection}{1em}{} 
Then modify it to enclose the number in a fixed width box

Code: Select all

\documentclass{article}

\usepackage{titlesec}

\titleformat{\section} 
	{\normalfont\Large\bfseries}{\makebox[30pt][l]{\thesection}}{0pt}{} 
\titleformat{\subsection} 
	{\normalfont\large\bfseries}{\makebox[30pt][l]{\thesubsection}}{0pt}{}



\begin{document}

\section{First section}

\subsection{subsection}

\subsection{subsection}

\subsection{subsection}

\subsection{subsection}

\subsection{subsection}

\subsection{subsection}

\subsection{subsection}

\subsection{subsection}

\subsection{subsection}

\subsection{subsection}

\subsection{subsection}

\subsection{subsection}

\end{document}
The \makebox command allows to change the width of a particular text, making latex believing it has another width

Code: Select all

\makebox[width][align]{text}
Have a nice day
Alain Rémillard
denadul
Posts: 2
Joined: Fri Jun 06, 2014 4:11 pm

Re: Properly Align Sections and Subsections

Post by denadul »

Alain, you made my day! The \makebox command proved to be crucial information. Thank you very much. Everything works perfectly fine!
Post Reply