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 05: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 –  Jul 28 '16 at 05:09
  • Nope. Even running xelatex three times, I get the same result :( –  Jul 28 '16 at 05: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 05: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. –  Jul 28 '16 at 05:16
  • @Make sense. I don't use xelatex so can't help you further. – Peter Grill Jul 28 '16 at 05:18
  • @PeterGrill Don't worry. At least it's nice to see it worked with another engine. –  Jul 28 '16 at 05:23
  • Why not with eso-pic package – Salim Bou Jul 28 '16 at 07:24
  • @salimbou I didn't know about it... Till now. Can you please tell me an example? –  Jul 28 '16 at 07:26

1 Answers1

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

Salim Bou
  • 17,021
  • 2
  • 31
  • 76