Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
    
See for example How to cut a section title in the header –  cgnieder Jun 11 '12 at 16:18

2 Answers 2

up vote 6 down vote accepted

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}
share|improve this answer
    
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

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}{}}
share|improve this answer
    
This should work but David Carlisle solution above seems to be the most elegant one... Thanks anyway! –  Francisco Sokol Jun 11 '12 at 17:48
    
It is, indeed. :) –  Count Zero Jun 11 '12 at 19:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.