3

I want to have an image behind the page number in my latex document, and tried to write it like

\fancyhead[CE,CO]{\textcolor{white}{\thepage}}
\chead{\includegraphics[width=1cm]{img.eps}}

The problem is: When I first have the line with the page number, and then the image, I only get the image, and if I have the other way round, then I only get the number. How can I have the number in front of the image? Should I use a background image for that?

  • What image is it? – Alenanno Feb 5 '16 at 19:01
  • A black tent (i.e. totally black figure, vectorized) – arc_lupus Feb 5 '16 at 19:04
  • As in a black triangle? – Alenanno Feb 5 '16 at 19:11
  • @Alenanno: Yes. – arc_lupus Feb 5 '16 at 20:21
3

Here's one way:

\documentclass{article}
\usepackage{fancyhdr,stackengine,xcolor,graphicx}
\fancypagestyle{mystyle}{
\chead{\stackinset{c}{}{c}{}{\textcolor{red}{\thepage}}{%
  \includegraphics[width=1cm]{example-image}}}
}
\pagestyle{mystyle}
\begin{document}
My text
\end{document}

enter image description here

  • Is there a possibility to shift the image also such that the number is not centered, but slightly shifted in one direction? – arc_lupus Feb 5 '16 at 21:15
  • @arc_lupus, one can easily move the page number within the image, by adding the x-shift as the 2nd argument and the y-shift as the 4th argument to \stackinset, as in \stackinset{c}{8pt}{c}{6pt}{inset}{base image}. – Steven B. Segletes Feb 5 '16 at 23:17
  • @arc_lupus The 1st argument to \stackinset is the horizontal anchor position, which can be left, center, or right. The 3rd argument is the vertical anchor, either top, center, or bottom. – Steven B. Segletes Feb 5 '16 at 23:19
2

You can set the content in the sequence that you want them layered.

With the above in mind, we can set the image in the Left header, which will be followed by setting of the Centred header:

enter image description here

\documentclass{article}

\usepackage{graphicx,fancyhdr,xcolor,lipsum}

\fancyhead[C]{\textcolor{white}{\thepage}}
\fancyhead[L]{\makebox[\textwidth]{\includegraphics[height=.7\baselineskip]{example-image}}}
\pagestyle{fancy}

\begin{document}

\lipsum[1-10]

\end{document}

The Left header is set in a box of width \textwidth which is naturally centred. Adjustment of the positioning of the image may be needed.

If all you want is a white page number on a black background, you can just use

\fancyhead[C]{\raisebox{0pt}[0pt][0pt]{\colorbox{black}{\textcolor{white}{\thepage}}}}

which sets the page number using \colorbox:

enter image description 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.