When I knit an Rmd doc to HTML, in-line code text shows up as a different font and surrounded by a grey box:
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
If I knit the same document to a pdf instead, the grey box disapears, making it more difficult to distinguish code from regular text:
Is there a way to format the output of in-line code in the pdf outputs?
> sessionInfo()
R version 4.4.0 (2024-04-24 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 10 x64 (build 19045)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.utf8 LC_CTYPE=English_United States.utf8 LC_MONETARY=English_United States.utf8 LC_NUMERIC=C
[5] LC_TIME=English_United States.utf8
time zone: America/Los_Angeles
tzcode source: internal
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] qgraph_1.9.8 igraph_2.0.3 ggplot2_3.5.1 dplyr_1.1.4
loaded via a namespace (and not attached):
[1] utf8_1.2.4 generics_0.1.3 gtools_3.9.5 lattice_0.22-6 jpeg_0.1-10 stringi_1.8.4 digest_0.6.35 magrittr_2.0.3
[9] evaluate_0.24.0 grid_4.4.0 fastmap_1.2.0 Matrix_1.7-0 plyr_1.8.9 nnet_7.3-19 backports_1.5.0 Formula_1.2-5
[17] gridExtra_2.3 fansi_1.0.6 scales_1.3.0 pbapply_1.7-2 pbivnorm_0.6.0 abind_1.4-5 mnormt_2.1.1 cli_3.6.2
[25] rlang_1.1.3 munsell_0.5.1 Hmisc_5.1-3 base64enc_0.1-3 withr_3.0.1 yaml_2.3.8 parallel_4.4.0 tools_4.4.0
[33] reshape2_1.4.4 checkmate_2.3.2 htmlTable_2.4.3 colorspace_2.1-1 corpcor_1.6.10 fdrtool_1.2.17 vctrs_0.6.5 R6_2.5.1
[41] png_0.1-8 rpart_4.1.23 stats4_4.4.0 lifecycle_1.0.4 stringr_1.5.1 htmlwidgets_1.6.4 psych_2.4.6.26 foreign_0.8-86
[49] cluster_2.1.6 glasso_1.11 pkgconfig_2.0.3 pillar_1.9.0 gtable_0.3.5 rsconnect_1.3.1 glue_1.7.0 data.table_1.15.4
[57] Rcpp_1.0.13 xfun_0.44 tibble_3.2.1 tidyselect_1.2.1 rstudioapi_0.16.0 knitr_1.48 farver_2.1.2 nlme_3.1-164
[65] htmltools_0.5.8.1 lavaan_0.6-18 rmarkdown_2.27 labeling_0.4.3 compiler_4.4.0 quadprog_1.5-8
The echo = FALSE
is converted to \texttt{echo = FALSE}
in the LaTeX
output. You can redefine this command in order to provide the background color box. The light grey from the HTML
example is rgba(0, 0, 0, 0.04)
which could be translated to #f4f4f4
in Hex. Add
```{=latex}
\definecolor{codegray}{HTML}{f4f4f4}
\let\textttOrig\texttt
\renewcommand{\texttt}[1]{\textttOrig{\colorbox{codegray}{#1}}}
```
at the beginning of your .Rmd
and you get something like this in the .pdf
:
Compared to the .html
:
Minimal example:
---
output: pdf_document
---
```{=latex}
\definecolor{codegray}{HTML}{f4f4f4}
\let\textttOrig\texttt
\renewcommand{\texttt}[1]{\textttOrig{\colorbox{codegray}{#1}}}
```
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.