Text FormattingCenter a very long chapter tile

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
Mimi
Posts: 5
Joined: Wed Jan 07, 2015 5:40 pm

Center a very long chapter tile

Post by Mimi »

Hello!

Below is my MWE.

Code: Select all

\documentclass{report}

\usepackage{titlesec}
\titleformat{\chapter}[hang]
{\normalfont\normalsize\bfseries\centering}{\chaptertitlename\ \thechapter:}{2mm}{}

\begin{document}
\chapter{The big black bug bit the big black bear, but the big black bear bit the big black bug back!}
\end{document}
I would like to have the second line of the chapter title to be in the center of the page.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Center a very long chapter tile

Post by Johannes_B »

Both are typeset in different boxes, so you manually need to break the lines.

Code: Select all

\documentclass{report}

\usepackage{showframe}
\usepackage{titlesec}
\titleformat{\chapter}[hang]
{\normalfont\normalsize\bfseries\centering}{\chaptertitlename\
\thechapter:}{2mm}{}
\newlength{\chapterindent}
\newcommand{\gethalfwidth}{\settowidth{\chapterindent}{\bfseries\chaptertitlename\
\thechapter:}%
\addtolength{\chapterindent}{2mm}
}
\begin{document}
\newcommand{\breakline}{\\\gethalfwidth\hspace{-\chapterindent}\ignorespaces}
\chapter{The big black bug bit the big
	black bear, but the big\breakline
bear bit the big black bug back!}

\setcounter{chapter}{20}
\chapter{The fox, and the duck, and another small animal,
but\breakline also a big red elephant are walking down the road
to\breakline happiness}
\end{document}
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
Mimi
Posts: 5
Joined: Wed Jan 07, 2015 5:40 pm

Re: Center a very long chapter tile

Post by Mimi »

谢谢你的帮助!
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Re: Center a very long chapter tile

Post by Johannes_B »

If google translate works properly; i respond to that with: No problem ;-)
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
Post Reply