在RMarkdown中有没有办法指定字体颜色?
通过浏览代码块选项,似乎没有这个选项。
@Ben Bolker提供的链接中给出的答案:
Roses are <span style="color:red">red</span>,
violets are <span style="color:blue">blue</span>.
如果您选择 HTML (ioslides) 作为输出格式,则此方法可用。
但是,如果您选择 pdf (beamer) 作为输出格式,则此方法无效。如果您想创建一个 pdf,请使用 LaTeX 语法:
Roses are \textcolor{red}{red}, violets are \textcolor{blue}{blue}.
我创建了一个像这样的函数:
## Color Format
colFmt <- function(x,color) {
outputFormat <- knitr::opts_knit$get("rmarkdown.pandoc.to")
if(outputFormat == 'latex') {
ret <- paste("\\textcolor{",color,"}{",x,"}",sep="")
} else if(outputFormat == 'html') {
ret <- paste("<font color='",color,"'>",x,"</font>",sep="")
} else {
ret <- x
}
return(ret)
}
然后,您可以像这样内联使用它:`r colFmt("MY RED TEXT",'red')`
。无论是在LaTeX文档还是HTML文档中工作,都将呈现彩色文本。
rmarkdown::render
进行pdf输出时,我发现它会在tex文件中转换为$\backslash$
。因此,它会直接打印字符串而不是将其解释为latex。非常令人恼火。你有什么想法吗? - Midnighterxtable
内部。 - Midnighter这似乎在输出格式pdf和html中都非常有效:
Roses are $\color{red}{\text{beautiful red}}$,
violets are $\color{blue}{\text{lovely blue}}$.
希望能对你有所帮助。
一种与输出格式无关的解决方案是使用kableExtra
包中的专用text_spec()
函数:
Roses are `r kableExtra::text_spec("red", color = "red")`,
violets are `r kableExtra::text_spec("blue", color = "blue")`
其他人已经提供了输出Word以外的答案。对于Word文档,你可以使用Pandoc自定义样式语法来实现这一点,并借助参考Word文档做到这一点。首先,在你的reference.docx模板中,创建一个具有简短而明显名称的新的Word样式。如果你想要字体颜色应用于整个段落,那么你可以使用默认的“链接段落和字符”样式类型。如果你只想用颜色强调段落中的某些单词,则需要选择“字符”样式类型。更改字体颜色(和任何其他所需的样式),然后保存reference.docx文件。
接下来,在你的.Rmd文件中,你可以使用以下标记:
<div custom-style=“DivCol”>Whole paragraph of colored text</div>
Just a <span custom-style=“SpanCol”>few words</span> of colored text
关于样式名称的说明 - 出于我不理解的原因,“Span_Add” 样式名称无法使用此方法,但“SpanAdd”是可以的。我基本上使用了Nicholas Hamilton的答案,但由于我使用了xtable
和print
,所以在某些latex序列被转义时出现了一些问题。具体来说,\\textcolor
被转换为$\backslash$textcolor
。通过以下方式避免清理,我成功地解决了这个问题:
```{r results='asis'}
tbl = data.frame(a = letters[1:10], b = 1:10 / 10)
tbl$b = ifelse(tbl$b < 0.5, colFmt(tbl$b, "red"), colFmt(tbl$b, "green"))
print(xtable(tbl), sanitize.text.function = identity)
```
然后我不得不手动清理一些字符,比如%
,但至少\textcolor
被正确应用了。当然,这可以通过扩展自己的清理函数来避免。
print(xtable(tbl), sanitize.text.function = identity)
- Nicholas Hamiltonsanitize.text.function = identity
可以避免你创建一个微不足道的内联函数。 - Nicholas HamiltonR Markdown中有一种新的更好的指定颜色或字体的方法:
sass
{sass, echo = FALSE}
// This chunk contains SASS-code to specify font color
h1, h2, h3, h4, h5
color: blue // Color for the headers
body
color: red // Color for the text in the main part of your document
// The colors are only examples. You can change them to anything. You could also use hex codes.
通常,我在我的文档中包含类似以下内容的代码,以便同时更改字体类型、大小、链接颜色等:
// These are variables, and are easy to change
$color: blue
$color2: red
$font: "Arial"
$font-size: 16px
h1, h2, h3, h4, h5
color: $color
font-family: $font
font-weight: bold
body
background-color: white
color: black
font-family: $font
font-size: $font-size
a
color: $color
a:link
color: $color
a:hover
background-color: $color2
对于 PDF 和 HTML,要获取带有 markdown 高亮的可修改彩色文本:请查看 rmarkdown 书籍。Pandoc 过滤器是最好的选择。
对于 Microsoft Word,您首先需要创建一个包含自定义样式的 Template_MS.docx 文件。警告:为段落(段落样式)和少量单词(字符样式)着色创建不同的样式。这是在创建新样式时的一个选项。
在 YAML 中添加:
---
output:
word_document:
reference_docx: Template_MS.docx
---
For <span custom-style="Character1">few words</span> of colored text.
段落文本。
<div custom-style="Paragraph1">Paragraph of colored text. Blabla. Blabla.</div>
output:
bookdown::word_document2:
reference_docx: Template_MS.docx
- LMcreveal.js
color-text.lua
beamer
和reveal.js
演示格式时使用符号([red]{color="red"}
和[red]{color="#FF6347"}
)。Span = function(span)
color = span.attributes['color']
-- if no color attribute, return unchange
if color == nil then return span end
-- tranform to <span style="color: red;"></span>
if FORMAT:match 'html' or FORMAT:match 'revealjs' then -- Also available for revealjs
-- remove color attributes
span.attributes['color'] = nil
-- use style attribute instead
span.attributes['style'] = 'color: ' .. color .. ';'
-- return full span element
return span
elseif FORMAT:match 'latex' or FORMAT:match 'beamer' then -- Also available for beamer
-- remove color attributes
span.attributes['color'] = nil
-- encapsulate in latex code
if string.sub(color, 1, 1) == "#" and #color == 7 then
-- TODO: requires xcolor
local R = tostring(tonumber(string.sub(color, 2, 3), 16))
local G = tostring(tonumber(string.sub(color, 4, 5), 16))
local B = tostring(tonumber(string.sub(color, 6, 7), 16))
table.insert(
span.content, 1,
pandoc.RawInline('latex', '\\textcolor[RGB]{'..R..','..G..','..B..'}{')
)
elseif string.sub(color, 1, 1) == "#" and #color == 4 then
-- TODO: requires xcolor
local R = tostring(tonumber(string.sub(color, 2, 2), 16) * 0x11)
local G = tostring(tonumber(string.sub(color, 3, 3), 16) * 0x11)
local B = tostring(tonumber(string.sub(color, 4, 4), 16) * 0x11)
table.insert(
span.content, 1,
pandoc.RawInline('latex', '\\textcolor[RGB]{'..R..','..G..','..B..'}{')
)
else
table.insert(
span.content, 1,
pandoc.RawInline('latex', '\\textcolor{'..color..'}{')
)
end
table.insert(
span.content,
pandoc.RawInline('latex', '}')
)
-- returns only span content
return span.content
else
-- for other format return unchanged
return span
end
end
---
title: "Changing the font color"
output:
## PDFs
bookdown::pdf_document2:
pandoc_args:
- "--lua-filter=color-text.lua"
bookdown::beamer_presentation2:
pandoc_args:
- "--lua-filter=color-text.lua"
## HTMLs
bookdown::html_document2:
# base_format: "function(..., number_sections) revealjs::revealjs_presentation(...)" # comment this line when creating ordinary HTMLs
pandoc_args:
- "--lua-filter=color-text.lua"
self_contained: false
always_allow_html: yes
link-citations: yes
---
## First
we define a Lua filter and write it to
the file `color-text.lua`.
```{cat, engine.opts = list(file = "color-text.lua")}
Span = function(span)
color = span.attributes['color']
-- if no color attribute, return unchange
if color == nil then return span end
-- tranform to <span style="color: red;"></span>
if FORMAT:match 'html' or FORMAT:match 'revealjs' then -- Also available for revealjs
-- remove color attributes
span.attributes['color'] = nil
-- use style attribute instead
span.attributes['style'] = 'color: ' .. color .. ';'
-- return full span element
return span
elseif FORMAT:match 'latex' or FORMAT:match 'beamer' then -- Also available for beamer
-- remove color attributes
span.attributes['color'] = nil
-- encapsulate in latex code
if string.sub(color, 1, 1) == "#" and #color == 7 then
-- TODO: requires xcolor
local R = tostring(tonumber(string.sub(color, 2, 3), 16))
local G = tostring(tonumber(string.sub(color, 4, 5), 16))
local B = tostring(tonumber(string.sub(color, 6, 7), 16))
table.insert(
span.content, 1,
pandoc.RawInline('latex', '\\textcolor[RGB]{'..R..','..G..','..B..'}{')
)
elseif string.sub(color, 1, 1) == "#" and #color == 4 then
-- TODO: requires xcolor
local R = tostring(tonumber(string.sub(color, 2, 2), 16) * 0x11)
local G = tostring(tonumber(string.sub(color, 3, 3), 16) * 0x11)
local B = tostring(tonumber(string.sub(color, 4, 4), 16) * 0x11)
table.insert(
span.content, 1,
pandoc.RawInline('latex', '\\textcolor[RGB]{'..R..','..G..','..B..'}{')
)
else
table.insert(
span.content, 1,
pandoc.RawInline('latex', '\\textcolor{'..color..'}{')
)
end
table.insert(
span.content,
pandoc.RawInline('latex', '}')
)
-- returns only span content
return span.content
else
-- for other format return unchanged
return span
end
end
```
Now we can test the filter with some text in brackets with
the `color` attribute, e.g.,
> Roses are [red and **bold**]{color="red"} and
> violets are [blue]{color="blue"}.
> Roses are [red and **bold**]{color="#FF6347"} and
> violets are [blue]{color="#6a5acd"}.
undefined control sequence \textcolor
。命令是pandoc ./main.md -o ./main.pdf
。主文本中的内容最简单的是\textcolor{red}{red}
。你使用什么翻译命令? 请问您能否解释PDF部分——我无法让它起作用:“undefined control sequence \textcolor”。命令为“pandoc ./main.md -o ./main.pdf”。主文本中的文本是最简单的\textcolor{red}{red}
。您使用的翻译命令是什么? - Ayratheader-includes: \usepackage{xcolor}
。 - aldo_tapia