条件字体颜色R Markdown [英] Conditional font color R Markdown

查看:142
本文介绍了条件字体颜色R Markdown的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法找到一种方法来基于变量的值(> 0,== 0或<0)在R Markdown中包含动态字体颜色.有人可以帮忙吗?我尝试了一个if语句,其返回值是乳胶语法,但是出错了.为了清楚起见,我正在寻找PDF输出.这是我尝试过的:

I am unable to find a way to include dynamic font colors in R Markdown based on the value of a variable (>0, ==0, or <0). Could anyone help? I tried an if statement where the return value is latex syntax, but that errored out. To be clear, I'm looking for PDF output. Here's what I tried:

```{r setup, include=FALSE}
x <- 4
```

This is an R Markdown document.
`r if (x>0) {\textcolor{red}{Markdown}} else if (x==0) {\textcolor{blue}{Markdown}} else {\textcolor{yellow}{Markdown}}`

在此虚拟示例中,字体颜色应基于x的值(我将其设置为4)进行更改.

In this dummy example, the font color should change based on the value of x (which I've set to 4).

任何帮助将不胜感激!

推荐答案

我相信您的问题是\t被解释为tab字符.尝试像这样转义\:

I believe your issue is that the \t is interpretted as a tab character. Try escaping the \ like so:

`r if (x>0) "\\textcolor{red}{Markdown}" else if (x==0) "\\textcolor{blue}{Markdown}" else "\\textcolor{yellow}{Markdown}"`

在我运行它时可以使用.

That works when I run it.

这篇关于条件字体颜色R Markdown的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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