如何在 rmarkdown 中为 PDF 报告添加颜色?

How to add coloring to PDF reports in rmarkdown?

如何在使用 rmarkdown 创建 PDF 报告时为文本的特定部分添加背景颜色。如下所示

我试过这样的东西,但它在乳胶中有效,但在 rmarkdown 中无效

\titleformat{\section}{\sffamily\Large\bfseries\rlap{\color{DarkGreen!90}\rule[-0.5ex]{\linewidth}{3ex}\vspace{-3ex}}\sffamily\Large\color{white}}{\thesection}{1em}{}

this is how it looks in latex

this is in rmarkdown

---
title: "Untitled"
output: pdf_document
header-includes:
- \usepackage{amsfonts,amssymb,amsmath}
- \usepackage[table, svgnames]{xcolor}
- \usepackage{titlesec}
- \usepackage{sectsty}
- \usepackage{xcolor, soul}  
- \sectionfont{\color{red}}
- \subsectionfont{\color{green}}
- \subsubsectionfont{\color{blue}}
- \titleformat{\section}{\sffamily\Large\bfseries\rlap{\color{DarkGreen!90}\rule[-0.5ex]{\linewidth}{3ex}\vspace{-3ex}}\sffamily\Large\color{white}}{\thesection}{1em}{}

---
\section{Highlights}
# Section

两个问题:

  • rmarkdown 已经加载了 xcolor 而没有注意你需要的选项。作为一种解决方法,您可以通过将必要的选项传递给所有包来欺骗降价

  • sectsty 将破坏您通过 titlesec 所做的所有更改。不要同时加载它们


---
title: "Untitled"
output: 
  pdf_document:
    keep_tex: true
classoption: svgnames
header-includes:
- \usepackage{amsfonts,amssymb,amsmath}
- \usepackage{titlesec}
- \usepackage{soul}  
- \titleformat{\section}{\sffamily\Large\bfseries\rlap{\color{DarkGreen!90}\rule[-1.5ex]{\linewidth}{3ex}\vspace{-3.5ex}}\sffamily\Large\color{white}}{\thesection}{1em}{}

---
\section{Highlights}
# Section