How to make chapter and section titles at the beginning of pages be on the same line

reporttitlesec

Consider test case:

\documentclass{report}

\usepackage{titlesec}
\titleformat{\chapter}{\normalfont\LARGE\bfseries}{\thechapter}{10pt}{}
\titlespacing*{\chapter}{0pt}{0pt}{10pt}


\begin{document}
\chapter{Test0}\newpage
\chapter{Test1}\newpage
\section{Test1.1}\newpage
\end{document}

Even after setting vertical space before title to 0pt with titlespacing, chapter and section titles at the beginning of pages still aren't on the same line:

enter image description here

This can be a result of report use, at least in article this difference is about 1pt.

Is it possible to set vertical space before chapter title to 0 or remove it? Instead of using titlespacing with negative values?

Best Answer

If you use Gonzalo Medina's or Danie Els' answer, the vertical space above the chapter title is 'almost' removed. According to Gonzalo Medina's answer:

The internal command \ttl@mkchap@i adds some vertical spacing to the chapter titles, so you need to remove this space; this can be done by commenting out the line responsible for this space (the one marked with % NEW in the code):

Output after applying Gonzalo Medina's solution: enter image description here The red line indicates page margin.

After removing the vertical space by commenting out the \vspace*, there is still a small space left. The reason behind this small space (see struts) being left is described in the titlesec documentation. From the documentation:

The styles defined by titlesec insert some struts at certain places to make sure the vertical space is the same with relation with the baseline.

To remove this strut from the chapter title, the \nostruts macro can be used in the \titleformat of the chapter.

\documentclass{report}

\usepackage{titlesec}

\makeatletter
\def\ttl@mkchap@i#1#2#3#4#5#6#7{%
  \ttl@assign\@tempskipa#3\relax\beforetitleunit
  %\vspace*{\@tempskipa}% NEW
  \global\@afterindenttrue
  \ifcase#5 \global\@afterindentfalse\fi
  \ttl@assign\@tempskipb#4\relax\aftertitleunit
  \ttl@topmode{\@tempskipb}{%
    \ttl@select{#6}{#1}{#2}{#7}}%
  \ttl@finmarks  % Outside the box!
  \@ifundefined{ttlp@#6}{}{\ttlp@write{#6}}}
\makeatother

\titleformat{\chapter}{\normalfont\LARGE\bfseries\nostruts}{\thechapter}{10pt}{} %%\nostruts here
\titlespacing*{\chapter}{0pt}{0pt}{10pt}

%-------Shows page layout-------------------
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%-------------------------------------------

\begin{document}
\chapter{Test0}\newpage
\chapter{Test1}\newpage
\section{Test1.1}\newpage
\end{document} 

1st page:

1

2nd page:

2

3rd page:

3

Notice that the strut is still present in the section title as we only removed it from the chapter title.

Related Question