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 https://www.shamindras.com/

Table of Contents

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:

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 ✌️.


I’d like to thank Salil Shrotriya for creating the preview image for this post. The hex sticker png files were sourced from here



If you see mistakes or want to suggest changes, please create an issue on the source repository.


For attribution, please cite this work as

Shrotriya (2019, Aug. 24). Shamindra Shrotriya: Tidyverse Fun - Part 2. Retrieved from https://www.shamindras.com/posts/2019-08-21-shrotriya2019tidyfunpt2/

BibTeX citation

  author = {Shrotriya, Shamindra},
  title = {Shamindra Shrotriya: Tidyverse Fun - Part 2},
  url = {https://www.shamindras.com/posts/2019-08-21-shrotriya2019tidyfunpt2/},
  year = {2019}