如何在页面上放置背景图像(使用 tikz?)

如何在页面上放置背景图像(使用 tikz?)

我想在文档中特定页面的所有内容后面放置一张图片,使其更生动一些。我尝试做的第一件事是将 tikz 图片作为背景,效果很好,但不幸的是标题被覆盖了。

以下是迄今为止我使用过的基本代码及其输出:

\documentclass[11pt,a4paper,twoside]{scrbook}
\RequirePackage{microtype}
\RequirePackage{pdfpages}

\usepackage{lipsum}
\usepackage{lettrine}
\usepackage{tikz}
\usepackage{multicol}

% Dimensions:
\setlength\topmargin{-58pt}                 
\setlength\headheight{20pt}                 
\setlength\headsep{25pt}                 
\setlength\marginparwidth{-20pt}            
\setlength\textwidth{\paperwidth - 82pt}    
\setlength\textheight{\paperheight - 112pt} 
\setlength\oddsidemargin{-30pt}
\setlength\evensidemargin{-30pt}

% Header and Footer:
\usepackage{fancyhdr}
% Header
\lhead[\thepage]{Edition}
\rhead[Name]{\thepage}
\chead{\sffamily HEADER}
% Headercolour
\renewcommand{\headrulewidth}{1pt}% 2pt header rule
\renewcommand{\headrule}{\hbox to\headwidth{%
\color{black}\leaders\hrule height \headrulewidth\hfill}}
% Footer
\lfoot{}
\rfoot{}
\cfoot{\vspace{-20pt}\textcolor{black}{\rule{\textwidth}{1pt}}}

% Fonts:
\usepackage{mathptmx}   % Serif
\usepackage{DejaVuSans} % Sans-serif
\usepackage[T1]{fontenc}
\usepackage{xstring}

\begin{document}

\noindent
\begin{tikzpicture}[remember picture,overlay]
\node (background) at (0.5\textwidth,-0.5\textheight+15pt) {\includegraphics[height=\paperheight+20pt]{bak}};
\draw [white , fill=white, opacity=0.7](-15pt,-\topmargin+5pt) rectangle (\textwidth+10pt,-\topmargin-\headheight-10pt);
\draw [white , fill=white, opacity=0.7](-15pt,15pt) rectangle (\textwidth+10pt,-\textheight);
\draw [white , fill=white, opacity=0.7](-15pt,-\textheight-22pt) rectangle (\textwidth+10pt,-\textheight-30pt);
\end{tikzpicture}
\pagestyle{fancy}

\begin{multicols}{2}
\lettrine[loversize=0.1]{A}{S} I said, this is a test. \lipsum
\end{multicols}
\clearpage

\begin{multicols}{2}
\lipsum
\end{multicols}

\end{document}

输出

请注意,在这两个页面中,只有第二个页面有页眉。在另一页中,tikz 图片绘制在页眉上方。如果有某种方法可以让 tikz 在页眉下方绘制,甚至强制重新绘制页眉,那将解决我的问题。但我不知道如何做这些。

还请注意页脚出现,而出于某种原因,标题消失在 tikz 绘图后面。我不知道该怎么做。

有任何想法吗?

答案1

这里有一个建议,需要最新的 KOMA-Script:版本 3.16 或更新版本。然后,您可以使用scrlayer-scrpagefancyhdr为带有背景图像的页面定义新的页面样式。

\documentclass[11pt,a4paper,twoside]{scrbook}
\usepackage{microtype}
\usepackage{pdfpages}

\usepackage{lipsum}
\usepackage{lettrine}
\usepackage{tikz}
\usepackage{multicol}

% Dimensions:
\usepackage{geometry}
\geometry{
  top=59pt,
  headheight=20pt,
  headsep=20pt,
  bottom=53pt,
  footskip=50pt,
  %marginparwidth=-20pt,
  hmargin=41pt
  }

