2

UPDATE: I'm using XeLaTeX, as I use polyglossia and fontspec packages. @PeterGrill pointed out that with pdflatex the picture is perfectly placed.

I'm trying to put a TikZ picture as background for all the pages of a document. I've used the background package and it's wonderful for external images. I followed the advice given here to do the same with TikZ pictures, but I'm having problems with the position of the background. Perhaps it's a problem with my TikZ code, but I'm not sure.

Here is the minimal code (I compiled it with 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}

And here's an image of my output:

The output of the given LaTeX code: a page with a margin in wrong position

As you can see, the margin is misplaced.

Any help about this will be appreciated.

  • 1
    Did you try running it twice (might need three times)? Oh, also add a % following \end{tikzpicture} i.e, it should be \end{tikzpicture}%. – Peter Grill Jul 28 '16 at 5:07
  • Let me do it, because I was using latexmk but I'm not sure. And yes, I forgot to put the %. It was on my original document :P – user101590 Jul 28 '16 at 5:09
  • Nope. Even running xelatex three times, I get the same result :( – user101590 Jul 28 '16 at 5:13
  • Some issue with xelatex then. Works fine for me with pfdlatex. I didn't look at the tags before so did not notice that. Yes, good to explicetly point that out in the question. – Peter Grill Jul 28 '16 at 5:14
  • @PeterGrill I'll try with pdflatex. The reason I'm using xelatex is because I use polyglossia for Spanish documents. And I tend to change the font with fontspec. – user101590 Jul 28 '16 at 5:16
1

You can insert your background picture using eso-pic rather then background, to add command (picture) which can be shipped out at every new page you can use \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}

Output with xelatex

enter image description here

  • Thanks a lot! It works like a charm! :) – user101590 Jul 28 '16 at 7:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy