4

I need to cut the chapter title that appears at page headers.

I'm developing a latex generator software (http://github.com/caelum/tubaina, new contributors are welcome!), and because of implementation issues I can't simply use \chaptermark to create an arbitrary title for the header. I've tried using \StrLeft from xstring but didn't work for me.

Here is the latex code that produces the output that I would like to see, but in a way I can't use:

\documentclass[a4paper, 11pt, twoside]{book}

\usepackage{fancyhdr}
\usepackage{xstring}
\usepackage{lipsum}


\begin{document}

\fancypagestyle{plain}{
    \fancyhf{}
    \fancyhead[LO,RE]{\footnotesize{Publisher Name}}
    \fancyhead[LE]{\scriptsize{\nouppercase{\rightmark}}}
    \fancyhead[RO]{\scriptsize{\nouppercase{\leftmark}}}
    \fancyfoot[RO,LE]{\thepage}
}

\pagestyle{plain}

%% This doesn't work
% \renewcommand{\chaptermark}[1]{\markboth{\thechapter.\ \StrLeft{#1}{5}}{}}

\renewcommand{\chaptermark}[1]{\markboth{\thechapter.\ #1}{}}
\renewcommand{\sectionmark}[1]{\markright{\thesection.\ #1}{}}

\chapter{Very long long long long long long long long long long long long long long chapter title}

%% This also doesn't work
% \chaptermark{\StrLeft{chapter mark lalala lalala}{5}}

%% This works and that's the way I would like to the 
%% title appear, but I can't use it for other reasons...
\chaptermark{Very long long...}

\section{Section name}
\lipsum

\end{document}

Is there any another way to do that?

Count Zero
  • 17,424

2 Answers2

6

Fragile command in moving argument so you could use

\renewcommand{\chaptermark}[1]{\markboth{\thechapter.\ \protect\StrLeft{#1}{5}}{}}

Although rather than chopping off after so many characters I think a better result could be obtained by typesetting to a specified measure into a temporary box, and then just showing the first line.


Something like this implements the scheme sketched above:

enter image description here

\documentclass[a4paper, 11pt, twoside]{book}

\usepackage{fancyhdr}
\usepackage{xstring}
\usepackage{lipsum}


\begin{document}

\fancypagestyle{plain}{
    \fancyhf{}
    \fancyhead[LO,RE]{\footnotesize Publisher Name}
    \fancyhead[LE]{\scriptsize\nouppercase{\rightmark}}
    \fancyhead[RO]{\scriptsize\chopheader{\nouppercase{\leftmark}}}
    \fancyfoot[RO,LE]{\thepage}
}

\pagestyle{plain}

\makeatletter
\def\chopheader#1{{%
\hbadness\@M
\vbadness\@M
  \setbox\z@\vbox{%
\hsize=0.3\textwidth
\@parboxrestore\raggedright
\interlinepenalty-\@M
#1}%
\setbox\tw@\vsplit\z@ to \baselineskip
\setbox\tw@\vbox{\unvbox\tw@\global\setbox\@ne\lastbox}%
\leavevmode\unhbox\@ne\unskip\unskip\ifdim\ht\z@>\z@\ldots\fi
}}
\makeatother

%% This doesn't work
% \renewcommand{\chaptermark}[1]{\markboth{\thechapter.\ \protect\StrLeft{#1}{5}}{}}

%\renewcommand{\chaptermark}[1]{\markboth{\thechapter.\ #1}{}}
\renewcommand{\sectionmark}[1]{\markright{\thesection.\ #1}{}}

\chapter{Very long long long long long long long long long long long long long long chapter title}

%% This also doesn't work
% \chaptermark{\StrLeft{chapter mark lalala lalala}{5}}

%% This works and that's the way I would like to the 
%% title appear, but I can't use it for other reasons...
%\chaptermark{Very long long...}

\section{Section name}
\lipsum

\end{document}
David Carlisle
  • 757,742
  • It worked! Thanks! It seems to be the most elegant solution! – Francisco Sokol Jun 11 '12 at 17:45
  • I just added a \hfill to align the chapter title to right, like this: \fancyhead[RO]{\hfill\scriptsize\chopheader{\nouppercase{\leftmark}}} – Francisco Sokol Jun 11 '12 at 17:47
  • Is there a way to show a ellipsis in case the title overflow the vbox? Resulting in something like: "Very long long..." – Francisco Sokol Jun 11 '12 at 18:07
  • The \hfill shouldn't be necessary the RO option to fancyheader should do that I think, as shown in my image? adding an elipsis change one line so it looks like \leavevmode\unhbox\@ne\unskip\ifdim\ht\z@>\z@\ldots\fi – David Carlisle Jun 11 '12 at 18:12
  • Thanks again, it worked! Strange, without hfill the title didn't got aligned rigth, see this image link – Francisco Sokol Jun 11 '12 at 18:20
  • sorry that was a bug: if the heading was short you got rightskip and parfillskip that needs removing so there has to be two \unskip I edited the answer (and added the \ldots test at the same time) – David Carlisle Jun 11 '12 at 19:04
3

The expansion of some xstring macros are not purely expandable, as explained in the documentation - section 3.2 Expansion of macros, optional argument. That's why the author supplied them with an optional argument, to store the result. You can modify your code to make it work, like this for instance:

\renewcommand{\chaptermark}[1]{\StrLeft{#1}{5}[\shortTitle]%
\markboth{\thechapter.\ \shortTitle}{}}
Count Zero
  • 17,424