0

I am designing worksheets for my math class and I want to have a school logo and class logo at the top corners of each document. My worksheets have lots of elements and I found that if I made the pictures in the background, it would not cause any placement errors and not skip any lines from a floating object.

My problem is that I frequently (and randomly) switch between portrait and landscape documents. If I only had portrait worksheets, then I could just use 1 line to add the logos with \BgThispage. However, when I switch to landscape, I have to redo the background setup...every single time I make this switch.

I was wondering if there was a more efficient way to do this. Maybe like storing 2 different background setups and calling the one I need based on the orientation of the worksheet?

Here is an example of what I have:

\documentclass{exam}
\usepackage{lscape} % to allow pages to be temporarily landscape instead of portrait
\usepackage[margin=3cm]{geometry} % set margins to 3cm
\usepackage{tikz} % to overlay pictures
\usepackage{pdflscape} % allows you to see landscape without turning your head 90 degrees and allows landscape on any page with \begin{landscape}
%\usepackage{showframe} % make margins visible - useful for debugging
\usepackage[pages=some]{background} % make pictures in background so pictures don't skip lines when used at the top

\begin{document}
% [Page 1] ---------------------------------------------------------------------
\begin{landscape}
% Landscape Pictures
\backgroundsetup{opacity=1, scale=1, angle=0, contents={%
    \begin{tikzpicture}[remember picture,overlay]
    \node[xshift=3cm,yshift=3cm,anchor=south west] at (current page.south west){%
    \includegraphics[width=3cm, angle=90]{example-image-a}};
    \node[xshift=3cm,yshift=-3cm,anchor=north west] at (current page.north west){%
    \includegraphics[width=3cm, angle=90]{example-image-b}};
\end{tikzpicture}}}
\BgThispage % Add logos to the top corners

\begin{center}
    This is a landscape page
\end{center}

\end{landscape}

\pagebreak

% [Page 2] ---------------------------------------------------------------------

\restoregeometry % this fixes the page number being way too high. I seem to need this every time I switch from portrait to landscape

% Now I need to declare a backgroundsetup since this page has a different orientation than the last

% Portrait Pictures
\backgroundsetup{opacity=1, scale=1, angle=0, contents={%
    \begin{tikzpicture}[remember picture,overlay]
    \node[xshift=3cm,yshift=-3cm,anchor=north west] at (current page.north west){%
    \includegraphics[width=3cm]{example-image-a}};
    \node[xshift=-3cm,yshift=-3cm,anchor=north east] at (current page.north east){%
    \includegraphics[width=3cm]{example-image-b}};
\end{tikzpicture}}}

\BgThispage % Add logos to the top corners

\begin{center}
    This is a portrait page
\end{center}

\pagebreak

% [Page 3] ---------------------------------------------------------------------

\BgThispage

\begin{center}
    This is another portrait page that doesn't need the\\
    background setup since it was used in the last page.
\end{center}

\pagebreak

% [Page 4] ---------------------------------------------------------------------

\begin{landscape}

% Landscape Pictures
\backgroundsetup{opacity=1, scale=1, angle=0, contents={%
    \begin{tikzpicture}[remember picture,overlay]
    \node[xshift=3cm,yshift=3cm,anchor=south west] at (current page.south west){%
    \includegraphics[width=3cm, angle=90]{example-image-a}};
    \node[xshift=3cm,yshift=-3cm,anchor=north west] at (current page.north west){%
    \includegraphics[width=3cm, angle=90]{example-image-b}};
\end{tikzpicture}}}

\BgThispage % Add logos to the top corners

\begin{center}
    This is a landscape page that needs another background setup\\
    since it is a different orientation than the last page
\end{center}

\end{landscape}

\end{document}

Page 1 Page 2 Page 3 Page 4

1 Answer 1

1

imho the background package and tikz are quite unneeded for this (and always require two compilations. I would simply use the shipout hooks.

\documentclass{exam}
\usepackage[paper=a4paper,margin=3cm]{geometry} % set margins to 3cm
\usepackage{pdflscape} %
\usepackage{graphicx,tikz}

\newcommand\placelogos{
 \put(0,0){\tikz[overlay]\draw[red,<->](0,0)--++(3cm,-3cm);} %only for test
 \put(3cm,-3cm){\raisebox{-\height}{\includegraphics[width=3cm]{example-image-a}}}
 \put(3cm,-\paperheight+3cm){\includegraphics[width=3cm]{example-image-b}}
 \put(\paperwidth-6cm,-3cm){\raisebox{-\height}{\includegraphics[width=3cm]{example-image-c}}}
 \put(\paperwidth-6cm,-\paperheight+3cm){\includegraphics[width=3cm]{example-image}}}
 
\newcommand\placelogoslandscape{
 \put(0,0){\tikz[overlay]\draw[red,<->](0,0)--++(3cm,-3cm);} %only for test
 \put(3cm,-6cm){\includegraphics[width=3cm,angle=90]{example-image-a}}
 \put(3cm,-\paperheight+3cm){\includegraphics[width=3cm,angle=90]{example-image-b}}
 \put(\paperwidth-3cm,-6cm){\llap{\includegraphics[width=3cm,angle=90]{example-image-c}}}
 \put(\paperwidth-3cm,-\paperheight+3cm){\llap{\includegraphics[width=3cm,angle=90]{example-image}}}}
 
 \AddToHook{env/landscape/after}{\restoregeometry} %to get around the exam incompabitily
\begin{document}
% [Page 1] ---------------------------------------------------------------------
\begin{center}
    This is a portrait page
\end{center}



\begin{landscape}
% Landscape Pictures
\AddToHookNext{shipout/background}{\placelogoslandscape}

\begin{center}
    This is a landscape page
\end{center}

\end{landscape}


\AddToHookNext{shipout/background}{\placelogos}


\begin{center}
    This is a portrait page
\end{center}

\pagebreak

% [Page 3] ---------------------------------------------------------------------

\begin{center}
    This is another portrait page that doesn't need the\\
    background setup since it was used in the last page.
\end{center}

\pagebreak

% [Page 4] ---------------------------------------------------------------------

\begin{landscape}

\AddToHookNext{shipout/background}{\placelogoslandscape}
\begin{center}
    This is a landscape page that needs another background setup\\
    since it is a different orientation than the last page
\end{center}

\end{landscape}

\end{document}

You must log in to answer this question.

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