6

The problem I am having is, sometimes the section or chapter title is too long to be fit into the header space and overlaps with other content. Is there any way to automatically substitute excessive texts with "…" or whatever is appropriate?

Here is MWE

% !Mode:: "TeX:UTF-8"
\documentclass[UTF8, english]{book}
\usepackage{babel}
\usepackage{blindtext}
\usepackage{lipsum}

\usepackage{fancyhdr}


\fancyhf{}
\fancyhead[LE, RO]{\scriptsize \slshape \rightmark} %% Section number and title.
\fancyhead[RE, LO]{\scriptsize \slshape \leftmark} %% Chapter number and title.

\fancyfoot[R]{\scriptsize \thepage} %% Page number.

\begin{document}
\pagestyle{fancy}

\chapter{Asymptotic Behaviour of Supersymmetric Yang Mills Theories in The Two Loop Approximation}

\blindtext

\section{Calculation of $\beta \left( g \right)$}

\lipsum

\end{document}

Thank you in advance!

6
  • 3
    \section[short title]{long title} ?
    – Fran
    Commented Jul 10, 2018 at 20:06
  • 1
    Use the optional argument of \chapter to define a short version of the chapter title.
    – Bernard
    Commented Jul 10, 2018 at 20:08
  • @Fran Thank you for providing that solution, I did consider that option, but the feature I am trying to achieve is automatic truncation of long text, which means I do not need to think about whether a title is too long or not, the typesetter thinks about all that for me.
    – zyy
    Commented Jul 10, 2018 at 20:39
  • @zyy Often it's better left to the author the task of thinking, but please see my edited answer.
    – Fran
    Commented Jul 11, 2018 at 2:45
  • But you have to think about titles like “A short introduction to electromagnetism”. Something like ”A short introduction to...” isn't very helpful. Commented Jul 12, 2018 at 18:27

3 Answers 3

6

I do not what is the appropriate short title, but some like this solve the problem:

\chapter[Yang Mills Theories]
{Asymptotic Behaviour of Supersymmetric 
Yang Mills Theories in The Two 
Loop Approximation}

All sectioning commands (\chapter,\section, etc.) have this optional argument that is used in headers and table of contents.

Edit: The automatic truncation at some length in general is not a good idea for short titles, as usually this could left a string with no meaning. What if the key words of the chapter title are not "Asymptotic Behaviour" but "Loop Approximation" ? And what if the truncation left a incomplete word as "Asymptotic Behaviour of Super"... What "Super", maybe Superman? Do you really want a table of contents with truncated titles?

However, if that is no problem for you, this is an automatic solution using xtring:

mwe

\documentclass{book}
\usepackage{xstring}
\usepackage{lipsum}
\usepackage{fancyhdr}
\fancyhf{}
\fancyhead[LE, RO]{\scriptsize \slshape \rightmark}
\fancyhead[RE, LO]{\scriptsize \slshape \leftmark}
\fancyfoot[R]{\scriptsize \thepage}
\def\mychapter#1{\StrLeft{#1}{20}[\Result]\chapter[\Result\ldots]{#1}}
\begin{document}
\pagestyle{fancy}
\mychapter{Asymptotic Behaviour of Supersymmetric Yang Mills 
Theories in The Two Loop Approximation}
\lipsum[2]
\section{Calculation of $\beta \left( g \right)$}
\lipsum[3-12]
\end{document}
1
  • How did you get the special background for the picture?
    – zyy
    Commented Jul 12, 2018 at 0:36
2

I have found a package truncate that does this! Here is how

% !TEX encoding = UTF-8 Unicode
\documentclass[UTF8, english]{book}
\usepackage{babel}
\usepackage{blindtext}
\usepackage{lipsum}

\usepackage[fit, breakall]{truncate}
\usepackage{fancyhdr}

\fancyhf{}
\fancyhead[LE, RO]{\tiny \truncate{0.47 \textwidth} \leftmark}
\fancyhead[RE, LO]{\tiny \truncate{0.47 \textwidth} \rightmark}

\begin{document}
\pagestyle{fancy}

\chapter{Asymptotic Behaviour of Supersymmetric Yang Mills Theories in The Two Loop Approximation}

\blindtext

\section{Calculation of $\beta \left( g \right)$ and a lot more text over here which to too long to show}

\lipsum
\blindtext

\end{document}

There are two more pieces of detail to be noticed.

  1. Package truncate has to be included with option breakall, which allows truncation at any character. If not, the truncation would only happen at the end of words or hyphenation points, which could affect the positioning of header.
  2. Package truncate has to be included with option fit, which prints the output in its natural position.

Here is the output

truncate header

4
  • +1. This solution is also perfect for headers, but note that unlike using the optional argument, this allow very long titles in the table of contents (that could be fine in your case, but personally I find annoying).
    – Fran
    Commented Jul 11, 2018 at 23:00
  • @Fran That is a problem I should think about, thanks! However, there is a downside of this solution which I just found. It will not position the title correctly if you use \titleformat{\section}[block]{\usefont{OT1}{lmdh}{m}{it}}{\thesection.}{7 pt}{\filcenter}[] with package titlesec to center the section title in the body of the document! Any thoughts on this issue? Thanks!
    – zyy
    Commented Jul 12, 2018 at 0:32
  • Not sure of what you mean, it seem OK in my computer! Anyway, this is another problem. The philosophy of this site is: one question, one problem, another problem, another question. The change of the scope of the question turn right answers in wrong answers and mixing problems does not help to another users to find the right questions. Please make a new question and MWE (include also a link to the previous answer if you think that is related and that could help to fully understand the new problem).
    – Fran
    Commented Jul 12, 2018 at 15:12
  • @Fran I found out how to avoid the problem, package truncate has to be included with option fit.
    – zyy
    Commented Jul 12, 2018 at 18:05
0

If you want to define a shorter title for the header, but leave the section heading and the entry in the table of contents unchanged, you can use the \chaptermark, \sectionmark, etc. commands of the fancyhdr package:

\chapter[Title in TOC]{%
   Title at the beginning of the chapter\chaptermark{Title for header}%
}
\chaptermark{Title for header}

Note that we use \chaptermark twice here. This awkward solution is explained in detail in the fancyhdr documentation and avoids issues for some edge cases.

You must log in to answer this question.

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