4

Using knitr and Rmarkdown, I use code to fetch the content of a file (output of an analysis), with a code as the following:

{r comment='', echo=FALSE}
cat(readLines('/filepath/filename.out'), sep = '\n')

I would like the content of filename.out to be reproduced with the font Courier New, but want to have Times New Roman for ordinary text when knitting the Rmarkdown document.

I cannot figure out how to do that (I prefer not to ask for Courier New for the whole document).

zx8754
  • 52,746
  • 12
  • 114
  • 209
cibr
  • 453
  • 5
  • 16
  • Probably something like this: http://stackoverflow.com/questions/37944197/add-a-css-class-to-single-code-chunks-in-rmarkdown – talat Apr 04 '17 at 10:36

1 Answers1

2

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")`
Sébastien Rochette
  • 6,536
  • 2
  • 22
  • 43
  • Thanks. Some quick attempts to make this work didn't succeed (using markdown code, output format is PDF). I will look further into this, starting with your blog. – cibr Apr 04 '17 at 11:16
  • True. I modified the `begin` and `end` functions, as well as the syntax for your code chunk, you should now be able to make it work. BTW, for you, the LateX code can be directly written in your Rmd file. – Sébastien Rochette Apr 04 '17 at 12:18