使R打印的文本具有颜色esp.在R markdown针织衫中? [英] Getting R printed texts to have color esp. in R markdown knits?

查看:46
本文介绍了使R打印的文本具有颜色esp.在R markdown针织衫中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个非常简单的问题:我喜欢编写R笔记本/降价笔记本,并且喜欢使用 highlight:tango 之类的东西,当我将笔记本编织成pdf时,可以为代码赋予背景色.

但是,我不知道如何为R中的打印输出赋予彩色背景或彩色字体.例如,假设我有以下块.

 ```{r,echo = FALSE}writeLines("Help")``` 

我希望看到帮助"一词被突出显示,用灰色背景的红色字体表示.我该如何实现?

非常感谢.

解决方案

对于PDF输出,下面是一些 latex 命令,用于获取彩色文本和底纹.(有关其他阴影选项,请参阅Tex Stack Exchange网站上的

Very simple question: I love writing R notebooks/markdowns, and with something like highlight: tango I can give background color to codes when I knit my notebook to pdfs.

However, I don't know how to give colored backgrounds or colored fonts to printed outputs in R. For example, suppose I have the following chunk.

```{r, echo = FALSE}
writeLines("Help")
```

I'd like to see the word Help to be highlighted, say in red font with gray background. How can I achieve this?

Many thanks in advance.

解决方案

For PDF output, below are some latex commands to get colored text and shading. (For additional shading options, see this answer at the Tex Stack Exchange site.) However, I'm not sure how to get the output from writeLines shaded. Enclosing the code chunk in a \shaded environment causes an error. Hopefully someone will come along with a solution that works with code chunk output.

---
output: pdf_document
header-includes:
  - \usepackage{xcolor}
  - \usepackage{framed}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```

\colorlet{shadecolor}{gray!10}

\color{red}

```{r}
writeLines("help")
```

\begin{shaded}

Red text with a gray background.

\end{shaded}

Red text.

\color{black}

Black text.

\colorlet{shadecolor}{red!10} 

\begin{shaded}

Black text with a red background.

\end{shaded}

\colorlet{shadecolor}{red!90} 

\begin{shaded}

Black text with a darker red background.

\end{shaded}

And here's the resulting PDF document:

这篇关于使R打印的文本具有颜色esp.在R markdown针织衫中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