Custom chapter title protruding in left margin

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}%
{%
\makebox[\linewidth][r]{\rule{\dimexpr\titlewidth+\mylen\relax}{1pt}}%
}%
]
\titlespacing{\chapter}{0pt}{0pt}{2cm}

\begin{document}

\chapter{A tremendously-long chapter title illustrating the bug I'm experiencing}

\lipsum[1]

\end{document}


-
And where should the chapter number be? Always at the left of the title and at a fixed distance? –  Gonzalo Medina Mar 23 '13 at 17:43
I was thinking I'd put it at 2pc from the title itself, but it should never poke into the left margin, so that this should give a maximum area for the title text, just a bit under the standard linewidth. If it breaks out into multiple lines, they shouldn't be allowed to go under the number. –  Christoph B. Mar 23 '13 at 17:55
But then the position of the chapter number will vary from chapter to chapter and I am not sure if this is a good choice. Wouldn't it be better if the chapter number stays at a fixed horizontal position for every chapter? (This is, of course, just a suggestion; the final decision is up to you). –  Gonzalo Medina Mar 23 '13 at 22:07

Here's one possible solution; the basic idea is to capture the title (#1 in the explicit option for titlesec), to box it inside a varwidth 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):

\documentclass{book}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\usepackage{varwidth}
\usepackage[calcwidth,explicit]{titlesec}
\usepackage{lipsum}

\newlength\mylen
\newsavebox\mybox
\newlength\titlewd
\newlength\ftitlewd
\newlength\titleboxwd
\setlength\mylen{3pc}
\setlength\titleboxwd{\dimexpr\textwidth-\mylen\relax}
\setlength\ftitlewd{\titleboxwd}

\newcommand\chapfont{\Huge\bfseries\raggedleft}

\titleformat{\chapter}[hang]
{\chapfont\filleft}
{}{0pc}
{\parbox[t]{\mylen}{\textcolor{gray}{\thechapter}\hfill}%
\begin{lrbox}{\mybox}%
\begin{varwidth}[t]{\titleboxwd}%
\chapfont#1%
\end{varwidth}%
\end{lrbox}%
\settowidth\titlewd{\usebox\mybox}%
\ifdim\titlewd<\titleboxwd%
\global\setlength\ftitlewd{\titlewd}%
\fi%
\parbox[t]{\ftitlewd}{\usebox\mybox}%
}
[\vskip-1.4ex{\makebox[0pt][l]{%
\hspace*{-\ftitlewd}\rule{\dimexpr\ftitlewd+2pc\relax}{1.5pt}}}%
]
\titleformat{name=\chapter,numberless}[hang]
{\chapfont\filleft}
{}{0pc}
{%
\begin{lrbox}{\mybox}%
\begin{varwidth}[t]{\titleboxwd}%
\chapfont#1%
\end{varwidth}%
\end{lrbox}%
\settowidth\titlewd{\usebox\mybox}%
\ifdim\titlewd<\titleboxwd%
\global\setlength\ftitlewd{\titlewd}%
\fi%
\parbox[t]{\ftitlewd}{\usebox\mybox}%
}
[\vskip-1.4ex{\makebox[0pt][l]{%
\hspace*{-\ftitlewd}\rule{\dimexpr\ftitlewd+2pc\relax}{1.5pt}}}%
]
\titlespacing*{\chapter}
{1\mylen}{50pt}{40pt}

\begin{document}

\chapter*{An Unnumbered Chapter}
\lipsum[4]
\chapter{A tremendously Long Chapter Title Showing a Solution}
\lipsum[4]
\chapter{Short Chapter Title}
\lipsum[4]

\end{document}


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:

\documentclass{book}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\usepackage{varwidth}
\usepackage[calcwidth,explicit]{titlesec}
\usepackage{lipsum}

\newsavebox\mybox

\newlength\mylen
\newlength\titlewd
\newlength\ftitlewd
\newlength\titleboxwd
\setlength\mylen{3pc}
\setlength\titleboxwd{\dimexpr\textwidth-\mylen\relax}
\setlength\ftitlewd{\titleboxwd}

\newcommand\chapfont{\Huge\bfseries\raggedleft}

\titleformat{\chapter}[hang]
{\chapfont\filleft}
{}{0pc}
{%
\begin{lrbox}{\mybox}%
\begin{varwidth}[t]{\titleboxwd}%
\chapfont#1%
\end{varwidth}%
\end{lrbox}%
\settowidth\titlewd{\usebox\mybox}%
\ifdim\titlewd<\titleboxwd%
\global\setlength\ftitlewd{\titlewd}%
\fi%
\parbox[t]{\ftitlewd}{\usebox\mybox}%
\makebox[0pt][l]{\parbox[t]{4pc}{\hfill\textcolor{gray}{\thechapter}}}%
}
[\vskip-1.4ex{\makebox[0pt][l]{%
\hspace*{-\ftitlewd}\rule{\dimexpr\ftitlewd+4pc\relax}{1.5pt}}}%
]
\titleformat{name=\chapter,numberless}[hang]
{\chapfont\filleft}
{}{0pc}
{%
\begin{lrbox}{\mybox}%
\begin{varwidth}[t]{\titleboxwd}%
\chapfont#1%
\end{varwidth}%
\end{lrbox}%
\settowidth\titlewd{\usebox\mybox}%
\ifdim\titlewd<\titleboxwd%
\global\setlength\ftitlewd{\titlewd}%
\fi%
\parbox[t]{\ftitlewd}{\usebox\mybox}%
}
[\vskip-1.4ex{\makebox[0pt][l]{%
\hspace*{-\ftitlewd}\rule{\dimexpr\ftitlewd+4pc\relax}{1.5pt}}}%
]
\titlespacing*{\chapter}
{1\mylen}{50pt}{40pt}

\begin{document}

\chapter*{An Unnumbered Chapter}
\lipsum[4]
\chapter{A tremendously Long Chapter Title Showing a Solution}
\lipsum[4]
\chapter{Short Chapter Title}
\lipsum[4]

\end{document}


The corresponding images:

-
Hey, thanks already for this! There are two issues when I adopt it to my full-length document. First, chapters in the \frontmatter have a "0" appearing in front, and the bibliography has the same chapter number as the last number. These should have no number, I guess. Second, one of the chapters actually runs out farther into the right margin than the line. Finally, I'd love to hear your suggestion, I'm open to good practices in type out design. –  Christoph B. Mar 24 '13 at 19:32
@ChristophB. I added the code for dealing with unnumbered chapters (ToC, LoF, LoT, bibliography). The second issue, the title surpassing the right margin should not happen, perhaps the last word cannot be, for some reason, properly hyphenated? I would need to see the actual problem (can you please make a simple test document like the ones in my examples, upload it elsewhere and provide the link?). What would you think about placing the chapter numbers to the right, outside the margin? I'll design the layout and post it here in some hours; I'll let you know. –  Gonzalo Medina Mar 24 '13 at 22:20
@ChristophB. I've already added this new option in my updated answer. –  Gonzalo Medina Mar 24 '13 at 22:32
Thanks a million! It had indeed something to do with hyphenation, I slightly changed the title and now it works perfectly. I also really like your suggestion for the placement of the chapter number, I've adopted it. –  Christoph B. Mar 25 '13 at 12:40
@ChristophB. You're welcome! Glad I could help. –  Gonzalo Medina Mar 25 '13 at 15:20