Based on this answer, I tried my hand at defining my own chapter title formatting. The idea is to align the chapter title to the right (a chapter always opening on an odd page) and underline it with a rule that protrudes into the right margin. When the chapter title is short, it works nicely. When it stretches far to the left however, the chapter number sometimes gets shoved into the left margin, where the binding is supposed to be, so that's a big no-no. I'm not very good at this, so maybe it's a very silly mistake. Thanks in advance for any advice!
My MWE:
\documentclass{book}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\usepackage{changepage}
\usepackage[calcwidth]{titlesec}
\usepackage{lipsum}
% Redefine chapter command
\newlength\mylen
\setlength\mylen{1cm}
\titleformat{\chapter}[hang]{\Huge\bfseries}{\textcolor{gray}{\thechapter}}{2pc}{\raggedleft}[\vspace{-20pt}%
{%
\begin{adjustwidth}{}{-\mylen}%
\makebox[\linewidth][r]{\rule{\dimexpr\titlewidth+\mylen\relax}{1pt}}%
\end{adjustwidth}%
}%
]
\titlespacing{\chapter}{0pt}{0pt}{2cm}
\begin{document}
\chapter{A tremendously-long chapter title illustrating the bug I'm experiencing}
\lipsum[1]
\end{document}
Best Answer
Here's one possible solution; the basic idea is to capture the title (
#1
in theexplicit
option fortitlesec
), to box it inside avarwidth
environment and decide the length of the rule to be used (\ftitlewd+2pc
) , by using a conditional test comparing the actual width of the title (\titlewd
) and the width that was initially reserved to typeset it (\titleboxwd=\textwidth-\mylen=\textwidth-3pc
):Some images of the resulting chapter headings: first, an unnumbered chapter (this will be the formatting fot the ToC, LoF, LoT and bibliography):
A numbered chapter with a long title:
and a numbered chapter with a short title:
Feel free to change the lengths to suit your needs.
As a personal note (of course, not more than a suggestion), with the requested layout, the position of the chapter number will vary from chapter to chapter and I am not sure that this is a good choice. Perhaps you could reconsider this.
The following variation places the chapter number outside the right margin, at the same distance that the rule protusion:
The corresponding images: