# Load required libraries
library(tidyverse)
library(glue)
# Create LaTeX macro newcommand
<- function(inp_letr, mac_type, mac_ref){
get_lec_newcmd <- glue('\\newcommand{\\<mac_type><inp_letr>}{\\<mac_ref>{<inp_letr>}}',
out_str .open = "<", .close = ">")
return(out_str)
}
Task: Generating LaTeX newcommand macros
The central problem
In a custom newcommand
entries of the form:
\newcommand{\bfa}{\mathbf{a}}
\newcommand{\bfA}{\mathbf{A}}
Where using $\bfa$
produces $\bfA$
produces mathbf
commands respectively.
Specifically I needed to construct 52 such combined sequential entries for both lowercase/uppercase letter versions of these newcommand
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
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
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!
Let’s just test this out quickly:
c("a", "A") %>%
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
c("a", "A") %>%
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
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 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
Reuse
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}
}