45

In the MWE below, the blue square aligns according to plan, whereas the dummy-image doesn't, see screenshots below. How to re-code so that the dummy-image "A" is aligned like the blue square?

\documentclass[a4paper]{article}

\usepackage{graphicx}

\usepackage{fancyhdr}
\pagestyle{fancy}

\usepackage{xcolor}

\usepackage{lipsum}

% \rhead{{\color{blue}\rule{1cm}{1cm}}}

\rhead{\begin{picture}(0,0) \put(0,0){\includegraphics[width=1cm]{example-image-a}} \end{picture}}

% \rhead{\begin{picture}(3,3) \put(3,3){\includegraphics[width=1cm]{example-image-a}} \end{picture}}

\begin{document}

\lipsum

\end{document}

"A" outside blue square inside

2
  • 3
    Is there a special reason why you use picture? Commented Jul 7, 2013 at 13:31
  • 2
    @MarcoDaniel Found that in the fancyhdr manual... but no, no special reason other than trying to come up with some solution... am just interested in a(n elegant) way to align the picture just like the blue square. Commented Jul 7, 2013 at 13:34

2 Answers 2

58

Don't use picture but instead directly use \includegraphics:

\rhead{\includegraphics[width=1cm]{example-image-a}}

Now you should get a warning by fancyhdr about headheight:

Package Fancyhdr Warning: \headheight is too small (12.0pt): 
 Make it at least 24.93825pt.
 We now make it that large for the rest of the document.
 This may cause the page layout to be inconsistent, however.

To get rid of this, add:

\setlength\headheight{26pt} 

to the preamble. This can be better done with the help of geometry package. That is a different story.

So the code will be:

\documentclass[a4paper]{article}

\usepackage{graphicx}

\usepackage{fancyhdr}
\pagestyle{fancy}

\usepackage{xcolor}

\usepackage{lipsum}
\setlength\headheight{26pt} %% just to make warning go away. Adjust the value after looking into the warning.
% \rhead{{\color{blue}\rule{1cm}{1cm}}}

\rhead{\includegraphics[width=1cm]{example-image-a}}

% \rhead{\begin{picture}(3,3) \put(3,3){\includegraphics[width=1cm]{example-image-a}} \end{picture}}

\begin{document}

\lipsum

\end{document}

enter image description here

2
  • 2
    Thanks! dunno how I missed trying that one... ;) +1 for the "heads up" on the fancyhdr warning ! Commented Jul 7, 2013 at 13:56
  • @HarishKumar Believe it or not, this helped me a lot just for the warning!!!. I did not receive this warning, however only the first page was printed properly, for the rest of the document the footer was being displayed way too low. I added \setlength\headheight{200pt} and now all the pages display the same, is just question of customize the page geometry (I used the geometry package).
    – Hans
    Commented Nov 22, 2014 at 0:09
6

In addition to Harish's answer, I recommend using the geometry package.

\usepackage[margin=2.5cm,headheight=26pt,includeheadfoot]{geometry}

The crucial points are setting headheight to a value bigger than the one in the warning (and omitting the \setlength\headheight command) and includeheadfoot if you use a footer at the same time.

Further details can be found here and here.

You must log in to answer this question.

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