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):
![enter image description here](https://i.stack.imgur.com/B91kj.png)
A numbered chapter with a long title:
![enter image description here](https://i.stack.imgur.com/mGEpA.png)
and a numbered chapter with a short title:
![enter image description here](https://i.stack.imgur.com/TLKWv.png)
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:
![enter image description here](https://i.stack.imgur.com/aVTpz.png)
![enter image description here](https://i.stack.imgur.com/1B20x.png)
![enter image description here](https://i.stack.imgur.com/GfQZq.png)
Hers's one possible solution; I used the tikzpagenodes
package to place the chapter number at the desired exact location; the draw=gray
option for the \node
was only used for visualization purposes to show that the box associated to the number glyph is really centered in the marginpar area; the number itself might look not centered, but this is due to the spaces associated to the glyph in the particular font (remove that option on your actual code):
\documentclass[a4paper,twoside,nofonts]{tufte-book}
\usepackage[%
nomath,%
nott,%
% nosf,%
noamsmath,%
notextcomp,%
oldstylenums%
]{kpfonts}
\usepackage{graphicx}
\usepackage{tikzpagenodes}
\usepackage{lineno}
\usepackage{lipsum}
\setcounter{secnumdepth}{0}
\titleformat{\chapter}[block]%
{\relax\ifthenelse{\NOT\boolean{@tufte@symmetric}}{\begin{fullwidth}}{}}% format applied to label+text
{\begin{tikzpicture}[remember picture,overlay]
\node[anchor=north,inner sep=0pt,draw=gray,font=\itshape,text=black!10] at ([yshift=-147pt]current page marginpar area.north) {\resizebox{!}{5cm}{\thechapter}};
\end{tikzpicture}}% label
{0pt}% horizontal separation between label and title body
{\huge\rmfamily\itshape}% before the title body
[\ifthenelse{\NOT\boolean{@tufte@symmetric}}{\end{fullwidth}}{}]% after the title body
\titleformat{name=\chapter,numberless}[block]%
{\relax\ifthenelse{\NOT\boolean{@tufte@symmetric}}{\begin{fullwidth}}{}}% format applied to label+text
{}% label
{0pt}% horizontal separation between label and title body
{\huge\rmfamily\itshape}% before the title body
[\ifthenelse{\NOT\boolean{@tufte@symmetric}}{\end{fullwidth}}{}]% after the title body
\geometry{showframe}
\begin{document}
\linenumbers
\chapter{Implementation}
\lipsum
\chapter{Implementation}
\section{Valence Shell Electron Pair Repulsion theory}
\lipsum
\end{document}
![enter image description here](https://i.stack.imgur.com/5Xaqf.png)
Best Answer
You can use
\filleft
just before\thechapter
: