Roland's homepage

My random knot in the Web

Adding text or graphics to a PDF file (again)

In an earlier article I talked about using a picture environment in LaTeX to add lines and text over an image. In another article I showed how to draw with PostScript, including using Encapsulated PostScript to include arbitrary images.

In this article I want to cover the pros and cons of these methods and introduce a third method: TikZ.

Picture environment

The picture environment in LaTeX works with all back-end drivers. Because of this its drawing capabilities are limited; its functionality is the least common denominator of all possible drivers.

A definite plus is the fact that you can use all the LaTeX facilities for text.

PostScript

Since PostScript is a full-blown programming language, its vector graphics capabilities are practically unlimited. It can also embed bitmaps.

The language is stack-based and uses reverse Polish notation, which is easy for computers but not so much for humans. Luckily modern PostScript interpreters like Ghostscript are not strapped for memory so you can define variables instead of spending your time juggling values on the stack.

The biggest downside is that the font handling of PostScript predates Unicode. So while you basically can show any character in a font, it is not trivial to say show a string consisting of unicode code points or UTF-8.

TikZ

Although I’m just starting on my TikZ journey,I like it a lot already. It has taken some of the best features of pstricks, metapost and asymptote. And of course it uses TeX’s typesetting capabilities.

Compared to asymptote (and postscript) it has one major weakness IMO. Doing calculations with variables is trivial in asymptote, like in basically any programming language. This makes it easy to create parametric designs. In TiKZ this is rather cumbersome.

The documentation for TikZ is excellent. It comes with a extensive manual (currently 1161 pages). There are also a lot examples to find on texample.net and it is a frequent topic on tex.stackexchange.com.

Next to its basic capabilities TikZ also comes with a lot of libraries for specialized tasks. For example electrical circuits and arrows.

Here is the example from the earlier article, but then using the standalone documentclass and TikZ.

\documentclass[tikz=true]{standalone}
% The tikz=true option puts every tikzpicture on its own page.

\usepackage[quiet]{fontspec}

\setmainfont{Tech} % A handwriting-like font

\begin{document}

    \begin{tikzpicture}
        [align=left,anchor=south west,color=blue]
        % Original page
        \node[inner sep=0pt, anchor=south west]
            (orig) at (0,0) {\includegraphics{blz/blz-10.pdf}};
        % Add a fine grid
        %\draw[step=1cm,gray,very thin] (0,0) grid (21,29.7);
        % A courser grid
        %\draw[step=10cm,red,thin] (0,0) grid (21,29.7);
        % checkboxes
        \fill (11.58,21.4) rectangle ++(0.2,0.2);
        \fill (11.58,20.95) rectangle ++(0.2,0.2);
        \fill (11.58,9.4) rectangle ++(0.2,0.2);
        % Texts
        \draw (11.45,18.8) node {1234\,AB};
        \draw (11.45,17.25) node {56};
        \draw (11.45,12.6) node {Geenweg};
        \draw (11.45,11.1) node {Nergenshuizen};
        \draw (11.45,4.3) node {Nergenshuizen};
        \draw (11.45,3) node {C};
    \end{tikzpicture}

\end{document}

For comments, please send me an e-mail.


Related articles


←  Installing Pelican 4 Nikon D70s tips and tricks  →