You can create the desired style in a css file or a tex file called with in_header
in the YAML, depending on your output.
Then you create a R function that will apply this style to your text.
css file to define the desired style
.Courier {
font-family: Courier New, Courier, monospace;
}
latex file to define the desired style
If your output is only LateX, you can put these commands directly in your document.
\newenvironment{Courier}{\ttfamily}{\par}
% Trick to avoid pandoc escaping texte between \begin and \end
\newcommand{\nopandoc}[1]{#1}
Style format functions for long text output to include in a chunk
These functions work for HTML or LateX/PDF outputs:
```{r, echo=FALSE}
beginStyleFmt <- function(textstyle, type = "span") {
outputFormat <- knitr:::pandoc_to()
if (outputFormat %in% c('latex', 'beamer')) {
if (type %in% c("div", "p")) {
paste0("\\nopandoc{\\begin{", textstyle, "}}\n")
} else {
paste0("\\nopandoc{\\", textstyle, "{")
}
} else if (outputFormat == 'html') {
paste0("<", type, " class='", textstyle, "'>")
} else {
""
}
}
endStyleFmt <- function(textstyle, type = "span") {
outputFormat <- knitr:::pandoc_to()
if (outputFormat %in% c('latex', 'beamer')) {
if (type %in% c("div", "p")) {
paste0("\n\\nopandoc{\\end{", textstyle, "}}")
} else {
paste0("}}")
}
} else if (outputFormat == 'html') {
paste0("</", type, ">")
} else {
""
}
}
```
Code for your chunk in your document
If you have some markdown-like syntax in the text file, like # Title
, it will be read as markdown syntax. But the text between titles will be in Courier. If you do not want your text to be read as markdown syntax, you can remove the \\nopandoc{
in beginStyleFmt
and corresponding }
in endStyleFmt
functions.
`r beginStyleFmt("Courier", type = "div")`
```{r comment='', echo=FALSE, results='asis'}
cat(readLines('/filepath/filename.out'), sep = '\n')
```
`r endStyleFmt("Courier", type = "div")`