LaTeX forum ⇒ Page LayoutTitlesec, part and superfluous space Topic is solved

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

Titlesec, part and superfluous space

Postby LaTeX_Minimix » Wed Nov 02, 2016 7:09 pm

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] [Expand/Collapse] [Download] ({untitled.tex})
  1. \documentclass{article}
  2. \usepackage[T1]{fontenc}
  3. \usepackage[utf8]{inputenc}
  4. \usepackage{lipsum}
  5.  
  6. \usepackage{titlesec}
  7. \titleformat{\part}[hang]{\scshape\Large}{Part~\Alph{part}}{2ex}{}{}
  8. \titlespacing{\part}{0pt}{0pt}{*3}
  9. \titleformat{\section}[hang]{\bfseries\large}{Section~\arabic{section}}{2ex}{}{}
  10. \titlespacing{\section}{0pt}{*2}{0pt}
  11.  
  12. \usepackage{fancyhdr}
  13. \pagestyle{fancy}
  14.  
  15.  
  16. \begin{document}
  17.  
  18. \part{At the start of a page \ldots{} space above}
  19. \lipsum[1]
  20.  
  21.  
  22. \pagebreak
  23. \lipsum[1]
  24.  
  25. \part{In the middle of a page looks ok}
  26. \lipsum[1]
  27.  
  28. \pagebreak{}
  29. \section{At the start of a page looks ok}
  30. \lipsum[1]
  31.  
  32. \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.

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

Postby Johannes_B » Sat Nov 05, 2016 4:42 pm

For this use case, i wouldn't use package titlesec, i would patch the original command.
Code: [Select all] [Expand/Collapse] [Download] ({untitled.tex})
  1. \documentclass{article}
  2. \usepackage[T1]{fontenc}
  3. \usepackage[utf8]{inputenc}
  4. \usepackage{lipsum}
  5.  
  6. \usepackage{showframe}
  7. \usepackage{titlesec}
  8. %\titleformat{\part}[hang]{\scshape\Large}{Part~\Alph{part}}{2ex}{}{}
  9. %\titlespacing{\part}{0pt}{0pt}{*3}
  10. \titleformat{\section}[hang]{\bfseries\large}{Section~\arabic{section}}{2ex}{}{}
  11. \titlespacing{\section}{0pt}{*2}{0pt}
  12.  
  13. \usepackage{fancyhdr}
  14. \pagestyle{fancy}
  15.  
  16. \usepackage{etoolbox}
  17. \renewcommand{\thepart}{\Alph{part}}
  18. \newcommand{\partfont}{\Large\scshape}
  19. \makeatletter
  20. \patchcmd{\@part}{\par\nobreak}{\enspace}{}{}
  21. \patchcmd{\@part}{\Large\bfseries}{\partfont}{}{}
  22. \patchcmd{\@part}{\huge\bfseries}{\partfont}{}{}
  23. \patchcmd{\@spart}{\huge\bfseries}{\partfont}{}{}
  24. \makeatother
  25.  
  26. \begin{document}
  27.  
  28. \part{At the start of a page \ldots{} space above}
  29. \lipsum[1]
  30.  
  31.  
  32. \pagebreak
  33. \lipsum[1]
  34.  
  35. \part{In the middle of a page looks ok}
  36. \lipsum[1]
  37.  
  38. \pagebreak{}
  39. \section{At the start of a page looks ok}
  40. \lipsum[1]
  41.  
  42. \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

Postby LaTeX_Minimix » Tue Nov 08, 2016 8:45 pm

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: 4160
Joined: Thu Nov 01, 2012 4:08 pm

Postby Johannes_B » Tue Nov 08, 2016 9:50 pm

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: 9567
Joined: Mon Mar 10, 2008 9:44 pm

Postby Stefan Kottwitz » Wed Nov 09, 2016 11:04 am

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

Postby LaTeX_Minimix » Thu Nov 10, 2016 4:12 pm

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

Postby damitr » Wed May 06, 2020 10:40 pm

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

Code: [Select all] [Expand/Collapse] [Download] ({untitled.tex})
  1. \renewcommand{\thepart}{\color{red}\Roman{part}}
  2. \newcommand{\partfont}{\Large\scshape}
  3. \makeatletter
  4. \patchcmd{\@part}{\par\nobreak}{\enspace}{}{}
  5. \patchcmd{\@part}{\Large\bfseries}{\partfont}{}{}
  6. \patchcmd{\@part}{\huge\bfseries}{\partfont}{}{}
  7. \patchcmd{\@spart}{\huge\bfseries}{\partfont}{}{}
  8. \makeatother


but this doesn't colour the \thepart ""Part" only the number and the part title.

User avatar
Ijon Tichy
Posts: 266
Joined: Mon Dec 24, 2018 10:12 am

Postby Ijon Tichy » Thu May 07, 2020 8:42 am

Untested and speculative because of missing Infominimal working example:
Code: [Select all] [Expand/Collapse] [Download] ({untitled.tex})
  1. \patchcmd{\@part}{\normalfont}{\normalfont\color{red}}{}{}
  2. \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.


Return to “Page Layout”

Who is online

Users browsing this forum: No registered users and 3 guests