\addtokomafont{pagehead}{\normalfont}
\usepackage[headsepline=1pt,footsepline=1pt]{scrlayer-scrpage}%<- activates pagestyle scrheadings
\clearpairofpagestyles
\newcommand*{\headcontents}[1]{%
  \raisebox{0pt}[\ht\strutbox][\dimexpr\headheight-\ht\strutbox\relax]{#1}}
\lehead{\headcontents{\pagemark}}
\lohead{\headcontents{Edition}}
\rehead{\headcontents{Name}}
\rohead{\headcontents{\pagemark}}
\chead{\sffamily\headcontents{HEADER}}

\newpairofpagestyles[scrheadings]{backgroundimage}{}
\makeatletter
\newcommand*\@backgroundimage{}
\newcommand*\backgroundimage[1]{%
  \ifstr{#1}{}{\gdef\@backgroundimage{}}{%
    \gdef\@backgroundimage{\includegraphics[height=\dimexpr\paperheight+20pt\relax]{#1}}%
}}

\colorlet{backgroundcolor}{white}
\newcommand*\coloredbg{%
  \tikz\fill[backgroundcolor,opacity=.7](0,0)rectangle({\layerwidth},{\layerheight});}

\DeclareNewLayer[background,textarea,
  addvoffset=-5pt,addhoffset=-5pt,addwidth=10pt,addheight=10pt,
  contents=\coloredbg
]{text.bg}
\DeclareNewLayer[background,foot,align=b,
  addvoffset=9pt,addhoffset=-5pt,addwidth=10pt,height=12pt,
  contents=\coloredbg
]{foot.bg}
\DeclareNewLayer[background,head,
  addvoffset=11pt,addhoffset=-5pt,addwidth=10pt,addheight=13pt,
  contents=\coloredbg
]{head.bg}
\DeclareNewLayer[background,
  area={.5\paperwidth}{.5\paperheight}{0pt}{0pt},
  contents={\makebox[0pt]{%
    \raisebox{0pt}[.5\height][.5\height]{\@backgroundimage}%
  }}
]{image.bg}
\AddLayersAtBeginOfPageStyle{backgroundimage}{text.bg,foot.bg,head.bg,image.bg}
\makeatother

\usepackage{mathptmx}
\usepackage{DejaVuSans}
\usepackage[T1]{fontenc}
\usepackage{xstring}

\usepackage{mwe}% for the example images

\begin{document}
\backgroundimage{example-image}%<- set the background image
\thispagestyle{backgroundimage}%<- use the background image only on the current page
\begin{multicols}{2}
\lettrine[loversize=0.1]{A}{S} I said, this is a test. \lipsum
\lipsum
\end{multicols}
\clearpage

\backgroundimage{example-image-A}%<- change the background image
\pagestyle{backgroundimage}%<- change the pagestyle
\begin{multicols}{2}
\lipsum\lipsum
\end{multicols}
\end{document}

在此处输入图片描述

答案2

这是一个解决方案

\documentclass[11pt,a4paper,twoside]{scrbook}
\RequirePackage{microtype}
\RequirePackage{pdfpages}

\usepackage{lipsum}
\usepackage{lettrine}
\usepackage{tikz}
\usepackage{multicol}

% Dimensions:
\setlength\topmargin{-58pt}                 
\setlength\headheight{20pt}                 
\setlength\headsep{25pt}                 
\setlength\marginparwidth{-20pt}            
\setlength\textwidth{\paperwidth - 82pt}    
\setlength\textheight{\paperheight - 112pt} 
\setlength\oddsidemargin{-30pt}
\setlength\evensidemargin{-30pt}

% Header and Footer:
\usepackage{fancyhdr}
% Header
\lhead[\thepage]{Edition}
\rhead[Name]{\thepage}
\chead{\sffamily HEADER}
% Headercolour
\renewcommand{\headrulewidth}{1pt}% 2pt header rule
\renewcommand{\headrule}{\hbox to\headwidth{%
\color{black}\leaders\hrule height \headrulewidth\hfill}}
% Footer
\lfoot{}
\rfoot{}
\cfoot{\vspace{-20pt}\textcolor{black}{\rule{\textwidth}{1pt}}}

% Fonts:
\usepackage{mathptmx}   % Serif
\usepackage{DejaVuSans} % Sans-serif
\usepackage[T1]{fontenc}
\usepackage{xstring}

\begin{document}

\makeatletter
\def \@begindvi{%
  \unvbox \@begindvibox
\begin{tikzpicture}[remember picture,overlay]
\node (background) at (0.5\textwidth,-0.5\textheight+15pt) {\includegraphics[height=\paperheight+20pt]{bak}};
\draw [white , fill=white, opacity=0.7](-15pt,-\topmargin+5pt) rectangle (\textwidth+10pt,-\topmargin-\headheight-10pt);
\draw [white , fill=white, opacity=0.7](-15pt,15pt) rectangle (\textwidth+10pt,-\textheight);
\draw [white , fill=white, opacity=0.7](-15pt,-\textheight-22pt) rectangle (\textwidth+10pt,-\textheight-30pt);
\end{tikzpicture}
  \global\let \@begindvi \@empty
}
\makeatother

\pagestyle{fancy}

\begin{multicols}{2}
\lettrine[loversize=0.1]{A}{S} I said, this is a test. 
\lipsum
\end{multicols}
\clearpage

\begin{multicols}{2}
\lipsum
\end{multicols}

\end{document}

编辑:通过命令使用它\mtbg{巴克}

    \def\mtbak#1{%
\begin{tikzpicture}[remember picture,overlay]
\node (background) at (0.5\textwidth,-0.5\textheight+15pt) {\includegraphics[height=\paperheight+20pt]{#1}};
\draw [white , fill=white, opacity=0.7](-15pt,-\topmargin+5pt) rectangle (\textwidth+10pt,-\topmargin-\headheight-10pt);
\draw [white , fill=white, opacity=0.7](-15pt,15pt) rectangle (\textwidth+10pt,-\textheight);
\draw [white , fill=white, opacity=0.7](-15pt,-\textheight-22pt) rectangle (\textwidth+10pt,-\textheight-30pt);
\end{tikzpicture}}
\makeatletter
\def\mtbg#1{%
\def \@begindvi{%
  \unvbox \@begindvibox
  \mtbak{#1}
  \global\let \@begindvi \@empty
}}
\makeatother

相关内容