0

My TeX document has a \section with a too long text, so I'd like to change header by \markright` with something like that^

\section{Too long text}
\markright{\thesection. Shorter text}

However, as the help promises, right header is set by the first \markright or \markboth on the page, so with the code above the header contains Too long text (generated by \section). The code

\markright{\thesection. Shorter text}
\section{Too long text}

gives almost what is required, excepting the text number which is 1 less that it should, because increasing \section is just below. So, it is needed to write something like

\stepcounter{section}
\markright{\thesection. Shorter text}
\addtocounter{section}{-1}
\section{Too long text}

It gives solution, but seems too tricky for so simple task. There is optional argument in \section but it changes not only header, but also line in the contents, which is not needed.

So, is it possible to change header after `\section' in a simple way?

1

3 Answers 3

1

Please try this code

\documentclass[10pt, a4paper,twoside]{article}

\usepackage{kantlipsum}

\usepackage{fancyhdr}
\pagestyle{fancy}

\fancypagestyle{plain}{}
\fancyhf{}

\fancyhead[RE,LO]{\rmfamily \small \nouppercase \rightmark}

\begin{document}

\pagestyle{fancy}

\section{Abstract \protect\markboth{Abstract}{Abstract} }

\kant[1]

\clearpage

\section{A very long title perhaps two lines \protect\markboth{Short title I }{Short title I}}
\kant[2-5]

\clearpage

\section{A much  longer  title perhaps two lines  o more A much  longer  title perhaps two lines  o more \protect\markboth{Short title II}{Short title II}}
\kant[6-9]           
    
\clearpage
\section{Normal title}
\kant[10-12] 


\end{document}

output

1

You need to change the headers anyway, because the style with uppercase slanted titles is really awful.

I propose a \doubletitle command, to be used for long section (or chapter) titles; the first argument is the long title, the second one is the short form.

\documentclass{book}

\usepackage{fancyhdr}

\usepackage{lipsum} % for mock text

\fancyhf{} % clear all fields
\fancyhead[LE,RO]{\thepage}
\fancyhead[RE]{\shorttrue\nouppercase{\leftmark}}
\fancyhead[LO]{\shorttrue\nouppercase{\rightmark}}
\pagestyle{fancy}

\newif\ifshort
\DeclareRobustCommand{\doubletitle}[2]{\ifshort #2\else #1\fi}

\begin{document}

\frontmatter

\tableofcontents

\mainmatter

\chapter{Title}

\section{\doubletitle{First Here we have a very very very very very very very very very
  very very very very very very very very very very very very long title}{First}}

\lipsum[1-20]

\section{\doubletitle{Second Here we have a very very very very very very very very very
  very very very very very very very very very very very very long title}{Second}}

\lipsum[1-20]

\section{Short title}

\lipsum[1-20]

\end{document}

You might consider using memoir instead of book that has a built in mechanism with two optional arguments for the sectional commands.

0

Try the memoir class (encompasses the book, report and article) classes. Document divisions may take two optional arguments for the ToC text, and separately the heading text. For example:

\section{<toc-title>][<head-title>]{<title>}
  • no optional arguments <title> used in ToC, header, and document; <title> is always used for the document.
  • one optional argument <toc-title> used for ToC and header.
  • two optional arguments <toc-title> for the ToC, <head-title> for the header.

You must log in to answer this question.

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