3

Here How to cut a section title in the header we learn how to use \StrLeft with the package titleps.

However, LaTeX outputs an error when there is a math expression in my header say,

\section{The set  $\mathbb{A}$ }

This is linked to the second defect: \StrLeft breaks the title and generally it will output a single letter before the three dots. Like so,

the se...

Is it possible to count the words (including the maths expressions between $ $) instead of the letters so as to cut the title always after a full word ?

is it possible to do the same when the math is entered via a personal command which included a \ensuremath, like so,

\newcommand{\rr}{\ensuremath{\mathbb{R}}\xspace}
  • Welcome to TeX.SE.

    It would be helpful if you composed a fully compilable MWE including \documentclass and the appropriate packages that reproduces the problem.

    While solving problems can be fun, setting them up is not. Then, those trying to help can simply cut and paste your MWE and get started on solving the problem.

    – Peter Grill Apr 21 '14 at 15:31

1 Answers1

3
  • \StrLeft is not robust and it should not expand its argument the hard way.
  • The unbreakable stuff should be put into curly braces.

Example based on the accepted answer of the cited question. Additionally it is modified to avoid the extra dots if they are not needed.

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

\newpagestyle{monstyle}{%
  \headrule
  \sethead{\thepage}{}{%
    \protect\monstyletitle
  }%
}
\makeatletter
\newcommand*{\monstyletitle}{%
    \begingroup
      \protected@edef\sectiontitle{\sectiontitle}%
      \noexpandarg
      \expandafter\StrLen\expandafter{\sectiontitle}[\mylen]%
      \ifnum\mylen>9 % or 10-12, the dots would also need place
        \expandafter\StrLeft\expandafter{\sectiontitle}{9}...%
      \else
        \sectiontitle
      \fi
    \endgroup
}
\makeatother

\begin{document}
\pagestyle{monstyle}
\section{le tres tres long titre qui prend trop de place sur la ligne}
\section{The set {$\mathbb{A}_0$} and {$\mathbb{B}$}}
\end{document}

Result

Usually the place is limited in the header, not the number of characters, thus the answer based on package truncate does make more sense to me.

Heiko Oberdiek
  • 271,626