library(ggforma)
library(grid)
library(systemfonts)

options <- list(sans = c("Arial", "Liberation Sans", "DejaVu Sans"))
installed <- system_fonts()$family
fonts <- vapply(options, function(x) {
  found <- intersect(x, installed)
  if (!length(found)) stop('Choose a suitable installed local font for ', paste(x, collapse = ', '))
  found[1L]
}, character(1))

sheet <- function(title, subtitle) {
  grid.newpage()
  grid.draw(markup_grob(title, format = "plain", x = .055, y = .925, hjust = 0,
                        size = 22, family = fonts["sans"], face = "bold", colour = "#172C3C"))
  grid.draw(markup_grob(subtitle, format = "plain", x = .055, y = .810, hjust = 0,
                        size = 10, family = fonts["sans"], colour = "#465865"))
  grid.lines(x = c(.055, .945), y = c(.745, .745), gp = gpar(col = "#CED7DA", lwd = .8))
}

note <- function(text, x, y, size = 17, family = fonts["sans"], ...) {
  grid.draw(markup_grob(text, x = x, y = y, size = size, family = family,
                        colour = "#172C3C", hjust = 0, ...))
}

mixed <- function() {
  sheet("The mean and its error", "Inline plotmath and one display equation in a measured paragraph.")
  body <- paste0("For <b>n observations</b>, the mean $bar(x)$ has estimated standard error ",
                 "$frac(s,sqrt(n))$. Under the usual assumptions, an interval is\n\n",
                 "$$\nbar(x) %+-% 1.96 * frac(s,sqrt(n))\n$$")
  note(body, .067, .67, format = "mixed", vjust = 1, size = 15,
       width = unit(148, "mm"), lineheight = 1.18, paragraph_spacing = 5)
}

mixed()
