将 tikzpicture 作为带有背景包的所有页面的背景时出现问题

将 tikzpicture 作为带有背景包的所有页面的背景时出现问题

更新:我正在使用 XeLaTeX,因为我使用polyglossiafontspec包。@PeterGrill 指出pdflatex图片的位置非常完美。

我正在尝试将 TikZ 图片作为文档所有页面的背景。我使用过该background软件包,它非常适合处理外部图像。我遵循了给出的建议这里对 TikZ 图片执行相同操作,但我在背景位置方面遇到了问题。也许是我的 TikZ 代码有问题,但我不确定。

这是最小代码(我用 XeLaTeX 编译了它):

\documentclass[12pt]{article}

% Paper size and margins
\usepackage{geometry}
\geometry{paper=letterpaper,margin=2cm}

% Colors
\usepackage[dvipsnames]{xcolor}

% Tikz package and libraries
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\usetikzlibrary{calc}

% Figure definitions
\tikzset{
  ballSet/.pic = {
    \begin{scope}[shading=ball,opacity=0.25]
      \shade[ball color=red] ($(0, 2.25)$) circle (1.5cm);
      \shade[ball color=green] ($(2.25, 0)$) circle (1.5cm);
      \shade[ball color=blue] ($(0, -2.25)$) circle (1.5cm);
      \shade[ball color=yellow] ($(-2.25, 0)$) circle (1.5cm);
    \end{scope}
  },
  margin/.pic = {
    \coordinate (ballA) at ($(current page.north west) + (1.5, -1.5)$);
    \coordinate (ballB) at ($(current page.north east) + (-1.5, -1.5)$);
    \coordinate (ballC) at ($(current page.south east) + (-1.5, 1.5)$);
    \coordinate (ballD) at ($(current page.south west) + (1.5, 1.5)$);

    \draw[color=Brown!50!white, line width=1mm, dash pattern=on 25pt off 5pt on 3pt off 5pt, double distance=1mm] (ballA) rectangle (ballC);

    \begin{scope}[shading=ball]
      \foreach \pos / \col in {(ballA)/red,(ballB)/green,(ballC)/blue,(ballD)/yellow}
        \shade[ball color=\col] \pos circle (0.4cm);
    \end{scope}
  },
  firstPage/.pic = {
    \path (current page.center) pic {margin};
    \path ($(current page.center) + (4.65,-7.75)$) pic {ballSet};
  }
}

% Command to insert margin
\newcommand{\Margin}{%
  \begin{tikzpicture}[remember picture,overlay,x=1cm,y=1cm]
    \path (current page.center) pic {margin};
  \end{tikzpicture}%
}

% Background
\usepackage[pages=all]{background}

\backgroundsetup{
  scale=1,
  color=black,
  opacity=1.0,
  angle=0,
  contents={\Margin}
}

\begin{document}
  Hello world!
\end{document}

这是我的输出图像:

给定的 LaTeX 代码的输出:页边距位置错误的页面

正如您所见,边距放错了位置。

任何有关此事的帮助都将受到感谢。

答案1

您可以使用插入背景图片eso-pic而不是background,添加可以在每个新页面上发送的命令(图片),你可以使用\AddToShipoutPictureBG{code_for_your_picture}

\documentclass[12pt]{article}

% Paper size and margins
\usepackage{geometry}
\geometry{paper=letterpaper,margin=2cm}

% Colors
\usepackage[dvipsnames]{xcolor}

% Tikz package and libraries
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\usetikzlibrary{calc}

\usepackage{eso-pic}

% Figure definitions
\tikzset{
  ballSet/.pic = {
    \begin{scope}[shading=ball,opacity=0.25]
      \shade[ball color=red] ($(0, 2.25)$) circle (1.5cm);
      \shade[ball color=green] ($(2.25, 0)$) circle (1.5cm);
      \shade[ball color=blue] ($(0, -2.25)$) circle (1.5cm);
      \shade[ball color=yellow] ($(-2.25, 0)$) circle (1.5cm);
    \end{scope}
  },
  margin/.pic = {
    \coordinate (ballA) at ($(current page.north west) + (1.5, -1.5)$);
    \coordinate (ballB) at ($(current page.north east) + (-1.5, -1.5)$);
    \coordinate (ballC) at ($(current page.south east) + (-1.5, 1.5)$);
    \coordinate (ballD) at ($(current page.south west) + (1.5, 1.5)$);

    \draw[color=Brown!50!white, line width=1mm, dash pattern=on 25pt off 5pt on 3pt off 5pt, double distance=1mm] (ballA) rectangle (ballC);

    \begin{scope}[shading=ball]
      \foreach \pos / \col in {(ballA)/red,(ballB)/green,(ballC)/blue,(ballD)/yellow}
        \shade[ball color=\col] \pos circle (0.4cm);
    \end{scope}
  },
  firstPage/.pic = {
    \path (current page.center) pic {margin};
    \path ($(current page.center) + (4.65,-7.75)$) pic {ballSet};
  }
}

% Command to insert margin
\newcommand{\Margin}{%
  \begin{tikzpicture}[remember picture,overlay,x=1cm,y=1cm]
    \path (current page.center) pic {margin};
  \end{tikzpicture}%
}

\AddToShipoutPictureBG{\Margin}

\begin{document}
  Hello world!
\end{document}

使用 xelatex 输出

在此处输入图片描述

相关内容