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/
08-24-2019

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:


\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:

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

Acknowledgments

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

Corrections

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

Citation

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

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