Skip to contents

problemsGrob() creates a grid grob laying out arithmetic problems (as generated by generate_math_problems()) in long-form "stacked" style: right-aligned operands stacked atop each other, the operation symbol beside the bottom operand, a rule line underneath, and blank space below the rule for the answer (plus space above the operands for carry/borrow marks). Problems are laid out in reading order in a column/row grid filling a [0, 1] x [0, 1] viewport.

Usage

problemsGrob(
  problems,
  ncol = NULL,
  nrow = NULL,
  gp = gpar(col = "black", lwd = 2),
  name = NULL,
  vp = NULL
)

Arguments

problems

A data frame as returned by generate_math_problems() (i.e. with columns problem, op, and operands).

ncol

Number of columns of problems. The default (NULL) picks 3 columns for up to 18 problems and 4 columns otherwise.

nrow

Number of rows of problems. The default (NULL) picks enough rows to fit the problems but at least 6 (so a puzzle with few problems keeps the same row height instead of stretching each problem to fill the panel).

gp

A grid::gpar() object.

name

A character identifier (for the grob).

vp

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

Value

A grid::gTree() object.

Examples

if (require("grid", quietly = TRUE)) {
    targets <- c(34, 37, 67, 64, 34, 96)
    problems <- withr::with_seed(42, generate_math_problems(targets))
    grid.newpage()
    vp <- viewport(width = unit(5, "in"), height = unit(7, "in"), just = "top")
    grid.draw(problemsGrob(problems, vp = vp))
}