在 pdf 输出中格式化 Rmd 灰色框的行代码文本

问题描述 投票:0回答:1

当我将 Rmd 文档编织为 HTML 时,内联代码文本显示为不同的字体并被灰色框包围:

```{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.

enter image description here

如果我将相同的文档编织成 pdf,灰色框就会消失,从而更难以区分代码和常规文本:

enter image description here

有没有办法在 pdf 输出中格式化内联代码的输出?


> 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   
latex r-markdown pandoc pdflatex
1个回答
0
投票

echo = FALSE
\texttt{echo = FALSE}
输出中转换为
LaTeX
。您可以重新定义此命令以提供背景颜色框。
HTML
示例中的浅灰色是
rgba(0, 0, 0, 0.04)
,可以翻译为十六进制的
#f4f4f4
。添加

```{=latex}
\definecolor{codegray}{HTML}{f4f4f4}
\let\textttOrig\texttt
\renewcommand{\texttt}[1]{\textttOrig{\colorbox{codegray}{#1}}}
```

在你的

.Rmd
的开头,你会在
.pdf
中得到类似的东西:

enter image description here

.html
相比:

enter image description here

最小示例:

---
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.
© www.soinside.com 2019 - 2024. All rights reserved.