如何在 RMarkdown 的样式标签中包含 @font-face?
How to include @font-face within style tag of RMarkdown?
是否可以使用 CSS @font-face 为 RMarkdown 提供字体文件?
例如:
<style>
@font-face {
font-family: "Custom";
src: url(https://github.com/stevecondylios/fonts/raw/master/CircularStd-Book.ttf) format("truetype");
}
body {
font-family: "Custom", Verdana, Tahoma;
}
</style>
请注意,上面完全相同的 css 代码(减去样式标签)在存储在外部 .css 本地文件并在 Rmd 的 yaml 中引用
---
title: "My Rmarkdown doc"
date: "28 April 2020"
output:
html_document:
css: mystyles.css
---
最终目标
如果这是一个 A/B 问题,我的简单目标是应用自定义字体,同时避免使用 Rmd 文件本身以外的任何本地文件(即没有本地外部样式 sheets,虽然从网络上读取文件很好)。
我认为仅有的两个选择是在 <style> ... </style>
内从网上读取字体文件,或者 base64 encoding the font info within the tags of the Rmd itself。我一直在尝试,但收效甚微。
第三种选择是为 yaml 值提供 url(样式 sheet),尽管我不确定这是否可行
html_document:
code_folding: hide
css: get_file(www.mysite.com/mystyles.css)
进一步尝试
为了更好的衡量,我也试过了
```{r}
css_url <- "https://github.com/stevecondylios/fonts/raw/master/CircularStd-Book.ttf"
```
<style>
@font-face {
font-family: "Custom";
src: url(`r css_url`) format("truetype");
}
body {
font-family: "Custom", Verdana, Tahoma;
}
</style>
```
更新
我了解到您不能在 HTML body 中声明 @font-face
,但必须在 header 中声明。我们确实可以 ,但这会读取一个外部文件,这正是我要避免的,所以我回到第 1 个方块。
对于任何其他想要这样做的人,this 答案很有帮助。
基本上:
- 从任何字体网站下载
.tff
(字体)文件
- 将其转换为 base64(在 mac 上,只需
base64 myfont.ttf > myfont_base64.txt
- 将
myfont_base64.txt
的内容复制到下面的占位符中,并将全部内容放在你的 RMarkdown 中的某个位置(它不必像我一样在 HTML <head>
中以前怀疑,但在任何地方,例如 <body>
都可以)
<style>
@font-face {
font-family: 'myfont';
src: url(data:font/truetype;charset=utf-8;base64,<<copied base64 string>>) format('truetype');
font-weight: normal;
font-style: normal;
}
body {
font-family: "myfont", Verdana, Tahoma;
}
</style>
是否可以使用 CSS @font-face 为 RMarkdown 提供字体文件?
例如:
<style>
@font-face {
font-family: "Custom";
src: url(https://github.com/stevecondylios/fonts/raw/master/CircularStd-Book.ttf) format("truetype");
}
body {
font-family: "Custom", Verdana, Tahoma;
}
</style>
请注意,上面完全相同的 css 代码(减去样式标签)在存储在外部 .css 本地文件并在 Rmd 的 yaml 中引用
---
title: "My Rmarkdown doc"
date: "28 April 2020"
output:
html_document:
css: mystyles.css
---
最终目标
如果这是一个 A/B 问题,我的简单目标是应用自定义字体,同时避免使用 Rmd 文件本身以外的任何本地文件(即没有本地外部样式 sheets,虽然从网络上读取文件很好)。
我认为仅有的两个选择是在 <style> ... </style>
内从网上读取字体文件,或者 base64 encoding the font info within the tags of the Rmd itself。我一直在尝试,但收效甚微。
第三种选择是为 yaml 值提供 url(样式 sheet),尽管我不确定这是否可行
html_document:
code_folding: hide
css: get_file(www.mysite.com/mystyles.css)
进一步尝试
为了更好的衡量,我也试过了
```{r}
css_url <- "https://github.com/stevecondylios/fonts/raw/master/CircularStd-Book.ttf"
```
<style>
@font-face {
font-family: "Custom";
src: url(`r css_url`) format("truetype");
}
body {
font-family: "Custom", Verdana, Tahoma;
}
</style>
```
更新
我了解到您不能在 HTML body 中声明 @font-face
,但必须在 header 中声明。我们确实可以
对于任何其他想要这样做的人,this 答案很有帮助。
基本上:
- 从任何字体网站下载
.tff
(字体)文件 - 将其转换为 base64(在 mac 上,只需
base64 myfont.ttf > myfont_base64.txt
- 将
myfont_base64.txt
的内容复制到下面的占位符中,并将全部内容放在你的 RMarkdown 中的某个位置(它不必像我一样在 HTML<head>
中以前怀疑,但在任何地方,例如<body>
都可以)
<style>
@font-face {
font-family: 'myfont';
src: url(data:font/truetype;charset=utf-8;base64,<<copied base64 string>>) format('truetype');
font-weight: normal;
font-style: normal;
}
body {
font-family: "myfont", Verdana, Tahoma;
}
</style>