Tidyverse Fun - Part 2

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

tidyverse
rstats
Author

Shamindra Shrotriya

Published

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:

\newcommand{\bfa}{\mathbf{a}}
\newcommand{\bfA}{\mathbf{A}}

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
library(tidyverse)
library(glue)

# 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 = ">")
    base::return(out_str)
}

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")
\newcommand{\bfa}{\mathbf{a}}
\newcommand{\bfA}{\mathbf{A}}

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")
\newcommand{\mca}{\mathcal{a}}
\newcommand{\mcA}{\mathcal{A}}

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

\newcommand{\bfa}{\mathbf{a}}
\newcommand{\bfb}{\mathbf{b}}
\newcommand{\bfc}{\mathbf{c}}
\newcommand{\bfd}{\mathbf{d}}
\newcommand{\bfe}{\mathbf{e}}
\newcommand{\bff}{\mathbf{f}}
\newcommand{\bfg}{\mathbf{g}}
\newcommand{\bfh}{\mathbf{h}}
\newcommand{\bfi}{\mathbf{i}}
\newcommand{\bfj}{\mathbf{j}}
\newcommand{\bfk}{\mathbf{k}}
\newcommand{\bfl}{\mathbf{l}}
\newcommand{\bfm}{\mathbf{m}}
\newcommand{\bfn}{\mathbf{n}}
\newcommand{\bfo}{\mathbf{o}}
\newcommand{\bfp}{\mathbf{p}}
\newcommand{\bfq}{\mathbf{q}}
\newcommand{\bfr}{\mathbf{r}}
\newcommand{\bfs}{\mathbf{s}}
\newcommand{\bft}{\mathbf{t}}
\newcommand{\bfu}{\mathbf{u}}
\newcommand{\bfv}{\mathbf{v}}
\newcommand{\bfw}{\mathbf{w}}
\newcommand{\bfx}{\mathbf{x}}
\newcommand{\bfy}{\mathbf{y}}
\newcommand{\bfz}{\mathbf{z}}
\newcommand{\bfA}{\mathbf{A}}
\newcommand{\bfB}{\mathbf{B}}
\newcommand{\bfC}{\mathbf{C}}
\newcommand{\bfD}{\mathbf{D}}
\newcommand{\bfE}{\mathbf{E}}
\newcommand{\bfF}{\mathbf{F}}
\newcommand{\bfG}{\mathbf{G}}
\newcommand{\bfH}{\mathbf{H}}
\newcommand{\bfI}{\mathbf{I}}
\newcommand{\bfJ}{\mathbf{J}}
\newcommand{\bfK}{\mathbf{K}}
\newcommand{\bfL}{\mathbf{L}}
\newcommand{\bfM}{\mathbf{M}}
\newcommand{\bfN}{\mathbf{N}}
\newcommand{\bfO}{\mathbf{O}}
\newcommand{\bfP}{\mathbf{P}}
\newcommand{\bfQ}{\mathbf{Q}}
\newcommand{\bfR}{\mathbf{R}}
\newcommand{\bfS}{\mathbf{S}}
\newcommand{\bfT}{\mathbf{T}}
\newcommand{\bfU}{\mathbf{U}}
\newcommand{\bfV}{\mathbf{V}}
\newcommand{\bfW}{\mathbf{W}}
\newcommand{\bfX}{\mathbf{X}}
\newcommand{\bfY}{\mathbf{Y}}
\newcommand{\bfZ}{\mathbf{Z}}


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

Reuse

Citation

BibTeX citation:
@online{shrotriya2019,
  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.