Tidyverse Fun - Part 2

Second part in a series of doing useful tasks with the Tidyverse. This time auto-generating sequential LaTeX newcommand macros


Shamindra Shrotriya


August 24, 2019

Task: Generating LaTeX newcommand macros

The central problem

In a custom \(\LaTeX\) macro file I needed to generate several sequential \(\LaTeX\) newcommand entries of the form:


Where using $\bfa$ produces \(\mathbf{a}\) and using $\bfA$ produces \(\mathbf{A}\) i.e the lowecase/uppercase mathbf commands respectively.

Specifically I needed to construct 52 such combined sequential entries for both lowercase/uppercase letter versions of these newcommand \(\LaTeX\) macros. Rather than do this manually, I realized that this would be another fun scripting exercise with using the tidyverse packages glue, purrr, and stringr similar to this similar previous post here.

Goal: Create 52 such lowercase/uppercase newcommand entries and print to the console to directly-copy paste to my \(\LaTeX\) macros file.

The tidy approach

First step is to write a function that takes as an input the following:

  • a single letter (case-sensitive) e.g. "a"
  • the macro shortcut command prefix you prefer e.g "bf" (for bold font in case you were wondering!)
  • the specific \(\LaTeX\) command that we are creating a macro shortcut for i.e. "mathbf" in this case

The function then outputs a single newcommand entry for that lecture i.e \newcommand{\bfa}{\mathbf{a}} in this case. Let’s do it!

# Load required libraries

# Create LaTeX macro newcommand
get_lec_newcmd <- function(inp_letr, mac_type, mac_ref){
    out_str <- glue::glue('\\newcommand{\\<mac_type><inp_letr>}{\\<mac_ref>{<inp_letr>}}',
                          .open = "<", .close = ">")

Let’s just test this out quickly:

c("a", "A") %>%
    purrr::map_chr(.x = ., .f = ~get_lec_newcmd(inp_letr = .x,
                                                mac_type = "bf",
                                                mac_ref = "mathbf")) %>%
    cat(., sep = "\n")

Great - looks like it is working as required!

Note that we can easily generate other \(\LaTeX\) macros like follows

c("a", "A") %>%
    purrr::map_chr(.x = ., .f = ~get_lec_newcmd(inp_letr = .x,
                                                mac_type = "mc",
                                                mac_ref = "mathcal")) %>%
    cat(., sep = "\n")

Which generates the corresponding mathcal macros for \(\mathcal{a}\) and \(\mathcal{A}\) respectively.

So finally we can generate all 52 letter macros at time by simply replacing c("a", "A") with c(letters, LETTERS) which uses the input lowercase/uppercase letters/LETTERS vectors in base R:

Full newcommand Demo Output


Hope you have fun using this to quickly generate your \(\LaTeX\) newcommand macros ✌️.



BibTeX citation:
  author = {Shamindra Shrotriya},
  title = {Tidyverse {Fun} - {Part} 2},
  date = {2019-08-24},
  url = {https://www.shamindras.com/posts/2019-08-21-shrotriya2019tidyfunpt2},
  langid = {en}
For attribution, please cite this work as:
Shamindra Shrotriya. 2019. “Tidyverse Fun - Part 2.” August 24, 2019. https://www.shamindras.com/posts/2019-08-21-shrotriya2019tidyfunpt2.