I am trying to write a function for use in Rmarkdown that allows newlines, the use of variables and nested functions as well as Markdown and Latex commands for outputting pdf's. The function would be used to, for example, generate formatted sections in Rmarkdown documents (I realize that I could use Latex' \titleformat{\section} to achieve this in the particular case of sections but I would also like to be able to display, for example, JSON data with a certain formatting in Rmarkdown).
Here is an example of a function that doesn't work but that will hopefully illustrate what I would like to achieve:
---
title: "Printing from function with newlines and escaped Markdown/Latex commands"
author: "Jono3030"
output: pdf_document
---
thirdLineFunction <- function() {
thirdLine <- "**This is the third line in bold**"
return(thirdLine)
}
newLinePrinting <- function(exampleVar) {
firstLine <- "This is the first line"
secondLine <- sprintf("This is the %s line", exampleVar)
thirdLine <- thirdLineFunction()
fourthLine <- "\\textit{This is the fourth line in italics}"
cat(firstLine, "\n\n", secondLine, "\n\n", thirdLine, "\n\n", fourthLine)
}
Using cat and including newline characters results in non of the strings being printed i.e. only the title and author appear in the pdf. Even if printing with cat works, however, Markdown and Latex code does not get escaped. Using print, sprintf or paste instead of cat does not print newlines and some of these methods include indices ([1] etc.) and quotations marks in the outputs which is not desired.
The desired output that renders correctly to a pdf from Rmarkdown would be:
This is the first line
This is the second line
**This is the third line in bold**
\textit{This is the fourth line in italics}
I also tried to print from the function with a for loop:
test <- function() {
o1 <- "This is text"
o2 <- "This is \\textbf{also} text"
o3 <- thirdLineFunction()
o4 <- "This is *text*"
newline <- ""
listvar <- c(o1, newline, o2, newline, o3, newline, o4)
for (i in listvar) {
print(noquote(i))
}
}
Although closer to what I want it didn't work either as I could not get rid of the indices:
[1] This is text
[1]
[1] This is \\textbf{also} text
[1]
[1] **This is the third line in bold**
[1]
[1] This is *text*
Using cat to omit the indices resulted in the Markdown and Latex commands not being escaped.
Using the above for loop with a combination of paste and cat didn't work either:
for (i in listvar) {
paste(i, collapse = '\n') %>% cat()
}
Of note might be that cat does work when I execute code in the console but the output does not show in the rendered pdf. I also tried to render to html which doesn't work either.
Again, eventually I would like to be able to print something like this using a function:
## Section Title
\vspace{1em}
\hrulefill
Or:
This is the **first** line printed from a JSON file
\vspace{1em}
This is the \textbf{second} line printed from a JSON file
\vspace{1em}
The output should then be rendered correctly into pdf, respecting the Markdown and Latex formatting.
Thank you in advance for your time and help!
