[Tex/LaTex] Fill rectangular area with PDF pattern like graph paper

pdfpdftexpositioning

Short version

I want to fill one or more rectangular areas on a page using a regular pattern. The area should span the page width and stretch vertically to fill remaining space. The pattern should be included in the generated PDF only once, using the PDF pattern facilities, as opposed to repeatedly emitting the same drawing instructions for each copy of the basic cell. Using specials that only work for pdflatexis acceptable.

Long version

My aim is to create sheets with alternating problem statements and answer areas. The answer areas (and only these) should be patterned using a grid of 5mm squares. (In Germany this is usually called “Karopapier”, and judging from Wikipedia, the US term probably would be “graph paper” or more specifically “quadrille paper”.)

Black lines for the squares are too small, and gray lines won't reproduce correctly in the final result from an offset printing press. So the squares have to be made up by tiny dots, 10 per edge.

I currently have code to do this, adapted from some other code I don't fully understand. It creates a single suqare by saving the result of a pictureenvironment to a box. That box is then repeated using \cleaders to fill one row, and repeated again to fill a block. It works, but the result has many drawbacks. Apparently the PDF will contain separate drawing instructions for each and every dot. This makes the files very large, causes my PDF viewer to become very slwo when scrolling through these documents, and will cost considerable time to print as well.

I know that it is possible to define patterns in PDF, and to fill shapes using these patterns. I want to leverage this from within the LaTeX document. I'd hope to write some macros so that I can simply invoke one macro like \graphpaper{\textwidth}{fill} or similar to create such a box. Everything else should be hidden behind the scenes.

Sketch of a possible solution

I have a rough idea of how I'd go about solving this, using features provided by the pdfTeX engine resp. the pdflatex command.

  1. Use \pdfsavepos to mark the start position
  2. Use \pdflastxpos and \pdflastypos to write that position to some helper file
  3. Create space for the box, using the dimensions provided
  4. Mark and write the end position like 1. and 2.
  5. Reserve an object number using \pdfobj reserveobjnum
  6. Write the object number to the output file as well
  7. Insert some \special as a placeholder for the box, referencing the above object
  8. Replace \output with something that calls the original \output up front
  9. Before doing the actual output, open the helper file for output
  10. After the output, read the file back in to obtain positions and object numbers
  11. For each region to be patterned, compute the required dimensions by subtracting marked positions and rounding down to a multiple of the square size.
  12. Create the corresponding object using \pdfobj useobjnum
  13. For the first page which needs it, create an object to represent the pattern itself

I'm not completely sure this would work as intended. In particular, I still have some doubts about whether I'd be able to include more than one region per page, and I'm also somewhat fuzzy whether the above single-pass approach would actually work.

Questions

My core question is this:

Do you know of any easier way to achieve what I'm trying to do?

I'll be happy about references to a package which already provides what I need. Lacking that, I'll also value references to code which does something similar, upon which I might build my stuff. If anyone has particular comments about the approach I outlined above, and whether it would work, that would be useful too.

Best Answer

Here's the beginning of a solution. Needs much more work, but I don't have time now. Maybe someone will pick this up and improve it. For example, it could properly hook into the pdftex colour stack mechanism...

\documentclass{article}
\usepackage{xcolor}
%First  define the pattern:
\immediate\pdfobj stream attr{%
/Type /Pattern
/PatternType 1
/PaintType 1
/TilingType 1
/BBox [0 0 20 20]
/XStep 14.1732283 % 5mm step
/YStep 14.1732283 % 5mm step
/Resources << >>}{%
2.83465 0 0 2.83465 0 0 cm % use mm as units
1 J % rounded caps (to make dots)
0.25 w % line 0.25 units wide
[0 0.5] 0 d % dots every 0.5 units
1 1 m 1 6 l 6 6 l S % draw two edges of the unit square
}
%Now add pattern to the page resource dictionary:
\edef\myn{\the\pdflastobj}
\begingroup
   \edef\x{\endgroup
     \pdfpageresources{%
       \the\pdfpageresources /Pattern <</Mydots \myn\space0 R>>
     }%
   }%
\x

%Define a command to use the pattern:
\makeatletter
\newcommand{\mypattern}[1]{\pdfliteral{/Pattern cs /Mydots scn}#1\reset@color}
\makeatother

\begin{document}
Normal:\hrule height 10pt

%draw whatever you want. here i am using some text and a solid rule for demonstration
\mypattern{\Huge\textbf{\textsf{Patterned:}}\hrule height 40pt}

Normal:\hrule height 10pt

\end{document}
Related Question