2

This would have been so easy to do in ConTeXt... :(

I've been trying with this layout for a while now, and nothing seems to work as I intended.

I have a custom chapter title, created with titlesec. I want to keep the chapter title text and a really big chapter number vertically centered, with respect to each other, even if the chapter title is so short it only takes one line.

Currently, they align to the baseline, as should be expected. But I can't find a way to "force" that vertical alignment... I don't know if I should try with tikz, maybe.

The whole group (title + number) is aligned to the right, and are separated by a line with certain characteristics (same height as the number, a certain width and a color). I've tried with tabular, tabu, parbox... probably easy, but I can't find a viable solution. I don't know if some of my code is screwing things up so I've included at least the font (Erewhon) and arbitrary size definitions, and some macros I've created, maybe there's something wrong I'm not seeing.

Any help would be greatly appreciated.

Here's my MWE:

\documentclass{book}
\usepackage[clearempty,newlinetospace,raggedright,small,explicit]{titlesec}
\usepackage{pbox}
\usepackage{lipsum}
\usepackage{erewhon}
\usepackage{microtype}
\usepackage[usenames,dvipsnames]{xcolor}

\definecolor{gris50}{gray}{0.5}


\newcommand{\size}[2]{{\fontsize{#1}{0}\selectfont #2}}
\newcommand{\sizeline}[3]{{\fontsize{#1}{#2}\selectfont #3}}
\newenvironment{sizepar}[2]
 {\par\fontsize{#1}{#2}\selectfont}
 {\par}
\newcommand{\bigvertical}[1][black]{\kern12pt\textcolor{#1}{\rule[0pt]{1pt}{\fontcharht\font`M}}\kern10pt}

\titleformat{\chapter}[block]{\bfseries}{}{0pt}{%
  \filleft% 
  \pbox[b][][b]{.6\textwidth}{%
    \raggedleft%
    \sizeline{14}{16}{\textls[30]{#1}}%
  }%
  \sizeline{40}{40}{\bigvertical[gris50]\textcolor{gris50}{\thechapter}}%
}

\titleformat{\section}[block]{\bfseries}{}{0pt}{\textls[20]{\sizeline{11.2}{13}{#1}}}
\titleformat{\subsection}[block]{\bfseries\slshape}{}{0pt}{\textls[20]{\sizeline{10.2}{13}{#1}}}

\titlespacing*{\chapter}{0pt}{0pt}{122pt}

\begin{document}

\chapter{A really long chapter title with several words}

\lipsum[1-3]

\chapter{A short title}

\lipsum[1-3]

\end{document}

Title somewhat well aligned

Title aligns to the bottom

3
  • 1
    What you want is not quite clear to me. By ‘vertically aligned’, do you mean ‘vertically centred w.r.t. each other’, i.e. same horizontal axis?
    – Bernard
    Jun 30, 2019 at 9:30
  • 1
    LaTeX aligns baselines, but you can move the baseline using, for example, \raisebox. Personally, I would have gone with top alignment. Jun 30, 2019 at 16:26
  • @Bernard: yes. I mean that.
    – El Andi
    Jun 30, 2019 at 19:28

1 Answer 1

3

You can get the vertical centering with tabular and \parbox. Moreover, \vrule computes by itself the needed height and depth.

\documentclass{book}
\usepackage[clearempty,newlinetospace,raggedright,small,explicit]{titlesec}
\usepackage{pbox}
\usepackage{lipsum}
\usepackage{erewhon}
\usepackage{microtype}
\usepackage[usenames,dvipsnames]{xcolor}

\definecolor{gris50}{gray}{0.5}

\newcommand{\size}[2]{{\fontsize{#1}{0}\selectfont #2}}
\newcommand{\sizeline}[3]{{\fontsize{#1}{#2}\selectfont #3}}
\newenvironment{sizepar}[2]
 {\par\fontsize{#1}{#2}\selectfont}
 {\par}

\newcommand{\printchapternumber}{%
  \begingroup\renewcommand{\arraystretch}{0}%
  \begin{tabular}{@{}c@{}}\size{40}{\thechapter}\end{tabular}%
  \endgroup
}

\titleformat{\chapter}[block]
  {\bfseries\filleft}
  {}
  {0pt}
  {%
   \parbox{.6\textwidth}{\raggedleft\fontsize{14}{16}\selectfont#1}%
   \hspace{6pt}%
   \color{gris50}\vrule
   \hspace{6pt}%
   \printchapternumber
  }

\titleformat{\section}[block]{\bfseries}{}{0pt}{\textls[20]{\sizeline{11.2}{13}{#1}}}
\titleformat{\subsection}[block]{\bfseries\slshape}{}{0pt}{\textls[20]{\sizeline{10.2}{13}{#1}}}

\titlespacing*{\chapter}{0pt}{0pt}{122pt}

\begin{document}

\begingroup\let\clearpage\relax\let\cleardoublepage\relax

\chapter{A really long chapter title with several words}

\chapter{A short title}

\chapter{A really long long long long long chapter title with several several several words}

\endgroup

\end{document}

I redefined \clearpage and \cleardoublepage in the document body just to get the titles on the same page so to easily compare them.

enter image description here

Final note: I removed \textls. Whoever who would letterspace lowercase would also steal sheep.

3
  • thank you and I understand your position about letterspacing lowercase letters... and I agree. But I had to reproduce a previously made InDesign layout with some... let's say quirks, provided by my client, with a LaTeX one, while preserving as much of the original design as possible.
    – El Andi
    Jun 30, 2019 at 19:26
  • This works perfectly. Thank you very much @egreg. I'll convince my client to drop the letterspacing!
    – El Andi
    Jun 30, 2019 at 19:39
  • 1
    @ElAndi If you fail, you can always add it.
    – egreg
    Jun 30, 2019 at 19:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .