Skip to contents

Overview

Obviously, everyone wants to be able to customize their label templates! Why go through the bother of installing and running BarnebyLives if you cannot do that!

Note that the labels are made using LaTeX, an awesome guide for LaTeX (relevant to the level you will be using it at) is Overleaf.org, and check the links in the side bar too!

In this vignette we will replace the default LaTeX fonts with add-on fonts using the fontspec package. Also note that previously you may have been able to scrape by using any old LaTeX installation, at this point you will definitely need to make sure you have either XelaTex for lualatext installed. ‘tinytex’ which is installable directly from R should accomplish this on all operating systems.

This vignette assumes you were able to get through Custom Label Templates.

install.packages("tinytex")
tinytex::install_tinytex()

You can check that you have successfully installed this with

tinytex::is_tinytex()

If you are on linux for the sake of compatibility for this vignette, please install a ton of Windows fonts. It seems most of these are on Mac - but if you start hitting errors, maybe give this a go.

You can do this with the code below, which will give part of your soul to Bill Gates of yesteryear.

echo "ttf-mscorefonts-installer msttcorefonts/accepted-mscorefonts-eula select true" | sudo debconf-set-selections
sudo apt install -y ttf-mscorefonts-installer

or you can

sudo apt install ttf-mscorefonts-installer

and then click tab and enter on your keyboard when the debconf pops up to accept that you are giving Bill Gates of yesteryear your soul.

fc-list | grep "Times New Roman"

and doubly verify the Tex install can access them

sudo fc-cache -fv

You may get messages about loops, these are fine as long as the last line reads something like

fc-cache: succeeded

Prepare workspace

library(BarnebyLives)
library(tidyverse)

local <- file.path('~', 'Documents', 'assoRted', 'Barneby_Lives_dev', 'LabelStyles')

l.nevadense <- collection_examples |>
  filter(Collection_number == 2823) |>
  mutate(
    Project_name = 'Custom Fonts', 
    Coordinate_uncertainty = '5m'
    ) 

write.csv(l.nevadense, file.path(local, 'SoS-ExampleCollection.csv'), row.names = F)

All of the labels can be copied from their original locations using the following code.

p2lib <- file.path(
  system.file(package = 'BarnebyLives'),
  'rmarkdown', 'templates', 'labels', 'skeleton'
  )

# here we copy over one of the skeletons which we are going to modify in this example
file.copy(
  from = file.path(p2lib, 'skeleton-customfont.Rmd'), 
  to = file.path(local, 'skeleton-customfont.Rmd')
  )

rm(p2lib)

Render Labels

If you look at the header on this file, you’ll notice it has more going on than the other skeletons. As you will likely end up wanting to merge multiple label components together into a single skeleton (eventually), be sure to use this as the preamble.

output: 
  pdf_document:
    latex_engine: xelatex # or lualatex
    keep_text: true
params:
  Collection_number: Collection_number
geometry: paperheight=4in,paperwidth=4in,margin=0.1in
header-includes:
  - \pagenumbering{gobble}
  - \usepackage{setspace}
  - \usepackage{fontspec}
  - \setmainfont{Georgia}

Note that the last line in the preamble is: this is the command ‘setmainfont’ to set the font ‘Georgia’ which will be used.

Examples with Common Fonts

One custom Fonts
One custom Fonts

However, we can use multiple fonts (…if so desired… ) by importing an additional font, and specifying where we should use it.

output: 
  ...
header-includes:
  ...
  - \usepackage{fontspec}
  - \setmainfont{Georgia}
  - \newfontfamily\couriernew{Courier New}

And edit portions of the skeletons body

...
\textcouriernew{`r writer(data[['Habitat']])`. `r data[['physical_environ']]`}
Veg.: `r species_font(record[['Vegetation']])` `r associates_writer(record[['Associates']])`
`r writer(record[['Notes']])`  
\endgroup
...
Two custom Fonts
Two custom Fonts

But! Notice that this new font is applied to the whole group not just the braced contents, you can bypass this with custom macros - which we won’t discuss or support in this package. TBH I’m not sure how often you should really have two fonts per label anyways…