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 pdflatex
is 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 picture
environment 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.
- Use
\pdfsavepos
to mark the start position - Use
\pdflastxpos
and\pdflastypos
to write that position to some helper file - Create space for the box, using the dimensions provided
- Mark and write the end position like 1. and 2.
- Reserve an object number using
\pdfobj reserveobjnum
- Write the object number to the output file as well
- Insert some
\special
as a placeholder for the box, referencing the above object - Replace
\output
with something that calls the original\output
up front - Before doing the actual output, open the helper file for output
- After the output, read the file back in to obtain positions and object numbers
- For each region to be patterned, compute the required dimensions by subtracting marked positions and rounding down to a multiple of the square size.
- Create the corresponding object using
\pdfobj useobjnum
- 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.
picture
and\multiput
would do). This would reduce the problem to determining the empty space on a page.picture
has the problem that it will output all dot drawing commands to PDF, with the problems I described. Furthermore, stretching apicture
to fill available space appears nontrivial, particularly in a single-pass setup. I guess that if I knew the size, I might be able to code suitable PDF drawing instructions to draw the lines using\special
, but if I got that far, doing the pattern fill doesn't seem a big step either.