How can I change the alignment of a chapter/section/subsection heading? Preferably, I want this to be something I specify in the header, as this will be necessary for all headers in the document.
[Tex/LaTex] Right-align chapter/section/subsection header
horizontal alignmentsectioning
Related Solutions
Another option, not requiring additional packages would be to use the TeX primitive \parshape
:
\newcommand\section{%
\renewcommand{\@seccntformat}[1]{%
\csname thesection\endcsname\hspace{0.5em}}%
\@startsection {section}{1}{10mm}%
{3.5ex \@plus 1ex \@minus .2ex}%
{2.3ex \@plus.2ex}%
{\normalfont\bfseries\raggedright\parshape 1 0cm \textwidth}}
An example:
\documentclass{book}
\makeatletter
\renewcommand\section{%
\renewcommand{\@seccntformat}[1]{%
\csname thesection\endcsname\hspace{0.5em}}%
\@startsection {section}{1}{10mm}%
{3.5ex \@plus 1ex \@minus .2ex}%
{2.3ex \@plus.2ex}%
{\normalfont\bfseries\raggedright\parshape 1 0cm \textwidth}}
\makeatother
\begin{document}
\chapter{Test Chapter}
\section{Very long section name Lorem ipsum dolor sit amet, consectetur adipisci veliturea
adipiscing elit. Ut convallis pharetra augue at aliquet.}
Some text Lorem ipsum dolor sit amet, consectetur adipisci veliturea
adipiscing elit. Ut convallis pharetra augue at aliquet.
\end{document}
Your definition will work for sections and subsections, but not for chapters and has a potential drawback in that you loose control over the optional argument for the sectional units; with your definitions, the optional argument will always be equal to the mandatory argument and that might not be desirable.
Here's one option using the sectsty
package:
\documentclass{book}
\usepackage{sectsty}
\usepackage{lipsum}
\allsectionsfont{\centering}
\begin{document}
\chapter{Test Chapter}
\section{Test Section}
\lipsum[4]
\subsection{Test Subsection}
\end{document}
And here's now an option using the titlesec
package:
\documentclass{book}
\usepackage[center]{titlesec}
\usepackage{lipsum}
\begin{document}
\chapter{Test Chapter}
\section{Test Section}
\lipsum[4]
\subsection{Test Subsection}
\end{document}
These solution horizontally centers the headings for all sectional units; the packages provides commands to modify the formatting on a per-level basis. Here's the code (using sectsty
) to apply the change only to chapters, sections and subsections:
\documentclass{book}
\usepackage{sectsty}
\usepackage{lipsum}
\chapterfont{\centering}
\sectionfont{\centering}
\subsectionfont{\centering}
\begin{document}
\chapter{Test Chapter}
\section{Test Section}
\lipsum[4]
\subsection{Test Subsection}
\end{document}
Best Answer
This can be done using the
titlesec
package.Here's a compilable demonstration example:
Now the headings are right aligned. I defined a macro
\justifyheading
. If you later wish to left-justify or to center, simply define\justifyheading
to be\raggedleft
respctively\centering
.