Page LayoutMulti-line (section) headers?

Information and discussion about page layout specific issues (e.g. header and footer lines, page formats, page numbers).
Post Reply
gizmoguy
Posts: 3
Joined: Sat Oct 03, 2009 6:01 pm

Multi-line (section) headers?

Post by gizmoguy »

I'm having a bit of trouble with some of my section headers. A lot of them are very long, and as a result get pushed over the margins, also moving the page number. I have a line break(\\) in them anyway, because I want part of the section names to be on new lines both at the section title, and in the table of contents. Unfortunately this doesn't work for the headers. Is there any way I can get my headers to be multi-line so that the part after the line break goes on its own line in the header? I'm using the book class, double-sided if this helps. Thanks.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
User avatar
localghost
Site Moderator
Posts: 9201
Joined: Fri Feb 02, 2007 12:06 pm

Multi-line (section) headers?

Post by localghost »

I can't comprehend the problem with the line breaks.

Code: Select all

\documentclass[11pt,a4paper,english]{book}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage{blindtext}

\begin{document}
  \tableofcontents

  \chapter{A long chapter title only chosen to demonstrate a line break that shouldn't go into the margin}
    \blindtext

    \section{A long section title only chosen to demonstrate a line break that shouldn't go into the margin}
      \blindtext
\end{document}
Regarding the header I recommend to choose the short form option for the sectioning commands.

It would be much better to show your problems by means of a minimal working example (MWE) which shall be small, complete and compilable.


Best regards and welcome to the board
Thorsten¹
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes[/size]

¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1
gizmoguy
Posts: 3
Joined: Sat Oct 03, 2009 6:01 pm

Multi-line (section) headers?

Post by gizmoguy »

What I have now is like the following (I have set it as A5 to make the problem more obvious):

Code: Select all

\documentclass[a5paper,english]{book}
\usepackage[T1]{fontenc}
\usepackage[utf8x]{inputenc}
\usepackage{blindtext}

\setcounter{secnumdepth}{1}
\setcounter{tocdepth}{1}

\begin{document}
\tableofcontents

\chapter{This is an example chapter}
\section{This is an example section with a long name\\(and this is text I want to go below)}
\blindtext
\blindtext
\blindtext
\blindtext
\blindtext
\blindtext
\end{document}
Which in my ToC and at the location in the document where the section name is printed gives me:
This is an example section with a long name
(and this is text I want to go below)
Which is what I want. However, with my header it gives me:
This is an example section with a long name(and this is text I want to go below)
which is unacceptable because it runs right across the page. I want the header of the pages to be like it is in the ToC and at the location in the document (i.e. I want it to have a line break in it).
meho_r
Posts: 823
Joined: Tue Aug 07, 2007 5:28 pm

Multi-line (section) headers?

Post by meho_r »

You may consider using fancyhdr package for setting headers and footers easy way. Here's an example (your code, slightly changed):

Code: Select all

\documentclass[a4paper,english]{book}% Set a4paper
\usepackage[T1]{fontenc}
\usepackage[utf8x]{inputenc}
\usepackage{blindtext}
\usepackage{babel}

\usepackage{fancyhdr}% fancyhdr related command; details in its documentation
\pagestyle{fancy}
\fancyhead{}
\fancyhead[LE,RO]{\thepage}
\fancyhead[RE]{\leftmark}
\fancyhead[LO]{\rightmark}

\setcounter{secnumdepth}{1}
\setcounter{tocdepth}{1}

\begin{document}
\tableofcontents

\chapter{This is an example chapter}
\section{This is an example section with a long name\\(and this is text I want to go below)}
\blindtext
\blindtext
\blindtext
\blindtext
\blindtext
\blindtext
\blindtext
\blindtext
\blindtext
\blindtext
\blindtext
\blindtext
\end{document}
But still, two lines in headers look, ehm, strange at least.
gizmoguy
Posts: 3
Joined: Sat Oct 03, 2009 6:01 pm

Re: Multi-line (section) headers?

Post by gizmoguy »

That's great thanks! There were a few things I wanted to change in the headers actually, but I didn't think fancyhdr would do that as well. Thanks!
ahmedalmindelawy
Posts: 2
Joined: Mon Feb 28, 2022 3:13 am

Multi-line (section) headers?

Post by ahmedalmindelawy »

Good
Post Reply