wahani / templates

Tools for Template Programming in R
Other
2 stars 1 forks source link

Literal curly braces incorrectly consumed #3

Closed Xophmeister closed 6 years ago

Xophmeister commented 6 years ago

While trying to template some LaTeX, which uses curly braces to enclose directive parameters, I noticed that templates would consume the literal braces, rather than inserting them as required. For example, say if I want to template:

\section{<SECTION_NAME>}

...my immediate thought was to use tmpl("\\section{{{ section_name }}}", section_name = "foo"). This however is rendered as:

\sectionfoo

I found no combination of characters that allowed me to surround a templated expression with curly braces, without interposing whitespace. @wahani suggested the following workarounds:

Option 1:

tmpl("\\section{{ foo }}", foo = "{Hi}")

Option 2:

wrapCurly <- function(x) {
  sprintf("{%s}", x)
}

tmpl("\\section{{ wrapCurly(foo) }}", foo = "Hi")

Option 3:

section <- function(x) {
  sprintf("\\\\\\\\section{%s}", x)
}

tmpl("{{ section(foo) }}", foo = "Hi") 
wahani commented 6 years ago

Thanks for taking the time to post this here. I will look into it.