
Create a Word reference document from a brand.yml file
Source:R/brand_reference_docx.R
brand_reference_docx.Rdbrand_reference_docx() creates a Word document for use as a Quarto or
Pandoc reference document (the reference-doc option for docx output) using
the colors and typography from a
brand.yml file.
By default, the function starts from Pandoc's default reference.docx (so
every style Pandoc uses exists) and merges the brand settings into the
existing style definitions. Styles are edited with xml2, rather than with
officer::docx_set_paragraph_style(), because that function replaces a
style wholesale and drops properties the brand doesn't set (e.g. the heading
outline levels Word uses for the table of contents).
pandoc_reference_docx() writes Pandoc's default reference.docx to a file
using Quarto's bundled Pandoc (if Quarto is installed) or Pandoc.
Usage
brand_reference_docx(
brand = NULL,
path = NULL,
reference = NULL,
overwrite = TRUE,
quiet = FALSE
)
pandoc_reference_docx(path = tempfile(fileext = ".docx"))Arguments
- brand
Path to a brand.yml file or a directory containing one, a
brand_ymlobject frombrand.yml::read_brand_yml(), or a list with the brand settings (e.g. fromyaml12::read_yaml()). IfNULL(default), the path is taken from theBRAND_YML_PATHenvironment variable or, if that is not set, found by searching the working directory and its parent directories for_brand.ymlor_brand.yaml, either directly or in abrandor_brandsubdirectory (the same search used by Quarto andbrand.yml::read_brand_yml()).- path
Optional file path for the output docx file. If
NULL(default), the document is not written to a file.- reference
Optional path to a docx file or a rdocx object to use as the starting reference document. If
NULL(default), Pandoc's defaultreference.docxis used (which requires Quarto or Pandoc). Styles named indefaults.docx.stylesmust exist in the reference document. Note that a rdocx object is modified in place (as with other officer functions).- overwrite
If
TRUE(default), remove file at path if it already exists. IfFALSEand file exists, this function aborts.- quiet
If
TRUE, suppress the message reporting the path of the written file. Defaults toFALSE.
Value
brand_reference_docx() returns a rdocx object (invisibly if
path is supplied). pandoc_reference_docx() returns path.
Brand settings
The following brand.yml settings are used:
color:paletteand color roles (e.g.primaryorforeground). Wherever a color is used, it can be a hex value, a CSS color name, anrgb()value, a palette name, or a color role.typography.base:family,size,weight, andline-heightfor the document defaults and the Normal style.typography.headings:family,weight,style,color, andline-heightfor the Heading 1-9, TOC Heading, and Title styles.typography.link:color,background-color,weight, anddecorationfor the Hyperlink style.typography.monospace-inline(inheriting fromtypography.monospace):family,size,weight,color, andbackground-colorfor the Verbatim Char style.typography.monospace-block(inheriting fromtypography.monospace):background-color(as paragraph shading),line-height,color,family,size, andweightfor the Source Code style used for code blocks. Pandoc's default reference document has no Source Code style, so it is created ifmonospace-blocksetsbackground-color,line-height, orcolor; otherwise, Pandoc creates the style when rendering (with shading from the highlight style).
Pandoc formats the text in code blocks with the Verbatim Char style (or
syntax highlighting styles based on it), which takes precedence over the
Source Code style. As a result, monospace-block values for family,
size, weight, and color only apply if monospace-inline doesn't set
a different value, and highlighted code keeps the token colors and shading
from the highlight style (set with the Quarto highlight-style option).
brand.yml has no Word-specific options, so additional settings are read
from defaults.docx (the brand.yml section for format-specific options):
defaults.docx.styles: a list of style settings named by style ID (e.g.Heading1orBodyText) or style name (e.g.Body Text). Each style supports the typography fieldsfamily,size,weight,style,color,background-color,decoration, andline-heightplus the paragraph fieldsspace-before,space-after,indent-left, andkeep-with-next. TheTablestyle supportsborder-color,border-width,cell-padding(withtop,bottom,left, andright), andheader(typography fields for the first row).defaults.docx.page:size("letter", "legal", or "a4"),margins(withtop,bottom,left,right,header,footer, andgutter), andfooter(withtext,align,size,color, andfamily). In the footer text,{page}and{pages}are replaced with the page number and page count. Margins that aren't set keep the reference document's value (or Word's default of 1in for the page and 0.5in for the header and footer).
Field names can use hyphens (as in the brand.yml spec) or underscores (as in
objects from brand.yml::read_brand_yml()).
Sizes can use pt, in, cm, mm, px, rem, or em units (bare numbers are treated
as pt). Following CSS, 1rem and 1em are 16px (12pt). A unitless
line-height is a multiple of the font size; a line-height with units sets
an exact line height. Font weights can be numbers (100-900) or CSS weight
names (e.g. "semi-bold"); a weight of 600 or more is bold.
These brand.yml settings are not used: color.background (Word documents
don't have a page color by default), typography.fonts (fonts are not
embedded, so the font families
must be installed wherever the document is opened), and decoration: overline (Word has no overline).
See system.file("extdata", "_brand.yml", package = "officerExtras") for an
example.
Table borders and other details that the brand doesn't cover can be edited by hand in Word afterwards (but re-creating the reference document overwrites those edits).
Page settings and sections
The defaults.docx.page settings (page size, margins, and footer) are
stored in the reference document's section properties. Pandoc copies them
to the last section of the rendered document, so they don't always apply to
every page:
Quarto's
.landscapedivs add section breaks with no footer, no margins, and an A4 landscape page size, so the pages before and in a landscape section lose the footer and page settings.A custom Pandoc OpenXML template (the
templateoption for docx output, e.g. as used by the better-word Quarto extension) replaces the section properties if it doesn't use the$sectpr$variable.
Use docx_sync_sections() on the rendered document (e.g. in a Quarto
post-render script) to copy the footer and page settings from the last
section to every other section. The reference document only controls styles
and these defaults, not the document structure (e.g. the title block or
table of contents), which a custom template controls.
Examples
if (nzchar(Sys.which("quarto")) || nzchar(Sys.which("pandoc"))) {
brand_path <- system.file("extdata", "_brand.yml", package = "officerExtras")
withr::with_tempdir({
brand_reference_docx(brand_path, path = "reference.docx")
})
}
#> ✔ Wrote reference.docx