更改 rmarkdown 生成的 PDF 中的字体

问题描述 投票:0回答:4

我正在使用 rmarkdown 生成报告。编织 PDF 时

---
title: "Untitled"
output: pdf_document
---

我想指定创建 PDF 时使用的字体。官方文档(请参阅“LaTeX 选项”部分)说我可以做到这一点。 enter image description here 但是,我从未使用过 LaTeX,并且无法理解如何在

rmarkdown
包使用的 .Rmd 文档顶部的 YAML 选项中进行此类选择。

问题:如何更改rmarkdown生成的PDF中的字体?

会话信息() R版本3.1.0 (2014-04-10) 平台:x86_64-w64-mingw32/x64(64位)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggplot2_1.0.0 RODBC_1.3-10  knitr_1.6     dplyr_0.2

我从来没有使用过 LaTeX,也不想在这个妈妈身上接触它

r pdf latex yaml r-markdown
4个回答
48
投票

YAML 选项中的缩进是有意义的。正如说明所指出的“请注意,这些选项不会出现在输出部分下方,而是与标题、作者等一起出现在顶层)”。所以,

---
output:
  pdf_document:
    latex_engine: xelatex
    sansfont: Calibri Light
---

将产生未使用的参数错误,而

---
output:
  pdf_document:
    latex_engine: xelatex
sansfont: Calibri Light
---

会完成这项工作。此外,在 YAML 之后插入的 LaTeX 命令似乎会覆盖它:所以

---
output:
  pdf_document:
    latex_engine: xelatex
    sansfont: Calibri Light
---
\fontsize{12}{22}
\fontseries{b}
\selectfont

使用默认字体(而不是 Calibri)生成 PDF,但是,字体选项传递得很好。


13
投票

我使用 pdfLatex,所以在我使用“header-includes”之前几乎没有任何效果,如下所示:

---
title: "TITLE"
author: "ANDRES"
output: pdf_document
fontsize: 11pt
header-includes:
  % Allows me to use and print accented characters (document is in Spanish)
  \usepackage[T1]{fontenc}
  \usepackage[utf8]{inputenc}

  % Set up the document in Spanish
  \usepackage[spanish]{babel}

  % It allows me to place the tables or images where I want, whether there is space or not
  \usepackage{float}

  % It has several commands to have more control over the tables (at least I use it for this)
  \usepackage{array}

  % For custom headers and footers
  \usepackage{fancyhdr}

  % This line allows me to manipulate images within latex
  \usepackage{graphicx}

  % This package has several uses, for this document I used it to manage hyperlinks and introduce metadata to the document
  \usepackage{hyperref}

  % These lines define the font that I want to use in the document
  \usepackage{helvet}
  \renewcommand{\familydefault}{\sfdefault}
---

进行此更改后,生成的 pdf 将如下所示:

enter image description here

其他解决方案仅实现了这一点:

enter image description here

只是我需要的字体的一部分文本。


11
投票

只是一个简单的例子。 将这些行添加到 RMD 主文本区域并查看效果。

\fontfamily{cmr}
\fontsize{12}{22}
\fontseries{b}
\selectfont

希望这会有所帮助


0
投票

如果仅需要小文本更改字体,则需要使用egingroup和ndgroup语句。它们之间输出的所有字符都将具有确定的文本样式。以猫为例:

 cat("\\begingroup\\fontsize{8}{8}\\selectfont{YOUR TEXT}\\endgroup") 

简单的 rmarkdown 示例:

\begingroup
\fontsize{8}{8}\selectfont
YOUR TEXT
\endgroup 
© www.soinside.com 2019 - 2024. All rights reserved.