7

I have been trying to add content to a (proprietary) PDF with LaTeX by using pdfpages to inlude the original pages and draw on them using TikZ. This is the code I have been using:

\documentclass[a4paper]{article}
\usepackage{tikz,pdfpages}
\begin{document}
  \includepdf[pages=1,pagecommand={%
    \begin{tikzpicture}[overlay]
      \node[circle,fill=red] at (0,0) {0};
      \node[circle,fill=red] at (1,1) {1};
    \end{tikzpicture}%
  }]{pgfmanual.pdf}%
\end{document}

This basically works but the coordinate system is weird:

failing example

Both origin and grid unit seem to be chosen rather arbitrarily. For more convenient/intuitive use, I would like to have (0,0) in the lower-left corner -- just like a normal TikZ picture. How is this possible?

  • Come to think of it, the origin is probably put at "the position" of the TikZ picture, which would be before the first character of the page. If so, the origin moves when margins change. – Raphael Sep 10 '13 at 5:52
4

Here is a simple solution using the special node named current page with the shift and remember picture options:

\documentclass[a4paper]{article}
\usepackage{tikz,pdfpages}

\newcommand\superposition[1]{%
  \begin{tikzpicture}[overlay,remember picture,
    shift={(current page.south west)}]
    \node[circle,fill=red] at (0,0) {0};
    \node[circle,fill=red] at (1,1) {1};
  \end{tikzpicture}%
}
\begin{document}
  \includepdf[pages=1,pagecommand={\superposition}]{pgfmanual.pdf}%
\end{document}
  • Nice! I seem to remember current page only works after shipout; does includepdf execute pagecommand at that time, anyway? – Raphael Sep 10 '13 at 5:53
  • @Raphael I don't remember this detail... The remember picture option requires two compilations. – Paul Gaborit Sep 10 '13 at 12:49
6

There are two components here:

  1. Moving the picture's origin to the lower left corner.
  2. Changing the grid which TikZ uses.

The former can not be done before the page is shipped because only then are the page dimensions known, apparently. eso-pic -- already loaded by pdfpages -- provides facilities to add the TikZ drawing after shipout.

The latter can be achieved via TikZ options x and y for the unit step in horizontal and vertical direction, respectively. Note that you can flip the axes by using negative values.

Combining the two, you get:

\documentclass[a4paper]{article}
\usepackage{tikz,pdfpages}
\begin{document}
  \includepdf[pages=1,pagecommand={%
    \AddToShipoutPictureBG*{\put(0,0){%
      \begin{tikzpicture}[overlay,x=1cm,y=1cm]
        \node[circle,fill=red] at (0,0) {0};
        \node[circle,fill=red] at (1,1) {1};
      \end{tikzpicture}
    }}
  }]{pgfmanual.pdf}%
\end{document}

If you prefer a relative grid, you can use

x=\paperwidth,y=\paperheight

instead so (1,1) is in the upper-right corner of the page.

  • Nice to see the humble \put together with TikZ:) – Yiannis Lazarides Sep 9 '13 at 21:15
  • @YiannisLazarides: Humble for some, document-saver for others. – Raphael Sep 9 '13 at 21:31

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.