26

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

  • 1
    Is there a special reason why you use picture? – Marco Daniel Jul 7 '13 at 13:31
  • 1
    @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. – nutty about natty Jul 7 '13 at 13:34
33

Don't use picture but directly use \includegrahics:

\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
    Thanks! dunno how I missed trying that one... ;) +1 for the "heads up" on the fancyhdr warning ! – nutty about natty Jul 7 '13 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 Nov 22 '14 at 0:09
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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