1

I just use latex to grid a pdf file.

the code like follow

\documentclass[ letterpaper, 10pt]{article}
\usepackage{tikz}
\usepackage{pdfpages}

\newcommand\su[1]{
\begin{tikzpicture}[overlay,remember picture]%
\node at (current page.north west){
 \begin{tikzpicture}[remember picture, overlay]
    \draw[very thin, blue!10,step=0.2in]
  (current page.south west) grid (current page.north east);
  \draw[very thin, red!20,step=1in]
  (current page.south west) grid (current page.north east);
  \end{tikzpicture}
};
\end{tikzpicture}%
}
\begin{document}

\includepdf[pages=-,scale=1,pagecommand={\su}]{Jan31.pdf}
\end{document}

compile command are

pdflatex xxx.pdf
xelatex  xxx.pdf

Step1: After pdflatex compiled, the pdf file did not show any grid.

enter image description here

Step2: Then I try to use xelatex to generate pdf file, its just show a little of grid on the bottom.

enter image description here

Step3: I try to use pdflatex to generate it again, the file is changed, it shows most of the grid but miss some on the top.

enter image description here

Step4: Use pdflatex again and again, just the 1 inch of the bottom missed.

enter image description here

Step5: use pdflatex again........ it work!!!!

enter image description here

After that, I did a lot of experiment.

If do not run xelatex, whether run how many times pdflatex, the pdf file cannot be grided(just like the picture of step1).

But if run one time xelatex, run several times pdflatex, the pdf file will be grided(just like the picture of step5)

version:

pdfTeX 3.1415926-2.5-1.40.14 (TeX Live 2013)
kpathsea version 6.1.1
Copyright 2013 Peter Breitenlohner (eTeX)/Han The Thanh (pdfTeX).
There is NO warranty.  Redistribution of this software is
covered by the terms of both the pdfTeX copyright and
the Lesser GNU General Public License.
For more information about these matters, see the file
named COPYING and the pdfTeX source.
Primary author of pdfTeX: Peter Breitenlohner (eTeX)/Han The Thanh (pdfTeX).
Compiled with libpng 1.6.1; using libpng 1.6.1
Compiled with zlib 1.2.7; using zlib 1.2.7
Compiled with xpdf version 3.03

xelatex -v
XeTeX 3.1415926-2.5-0.9999.3-2013052718 (TeX Live 2013)
kpathsea version 6.1.1
Copyright 2013 SIL International and Jonathan Kew.
There is NO warranty.  Redistribution of this software is
covered by the terms of both the XeTeX copyright and
the Lesser GNU General Public License.
For more information about these matters, see the file
named COPYING and the XeTeX source.
Primary author of XeTeX: Jonathan Kew.
Compiled with ICU version 51.1; using 51.1
Compiled with zlib version 1.2.7; using 1.2.7
Compiled with FreeType2 version 2.4.11; using 2.4.11
Compiled with Graphite2 version 1.2.1; using 1.2.1
Compiled with HarfBuzz version 0.9.15; using 0.9.15
Using Mac OS X Core Text, Cocoa & ImageIO frameworks
  • 1
    If you are able to get the grid, can you please tell us what is the problem/question :) – user11232 Feb 27 '14 at 10:27
  • Your code is fautly. \su needs an argument. So you should use pagecommand={\su{}} – Ulrike Fischer Feb 27 '14 at 11:35
6

Option remember picture remembers the position of tikzpicture in the .aux file and uses the remembered value in the next LaTeX run. In more complicate cases (as in the question with nested tikzpictures) it can take even more runs. In your case three runs are needed:

pdflatex xxx
pdflatex xxx
pdflatex xxx

or

xelatex xxx
xelatex xxx
xelatex xxx

But do not intermix pdfTeX and XeTeX, because the internal position values are not compatible.

The complexity of the example can be reduced with the result that only two runs are necessary:

\documentclass[letterpaper, 10pt]{article}
\usepackage{tikz}
\usepackage{pdfpages}

\newcommand\su{%
  \begin{tikzpicture}[
    overlay,
    remember picture,
    shift={(current page.north west)},
  ]
    \draw[very thin, blue!10, step=.2in]
      (current page.south west) grid (current page.north east);
    \draw[very thin, red!20,step=1in]
      (current page.south west) grid (current page.north east);
  \end{tikzpicture}%
}

\begin{document}
  \includepdf[pages=-,pagecommand={\su}]{Jan31.pdf}
\end{document}
  • This \su code has a little problem, the grid is not start at the top left corner of the pages. – tywtyw2002 Feb 27 '14 at 19:06
  • @tywtyw2002: Fixed by adding option shift to move the origin in the top left corner. – Heiko Oberdiek Feb 27 '14 at 20:02
3

For a change, you can use background package that provides \BgThispage macro:

\documentclass[ letterpaper, 10pt]{article}
\usepackage{pdfpages}
\usepackage[]{background}    %% this loads tikz

\backgroundsetup{
pages=some,
scale=1,
opacity=1,
angle=0,
color=black,
contents={%
 \begin{tikzpicture}[overlay,remember picture,shift={(current page.north west)}]
    \draw[very thin, blue!10,step=0.2in]
  (current page.south west) grid (current page.north east);
  \draw[very thin, red!20,step=1in]
  (current page.south west) grid (current page.north east);
  \end{tikzpicture}%
}
}
\begin{document}

\includepdf[pages=1-4,scale=1,pagecommand={\BgThispage}]{pgfmanual.pdf}
\end{document}

enter image description here

Compile this with pdflatex and 2-3 compilations will be needed for things to settle down.

Here is a very light weight solution using eso-pic:

\documentclass[ letterpaper, 10pt]{article}
\usepackage[grid,gridunit=mm,gridcolor=red!20,subgridcolor=blue!10]{eso-pic}
\usepackage{pdfpages}
\gridSetup[mm]{1mm}{1}{5}{20}{1}

\begin{document}

\includepdf[pages=1-4,scale=1]{pgfmanual.pdf}
\end{document}

enter image description here

  • Thanks, but it has the same problem with Heiko Oberdiek's answer, the grid not start at the top left corner of the page. – tywtyw2002 Feb 27 '14 at 19:08
  • @tywtyw2002 Heiko has already shown how to shift. I have added another light weight solution using eso-pic. – user11232 Feb 27 '14 at 23:24

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.