Skip to contents

puzzleGrob() creates a grid grob of a complete puzzle page: arithmetic problems (see generate_math_problems()) on one side and the number grid (see numberGridGrob()) on the other side of a landscape page.

Usage

puzzleGrob(
  segments,
  op = "auto",
  max_factor = 12L,
  seed = sample.int(1000000L, 1L),
  hash = NULL,
  problems_side = c("left", "right"),
  margin = unit(0.25, "in"),
  gp = gpar(col = "black", fill = "black", lwd = 2),
  name = NULL,
  vp = NULL
)

Arguments

segments

A list of numeric vectors of numbers from 1 to 100. The first element is the (hidden) picture sequence the math problems are generated from; any later elements are drawn in the number grid.

op, max_factor

Passed to generate_math_problems().

seed

Random seed used to generate the math problems (and included in the page annotation). The default picks a random one.

hash

A short identifier included in the page annotation, e.g. the hash element added by read_puzzles() (which identifies the puzzle without spoiling the picture), or NULL to omit.

problems_side

Which panel the math problems go on: "left" (default, so a right-handed kid drawing on the grid doesn't cover their math solutions with their writing hand) or "right" (likely preferable for a left-handed kid). The number grid goes on the other panel.

margin

Printer margin around each panel (a grid::unit()).

gp

A grid::gpar() object.

name

A character identifier (for the grob).

vp

A grid::viewport() object (or NULL).

Value

A grid::gTree() object.

Details

The math problems are randomly generated using seed (via withr::with_seed(), so the caller's random number generator state is left undisturbed). The page is annotated in the bottom corner of the number-grid panel with hash (if any) and the seed so an exact page can be recreated later.

Examples

# a landscape letter PDF ready to print
if (isTRUE(capabilities("cairo")) && require("grid")) {
    puzzle <- read_puzzles()$rook
    f <- tempfile(fileext = ".pdf")
    cairo_pdf(f, width = 11, height = 8.5) # landscape letter
    grid.draw(puzzleGrob(puzzle$segments, seed = 42, hash = puzzle$hash))
    invisible(dev.off())
    unlink(f)
}