1

Basically I have a collection of small images, all having the same dimensions. I want to insert random ones from those collections on every page created. That means that page 1 would have a different image than page 2 and page 3 would have a different one than page 2 and so on. I know how to insert an image in the fancy header of the top right corner. But, I don't know if it is actually possible to have it randomly inserted or at least some sort of rotation from a pre-defined list? The following is what I use:

% just to make warning go away
\setlength\headheight{26pt}

\pagestyle{fancy}
\fancyhf{}
\rfoot{\thepage}

and this is what I have in the main document itself:

\section{Test}
\rhead{\includegraphics[scale=0.4]{invader_1}}

However, that makes put each section in a new page, and the paper doesn't look good at that point.

I appreciate if someone would also explain the solution if it exists.

halsten
  • 23
  • Welcome to TeX.SX! Which engine do you use (pdflatex, xelatex, lualatex) and would it be possible to switch? – TeXnician Mar 17 '17 at 07:02

2 Answers2

2

Not random but simple for numbered images as img1.png, img2.png, etc:

\documentclass{article}
\usepackage{graphicx}
\usepackage{fancyhdr}
\fancyhead[r]{\includegraphics[width=1cm]{img\thepage}}
\begin{document}
\pagestyle{fancy}
Text \newpage
Text \newpage
Text \newpage
Text \newpage
\end{document}

If you have a few images (said form img10.png to img13.png) for many pages, you can use another counter with some conditional:

\documentclass{article}
\newcounter{img}
\setcounter{img}{10}
\usepackage{ifthen}
\usepackage{graphicx}
\usepackage{fancyhdr}
\fancyhead[r]{ \ifnum\value{img}>13%
\setcounter{img}{10}\else%
\addtocounter{img}{1}\fi%
\includegraphics[width=1cm]{img\theimg}}
\begin{document}
\pagestyle{fancy}
Text \newpage
Text \newpage
Text \newpage
Text \newpage
Text \newpage
Text \newpage
Text \newpage
Text \newpage
Text \newpage
Text \newpage
Text \newpage
Text \newpage
\end{document}
Fran
  • 80,769
  • this works only if you have less pages than images – jakun Mar 17 '17 at 09:04
  • my answer shows an approach that works with less images – jakun Mar 17 '17 at 09:10
  • @jakun I saw, but is not so simple. Anyway, I added another solution that work also with less images than pages. – Fran Mar 17 '17 at 09:42
  • Thanks for the solution, it is simple and works, however it is just a matter of personal preference and it suits my situation more so I will go with the answer that @jakun proposed. Thanks again though! – halsten Mar 17 '17 at 16:09
  • @halsten You're welcome. BTW, you can also upvote any answer that you like (in the pen tip over the left number) including the accepted answer. – Fran Mar 17 '17 at 18:56
  • @Fran: My apologies, I didn't actually know that, and I went ahead and upvoted it. However my reputation is still lower than 15. So it said that once it reaches that level it will reflect accordingly. Again, thank you for your answer and let me know if there is something else I can do to provide a mean to show the value of your answer. – halsten Mar 18 '17 at 03:15
  • Enough, thanks, although I do not care about my answer but about an accepted answer with 0 votes (btw, no longer, +1 @jakun) – Fran Mar 18 '17 at 05:41
1

As suggested by this answer you can use the lcg package to generate random numbers. One setups the range while loading the package (or later with \reinitrand). With \rand a new random number can be generated. By default this random number will be saved in the rand counter (see lcg documentation page 1).

\documentclass{article}

\usepackage{fancyhdr}
\usepackage{graphicx}
\usepackage[first=1, last=6]{lcg}

\newcommand{\iconHeight}{2cm}
\usepackage[%
    headheight=\iconHeight, 
    includehead, top=2cm,
]{geometry}

\fancyhead{}
\fancyhead[r]{%
    \rand
    \includegraphics[height=\iconHeight]{img/header/icon\therand}%
}
\pagestyle{fancy}


\usepackage{blindtext}

\begin{document}
    \Blinddocument
\end{document}

Rotation:

\documentclass{article}

\usepackage{fancyhdr}
\usepackage{graphicx}

\newcommand{\iconHeight}{2cm}
\usepackage[%
    headheight=\iconHeight, 
    includehead, top=2cm,
]{geometry}

\newcounter{IconCounter}
\newcommand{\theIconCounterMin}{1}
\newcommand{\theIconCounterMax}{6}
\setcounter{IconCounter}{\theIconCounterMin}
\fancyhead{}
\fancyhead[r]{%
    \includegraphics[height=\iconHeight]{img/header/icon\theIconCounter}%
    \stepcounter{IconCounter}%
    \ifnum\theIconCounter>\theIconCounterMax\relax
        \setcounter{IconCounter}{\theIconCounterMin}%
    \fi
}
\pagestyle{fancy}


\usepackage{blindtext}

\begin{document}
    \Blinddocument
\end{document}
jakun
  • 5,981
  • Thanks a lot. This is pretty much what I had thought it would be like, but had no idea on whether its possible or not. Thank you! – halsten Mar 17 '17 at 16:10