2

For some reason, the header in my document has to include an image which width has to be the width of the paper (in a4 format, it is 21cm). But I don't want the margin of body to be changed, just the one of the header. Here is an example :

\documentclass{article}

\usepackage{graphicx} 
\usepackage{geometry}
\geometry{a4paper,includehead,top=0cm} 
\usepackage{fancyhdr}
\pagestyle{fancy} 
\setlength\headheight{28pt}
\chead{\includegraphics[width=\paperwidth]{hautpage1.jpg}}
\renewcommand{\headrulewidth}{0pt}

\begin{document}

gthfgh
\newpage
bzedgfhbd
\newpage
bzedgfhbd

\end{document}

enter image description here

As you can see, the image is shifted on the right, but does not fit all the paper width. What can I do/change in order to have my picture to fit all the page ? Here is what I want : enter image description here

Any suggestion ?

1

The problem is, that your image is wider (\paperwidth) than the place where you want to put it (\headwidth). When compiling, you currently get the warning

Overfull \hbox (179.2542pt too wide) ...

which informs you about that.

A possible solution is to place the picture inside a box of width \headwidth. That box can then be placed correctly by LaTeX, as it has the correct width. You can do that with the \makebox[width][alignment]{content} command, with a width of \headwidth and c for centered alignment.

\documentclass{article}

\usepackage{graphicx} 
\usepackage{geometry}
\geometry{a4paper,includehead,top=0cm} 

\usepackage{fancyhdr}
\pagestyle{fancy} 
\setlength\headheight{60.9pt}
\chead{\makebox[\headwidth][c]{\includegraphics{manyducks.jpg}}}

\renewcommand{\headrulewidth}{0pt}

\begin{document}

gthfgh
\newpage
bzedgfhbd
\newpage
bzedgfhbd

\end{document}

result

  • thanks for your quick answer, and it is exactly what I wanted ! I have a question then. You put the image in a box of width \headwidth even though the image is larger than that (width is \paperwidth), but it still works. – antoine Aug 3 '16 at 12:09
  • Exactly. That way, the image is treated as if it were \headwidth wide, though the content is actually \paperwidth. – hbaderts Aug 3 '16 at 12:11
  • I come back to you about that. Actually, I want to use the result of a Rmarkdown, which gives me a .tex with the R code and the text I wrote with it. In the preamble of the .tex, there is those lines to rescale automatically the pictures that are too large \makeatletter \def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi} \def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi} \makeatother \setkeys{Gin}{width=\paperwidth,height=\maxheight,keepaspectratio} – antoine Sep 5 '16 at 13:28
  • But then, the picture in my header is also rescale! So far, I just remove those lines, and I rescale by hand the pictures that are too large. Is there a way to let those command lines that are usefull, and still have the picture in my header to fit all the paper width ? Also, on the titlepage, I have a picture whose width is the width of the paper, so I'd like this to stay the same, meaning not to be rescale by the new Gin command. Thanks for you answer – antoine Sep 5 '16 at 13:30

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.