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'm using this code to customize the header layout:

\pagestyle{fancy}
\fancyhead{}
\fancyfoot{}
\lhead{\nouppercase{\rightmark}}
\rhead{\thepage}

I would like to have the section name in the top left of every page, in the form of “1.1 section”, and this is possible using \rightmark. But, like mentioned in the fancyhdr documentation, "on the first page of a chapter (or a section in article style) the \rightmark will be empty". So, when \rightmark is empty I would like have printed the chapter name like “Chapter 1. Intro” instead of the section name, and this is possible using \leftmark. So my question is: can I use such an "else condition" in order to use \leftmark if \rightmark is empty, and to use \rightmark otherwise? Something like:

empty(\rightmark) ? \leftmark : \rightmark

Thanks in advance.

share|improve this question

migrated from stackoverflow.com Mar 11 '13 at 11:14

This question came from our site for professional and enthusiast programmers.

    
Welcome to TeX.sx! Your post was migrated here from Stack Overflow. Please register on this site, too, and make sure that both accounts are associated with each other (by using the same OpenID), otherwise you won't be able to comment on or accept answers or edit your question. –  egreg Mar 11 '13 at 12:24
add comment

2 Answers

up vote 5 down vote accepted

You can test if the \rightmark is empty by fully expanding it; the command \rightorleftmark will do what you want (uncomment the \section line to see the difference).

\documentclass{book}

\usepackage{fancyhdr}
\pagestyle{fancy}

\fancyhf{}
\lhead{\nouppercase{\rightorleftmark}}
\rhead{\thepage}

\makeatletter
\newcommand{\rightorleftmark}{%
  \begingroup\protected@edef\x{\rightmark}%
  \ifx\x\@empty
    \endgroup\leftmark
  \else
    \endgroup\rightmark
  \fi}
\makeatother

\begin{document}
\mainmatter
\chapter{A}
%\section{B}
abc\newpage
def\newpage
\end{document}
share|improve this answer
add comment

I resolved with these commands, they print the chapter name and number on odd page, and the section on even. When there is no section defined yet for even page, it prints again the chapter name and number.

\newcommand{\customizedheader}{
    \ifthenelse
         {\isodd{\thepage}}
         {\leftmark{}}
         {\checkemptyrightmark{\rightmark{}}}
}

\newcommand{\checkemptyrightmark}[1]{
    \ifthenelse
        {\equal{#1}{\@empty{}}}
        {\leftmark{}}
        {\rightmark}
}

And finally use the macro for the header

\newcommand{\currentPar}{\customizedheader}

Ah, the ifthenelse command come from the ifthen package and the \@empty is defined as

\def\@empty{}
share|improve this answer
    
Welcome to TeX.SX! –  Heiko Oberdiek Apr 7 at 11:31
    
Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. Please rewrite your code as a MWE such everybody can apply your answer successfully. –  strpeter Apr 7 at 11:50
add comment

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.