Page LayoutTitlesec, part and superfluous space

Information and discussion about page layout specific issues (e.g. header and footer lines, page formats, page numbers).
Post Reply
LaTeX_Minimix
Posts: 3
Joined: Wed Nov 02, 2016 5:56 pm

Titlesec, part and superfluous space

Post by LaTeX_Minimix »

For a custom document type (based on article if this is relevant) I'm trying to redefine the way LaTeX typesets a \part. I want it to look more or less like a section with a larger font. For this I use titlesec. It works fine, except that LaTeX will insert additional space if the part start at the top of a new page. Why is that and how can I avoid it?

MWE:

Code: Select all

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lipsum}

\usepackage{titlesec}
\titleformat{\part}[hang]{\scshape\Large}{Part~\Alph{part}}{2ex}{}{}
\titlespacing{\part}{0pt}{0pt}{*3}
\titleformat{\section}[hang]{\bfseries\large}{Section~\arabic{section}}{2ex}{}{}
\titlespacing{\section}{0pt}{*2}{0pt}

\usepackage{fancyhdr}
\pagestyle{fancy}


\begin{document}

\part{At the start of a page \ldots{} space above}
\lipsum[1]


\pagebreak
\lipsum[1]

\part{In the middle of a page looks ok}
\lipsum[1]

\pagebreak{}
\section{At the start of a page looks ok}
\lipsum[1]

\end{document}
Even though I use fancyhdr in my real document as well, it's actually not needed to reproduce the unwanted behavior. It's just convenient to have the header separator because it makes the enlarged space more easily visible.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Titlesec, part and superfluous space

Post by Johannes_B »

For this use case, i wouldn't use package titlesec, i would patch the original command.

Code: Select all

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lipsum}

\usepackage{showframe}
\usepackage{titlesec}
%\titleformat{\part}[hang]{\scshape\Large}{Part~\Alph{part}}{2ex}{}{}
%\titlespacing{\part}{0pt}{0pt}{*3}
\titleformat{\section}[hang]{\bfseries\large}{Section~\arabic{section}}{2ex}{}{}
\titlespacing{\section}{0pt}{*2}{0pt}

\usepackage{fancyhdr}
\pagestyle{fancy}

\usepackage{etoolbox}
\renewcommand{\thepart}{\Alph{part}}
\newcommand{\partfont}{\Large\scshape}
\makeatletter
\patchcmd{\@part}{\par\nobreak}{\enspace}{}{}
\patchcmd{\@part}{\Large\bfseries}{\partfont}{}{}
\patchcmd{\@part}{\huge\bfseries}{\partfont}{}{}
\patchcmd{\@spart}{\huge\bfseries}{\partfont}{}{}
\makeatother

\begin{document}

\part{At the start of a page \ldots{} space above}
\lipsum[1]


\pagebreak
\lipsum[1]

\part{In the middle of a page looks ok}
\lipsum[1]

\pagebreak{}
\section{At the start of a page looks ok}
\lipsum[1]

\end{document}
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
LaTeX_Minimix
Posts: 3
Joined: Wed Nov 02, 2016 5:56 pm

Titlesec, part and superfluous space

Post by LaTeX_Minimix »

Johannes_B wrote:For this use case, i wouldn't use package titlesec, i would patch the original command.
Thanks a lot for this workaround and also for introducing me to showframe. But isn't this kind of customization exactly what titlesec is meant for? I would really like to understand what's going wrong there.
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Titlesec, part and superfluous space

Post by Johannes_B »

There are packages that do stuff, some do it well, some don't. titlesec is certainly one of the packages that do what they do well. Personally, i don't like the package that much, as the documentation is pretty confusing for me.
The package titlesec can do what you want, but you don't have to use it. You can define the layout by yourself or patch the original code, if you want to change small things.

What does titlesec do to introduce that space? No idea at all, and i don't care to check out honestly. The more a package can do, the more complex it is. Compare it to a swiss army knife, you can do a lot of stuff, but sometimes you just need a plain screw driver. I decided to use the screwdriver instead of examining the multi-tool.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10324
Joined: Mon Mar 10, 2008 9:44 pm

Titlesec, part and superfluous space

Post by Stefan Kottwitz »

Interesting! You made a very good minimal example that shows the problem. Thanks for that! I tested it and tried things, but could not get rid of that space too. I was only working with the titlesec manual, not yet with the source code. Looks like a bug to me. When I got time, I could take a look at the titlesec source code, just these days I'm very busy.

You are right, using packages is recommendable, I think patching should be used as a last resort. But Johannes is right too, sometimes it's good to patch to simply get things done. But that's not the official future-proof recommendation. ;-)

Stefan
LaTeX.org admin
LaTeX_Minimix
Posts: 3
Joined: Wed Nov 02, 2016 5:56 pm

Titlesec, part and superfluous space

Post by LaTeX_Minimix »

Johannes, Stefan, thank you both for your replies and explanations. I don't want to waste your time by going through the package code. If the behavior I see is a bug, that's enough of an explanation for me. I just thought that it's a known/expected thing and these I'd like to understand better. I'm definitely not up to understand complex LaTeX package code yet.
damitr
Posts: 1
Joined: Wed May 06, 2020 10:31 pm

Titlesec, part and superfluous space

Post by damitr »

How to color all the entries in the \part{}. I have tried this with your code above

Code: Select all

\renewcommand{\thepart}{\color{red}\Roman{part}}
\newcommand{\partfont}{\Large\scshape}
\makeatletter
\patchcmd{\@part}{\par\nobreak}{\enspace}{}{}
\patchcmd{\@part}{\Large\bfseries}{\partfont}{}{}
\patchcmd{\@part}{\huge\bfseries}{\partfont}{}{}
\patchcmd{\@spart}{\huge\bfseries}{\partfont}{}{}
\makeatother
but this doesn't colour the \thepart ""Part" only the number and the part title.
User avatar
Ijon Tichy
Posts: 640
Joined: Mon Dec 24, 2018 10:12 am

Titlesec, part and superfluous space

Post by Ijon Tichy »

Untested and speculative because of missing Infominimal working example:

Code: Select all

\patchcmd{\@part}{\normalfont}{\normalfont\color{red}}{}{}
\patchcmd{\@spart}{\normalfont}{\normalfont\color{red}}{}{}
BTW: If you have a new question, it is recommended to ask a new question with a new Infominimal working example instead of adding a comment to an already existing (more or less old) one.
Sorry, but I can no longer participate here as the administrator is trampling on my wishes on one of his other platforms. :cry:
Post Reply