# How to cut a section title in the header

I redefine my headers as in the following MWE:

\documentclass[12pt]{article}
\usepackage{xstring}
\usepackage{fancyhdr}
\fancypagestyle{monstyle}{%
\fancyhf{} % remove everything
\leftmark%
}
}
\begin{document}
\pagestyle{monstyle}
\section{le tres tres long titre qui prend trop de place sur la ligne}
\end{document}


The section title could be very long, like in my MWE, and i want to cut it at a fixed length (say 15 letters). My attempt is to use the command \StrLeft from the package xstring : I replace the line

\leftmark%


by

    \StrLeft{\leftmark}{15}...%


But i get compilation errors which i do not understand:

Argument of \@iiparbox has an extra }.


EDIT I try to use Mico's solution, but i encounter now a problem with the TOC :

\documentclass[12pt]{article}
\usepackage{xstring}
\usepackage{fancyhdr}
\usepackage{blindtext}

%% Redefine the short title
\let\origsection\section %
\renewcommand{\section}[1]{%
\def\ShortSecName{\StrLeft{#1}{10}...}
\origsection[\protect\ShortSecName]{#1}%
}

\begin{document}
\tableofcontents
\section{mon titre tres long tres tres long}
\blindtext\blindtext\blindtext
\section{mon titre tres long tres tres long}
\blindtext\blindtext\blindtext
\end{document}


I get the error :

! LaTeX Error: Something's wrong--perhaps a missing \item.


If i remove \tableofcontents then it compiles.

-
\section has an optional argument: \section[Short Title]{Long title which is long} The short version is what appears in headers, ToC etc. –  Seamus Jan 16 '12 at 16:37
possible duplicate of Chapter title in header too long –  Marco Daniel Jan 16 '12 at 16:46
Thank you for the hint, but i don't want to enter manually the "short title". I want to cut it automatically after n caraters. –  nicolas roy Jan 16 '12 at 19:08

An alternative approach would be using titleps instead of fancyhdr (caveat: I'm the author of titleps):

\documentclass[12pt]{article}
\usepackage{xstring}
\usepackage{titleps}

\newpagestyle{monstyle}{
{}
{\StrLeft{\sectiontitle}{15}...}}

\begin{document}
\pagestyle{monstyle}
\section{le tres tres long titre qui prend trop de place sur la ligne}
\end{document}


\sectiontitle is a macro containing just the title, not a mark with part of the header (formatted), and therefore it can be handled easily.

-
I added you as a package author in this thread: meta.tex.stackexchange.com/questions/1181/… I guess this is ok for you. –  Thorsten Jan 17 '12 at 17:44
@Thorsten. Of course it's ok. Thanx. –  Javier Bezos Jan 17 '12 at 18:00
thanks for pointing out titlesep to me. I would have to learn the new syntaxe to try to reproduce the formatting i have in my "old" documents with fancyhdr... By the way, i'm already using titlesec... do i need titlesep as well, or can i do everything with titlesec ? –  nicolas roy Jan 17 '12 at 19:56
ok, i see, titleps works like an option of titlesec. –  nicolas roy Jan 17 '12 at 20:12
I tested your solution. I encouter problems with accented accents written directly \section{ letters öäü} (i use \usepackage[utf8]{inputenc}). But it seems to be rather a problem of \StrLeft... –  nicolas roy Jan 18 '12 at 7:06

You could use the truncate package:

\documentclass[12pt]{article}
\usepackage{fancyhdr}
\usepackage[breakall]{truncate}
\fancypagestyle{monstyle}{%
\fancyhf{} % remove everything

\begin{document}
\pagestyle{monstyle}
\section{le tres tres long titre qui prend trop de place sur la ligne}
\end{document}


-

You need to \protect the \StrLeft command. You don't explain what exactly you do with redefining the \leftmark command, so I suggest you use the \protect\StrLeft approach in the setup of the \section[]{} command:

\section[\protect\StrLeft{Le tres tres long titre qui prend
trop de place sur la ligne}{15}]{Le tres tres long titre
qui prend trop de place sur la ligne}


Addendum One can automate this process by redefining the \section command suitably, say along the lines of the following MWE (minimum working example):

\documentclass[12pt]{article}
\usepackage{xstring,fancyhdr}
\fancypagestyle{monstyle}{%
\fancyhf{} % remove everything
}
\pagestyle{monstyle}

\let\origsection\section       %  Save the original \section command
\renewcommand{\section}[2][15]{%  Default value for the first, optional arg: "15"
\origsection[\protect\StrLeft{#2}{#1}]{#2}}
\begin{document}

\section[23]{Le tr{\e}s tr{\e}s long titre qui prend trop de place sur la ligne}

Il y avait une fois que \ldots
\end{document}


Note that the redefined \section command takes two arguments. The first argument, the number of characters to be retained for the short title or running head, is optional and defaults to 15. The second argument is the "regular" (possibly very long) section header. If you don't want any shortening of the section header, just supply a number -- is 1,000 large enough? -- that exceeds the number of characters in the section header.

Notes: (i) Should you need to create a section header without automatically setting the page's running head as well, you could use the command \origsection (which contains, unsurprisingly, the original version of the \section command). (ii) A side effect of this setup is that the \tableofcontents and \section* commands won't work correctly; let me know if this is a concern to you, and I'll figure out a solution.

-
it sounds interesting. It indeed works, if i declare the short title in \section, but this is not what i want to do. I want that for all sections automatically, and not enter StrLeft{my short title}{15} per hand... otherwise, i could cut it directly per hand. So, how could i use \protect inside the fancyhdr declaration ? –  nicolas roy Jan 16 '12 at 20:21
Please see the addendum I've provided. I don't believe your issue can be solved by modifying the fancyhdr macros. Instead, what's needed -- if I understand your objectives, of course -- is a redefinition of the \section command. –  Mico Jan 16 '12 at 23:07
I tested your solution, but it makes a problem with the TOC. Please see my edit (in 5 minutes). –  nicolas roy Jan 17 '12 at 14:38
by the way, yes, you are probably right, it is much more logical to redefine the short title than the fancyhdr macros. –  nicolas roy Jan 17 '12 at 14:56
yes, actually, i need the TOC. By the way, the first argument [15] could be hard-coded if it helps, and the command \section would stay with the same structure \section[short]{long title}, where the 1st argument wouldnt be used in my case. –  nicolas roy Jan 17 '12 at 19:39

If you want to keep all the functionality of \section and get the original title in the table of contents, here is how

\makeatletter
\let\cutsec@ori@section\section
\def\section{\@ifstar\cutsec@ssection\cutsec@section}
\def\cutsec@ssection{\cutsec@ori@section*}
\def\cutsec@section{\@dblarg\cutsec@@section}
\def\cutsec@@section[#1]#2{%
\cutsec@ori@section[#1]{#2}%
\StrLeft{#2}{10}[\temp]%
\expandafter\markboth{\expandafter\MakeUppercase\expandafter{\temp...}}{}}
\makeatother


However, such a way of doing is risky when the section title has accented characters, unless you use an 8 bit encoding (latin-1, for instance) and refrain from explicit accents such as \'el\eve, that is, you write élève. It's definitely bound to break with UTF-8 and pdflatex (not with UTF-8 engines such as XeLaTeX and LuaLaTeX).

-
Great answer! About accents and related diacritics in the section header: I believe that as long you place the diacritics and their associated characters within curly braces -- e.g., {\'e}l{e}ve and L{\"o}ffel -- you'll be safe even when running this code under pdflatex. –  Mico Jan 17 '12 at 16:42
@Mico Yes, but you lose kerning. –  egreg Jan 17 '12 at 16:44