![使用小背景图像在纵向和横向文档之间切换的最有效方法是什么](https://linux22.com/image/448130/%E4%BD%BF%E7%94%A8%E5%B0%8F%E8%83%8C%E6%99%AF%E5%9B%BE%E5%83%8F%E5%9C%A8%E7%BA%B5%E5%90%91%E5%92%8C%E6%A8%AA%E5%90%91%E6%96%87%E6%A1%A3%E4%B9%8B%E9%97%B4%E5%88%87%E6%8D%A2%E7%9A%84%E6%9C%80%E6%9C%89%E6%95%88%E6%96%B9%E6%B3%95%E6%98%AF%E4%BB%80%E4%B9%88.png)
我正在为我的数学课设计工作表,我想在每个文档的顶角放置学校徽标和班级徽标。我的工作表有很多元素,我发现如果我在背景中制作图片,它不会导致任何放置错误,也不会跳过浮动对象的任何线条。
我的问题是,我经常(随机地)在纵向和横向文档之间切换。如果我只有纵向工作表,那么我只需使用 \BgThispage 一行即可添加徽标。但是,当我切换到横向时,每次进行此切换时,我都必须重新进行背景设置。
我想知道是否有更有效的方法来做到这一点。也许像存储 2 个不同的背景设置并根据工作表的方向调用我需要的那个?
下面是我所拥有的一个例子:
\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}
答案1
我认为背景包和 tikz 对此来说是完全不需要的(并且总是需要两次编译。我只需使用 shipout 钩子即可。
\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}