[Tex/LaTex] Center body and left margin in page

horizontal alignmentmarginssectioningtitlesec

I'm using the titlesec package to put section titles to the left of their body, but I'd like for the entire page to appear centered. That is, I'd like for the space from the far left of the page to the beginning of the title equal the space from the end of the text body to the far right of the page.

How can I accomplish this?

Relevant WIP code:

\titleformat{\section}[leftmargin]{\raggedright\scshape}{}{0pt}{}
\titlespacing*{\section}{2.5cm}{*2.5}{0.5cm}
\titleformat{\subsection}{\bfseries}{}{0pt}{}
\titlespacing*{\subsection}{0pt}{*2}{*1}

Edit: As @cmhughes pointed out, I can manually adjust the margins with the geometry package to align the page properly. However, things like the title of the page look skewed, since they are centered with respect to the body. Is there any way I can center a different element with respect to the page?

Best Answer

Using titlesec, you set the title spacing to be 2.5cm+0.5cm (or 30mm) from the left margin of the text. As such, specifying the left and right margins, using geometry, as

\usepackage{geometry}% http://ctan.org/pkg/geometry
\newcommand{\mygeometry}[1]{%
  \geometry{right=#1,left=\dimexpr#1+30mm\relax}% Set new geometry
}
\mygeometry{25mm}% 25mm left/right margins

will give you 25mm left/right margins. You can specify any known dimension <len> in \mygeometry{<len>}. If you modify the spacing of your titles, would need to modify the 30mm dimension in \mygeometry as well.


Here is a minimal working example highlighting the above procedure and shows the first two pages of output:

enter image description here

\documentclass{article}
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{geometry}% http://ctan.org/pkg/geometry
\newcommand{\mygeometry}[1]{%
  \geometry{right=#1,left=\dimexpr#1+30mm\relax}% Set new geometry
}
\mygeometry{25mm}% 25mm left/rightmargins
\usepackage{titlesec}% http://ctan.org/pkg/titlesec
\titleformat{\section}[leftmargin]{\raggedright\scshape}{}{0pt}{}
\titlespacing*{\section}{2.5cm}{*2.5}{0.5cm}
\titleformat{\subsection}{\bfseries}{}{0pt}{}
\titlespacing*{\subsection}{0pt}{*2}{*1}
\begin{document}
\section{First section} \lipsum[1]
\subsection{First subsection} \lipsum[2]
\subsection{Second subsection} \lipsum[3]
\subsection{Third subsection} \lipsum[4]
\subsection{Last subsection} \lipsum[5]
\section{Second section} \lipsum[1]
\subsection{First subsection} \lipsum[2]
\subsection{Second subsection} \lipsum[3]
\subsection{Third subsection} \lipsum[4]
\subsection{Last subsection} \lipsum[5]
\section{Last section} \lipsum[1]
\subsection{First subsection} \lipsum[2]
\subsection{Second subsection} \lipsum[3]
\subsection{Third subsection} \lipsum[4]
\subsection{Last subsection} \lipsum[5]
\end{document}

lipsum was only used to provide dummy text.

Related Question