2

I'm wondering if there is a way in LaTex to add a different picture to every Section header using fancyhdr, but without defining \fancypagestyle XX times.

I think a good starting point could be giving a Section and its corresponding picture the same name, but then -- how to define \fancypagestyle using a variable/placeholder? (Sorry, I still don't have a working piece of code).

Thanks a lot!

2
  • Do you want to add the image to the header? Will there be something else in the header? Please clarify.
    – leandriis
    Mar 24, 2020 at 10:56
  • 2
    Can you please add a minimal working example to show what you have tried:. The document you want to use is probably relevant and it would be good to see what layout you want, otherwise people might waste time doing something else. Why don't you paste a \fancypagestyle that does what you want.
    – user30471
    Mar 24, 2020 at 11:07

1 Answer 1

2

Suppose you have an additional optional argument for \section to pass in the filename for an associated picture like \section(<image file>)[<toc name>]{<full name>}. Assuming that this additional information gets stored in a macro called \headerpic it is possible to simply declare the header once like so:

\fancyhead[C]{\includegraphics[width=\textwidth]{\headerpic}}

A possible implementation would look like this.

\let\ltx@section\section
\def\section{\kernel@ifnextchar(%
  {\@section@HP}%
  {\@section@HP(\@headerpic@default)}%
}
\def\@section@HP(#1){%
  \clearpage\xdef\headerpic{#1}\ltx@section
}

Passing in no image will result in falling back to \@headerpic@default, which should be set manually or through a wrapper to make it nice looking:

\def\SetHeaderpicDefault#1{%
  \xdef\headerpic{#1}
  \xdef\@headerpic@default{#1}
}

This also initializes \headerpic which is crucial if the first header is typeset without a section being present.

output0

output1

Complete Code

% arara: pdflatex: { shell: true }
\documentclass[11pt]{article}
% This is just to make sure that the the demo pictures used are present
\write18{wget https://i.picsum.photos/id/388/2000/200.jpg}
\write18{wget https://i.picsum.photos/id/495/2000/200.jpg}
\write18{wget https://i.picsum.photos/id/520/2000/200.jpg}
\usepackage{graphicx}
\usepackage{fancyhdr}
  \pagestyle{fancy}
  \fancyhf{}
  \fancyhead[C]{\includegraphics[width=\textwidth]{\headerpic}}

\usepackage{lipsum}

\makeatletter
\let\ltx@section\section
\def\section{\kernel@ifnextchar(%
  {\@section@HP}%
  {\@section@HP(\@headerpic@default)}%
}
\def\@section@HP(#1){%
  \clearpage\xdef\headerpic{#1}\ltx@section
}
\def\SetHeaderpicDefault#1{%
  \xdef\headerpic{#1}
  \xdef\@headerpic@default{#1}
}
\makeatother

\renewcommand*\headrulewidth{0pt}
\setlength\headheight{40pt}
\SetHeaderpicDefault{388-2000x200.jpg}

\begin{document}
\section{Lorem Ipsum}
\lipsum
\section(495-2000x200.jpg){Lorem Ipsum}
\lipsum
\section(520-2000x200.jpg){Dolor Sit}
\lipsum
\section{Dolor Sit}
\lipsum
\end{document}

A major caveat, however, is that the \@section@HP macro enforces a new page to make sure the right picture is associated with the according section.

If you want to avoid the syntactic sugar of above soultion, one could of course go with something like

\section[<toc name>]{<full name>}
\SetHeaderpic{<file name>}

with \def\SetHeaderpic#1{\xdef\headerpic{#1}}.

You must log in to answer this question.

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