Define a font to display one block

Using knitrand Rmarkdown, I use the code to extract the contents of the file (analysis output) with the code as follows:

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

I would like the content to be filename.outreproduced using the Courier New font, but you want to have Times New Roman for plain text when pasting an Rmarkdown document.

I cannot figure out how to do this (I prefer not to request Courier New for the entire document).

+4
source share
1 answer

css tex in_header YAML, . R, .

css

.Courier {
  font-family: Courier New, Courier, monospace;
}

- LateX, .

\newenvironment{Courier}{\ttfamily}{\par}
% Trick to avoid pandoc escaping texte between \begin and \end
\newcommand{\nopandoc}[1]{#1} 

HTML LateX/PDF:

```{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 {
    ""
  }
}
```

, , # Title, . Courier. , , \\nopandoc{ beginStyleFmt } endStyleFmt.

`r beginStyleFmt("Courier", type = "div")`
```{r comment='', echo=FALSE, results='asis'}
cat(readLines('/filepath/filename.out'), sep = '\n')
```
`r endStyleFmt("Courier", type = "div")`
+2

Source: https://habr.com/ru/post/1673960/


All Articles