r - 在pdf输出中进行编织时,如何在R Markdown中对文本进行正反对齐

标签 r latex r-markdown

我一直在寻找控制文本对齐方式的方法,但是我找不到用于PDF输出的任何内容。

有一个现有答案,但仅与HTML输出有关:How to justify the text to both sides when knitting html in rmarkdown

最佳答案

R Markdown应该默认使用对齐文本。但是,如果只想导出为PDF,我们可以使用标准参数\centering \raggedright\raggedleft直接在文档中使用LaTeX命令,如here所述。

这是一个最小的示例:

---
output: pdf_document
---

```{r, include = FALSE}
devtools::install_github("coolbutuseless/lipsum")
library(lipsum)
```

**Default**

`r lipsum[1]`

\centering

**Centered Text**

`r lipsum[1]`

\raggedright

**Ragged Right**

`r lipsum[1]`

\raggedleft

**Ragged Left**

`r lipsum[1]`

enter image description here

如果要还原为对齐文本,则可以使用ragged2e LaTeX软件包。您需要通过添加以下内容在YAML中加载此文件:
---
output: pdf_document
header-includes:
  - \usepackage[document]{ragged2e}
---

\raggedleft

**Ragged Left**

`r lipsum[1]`



\justify

**Revert to Justified**

`r lipsum[1]`

编辑

如果使用的是papaja模板,则需要包括所有YAML。不提供作者,简称或其他字段将导致其崩溃。
---
title             : "The title"
shorttitle        : "Title"

author: 
  - name          : "First Author"
    affiliation   : "1"
    corresponding : yes    # Define only one corresponding author
    address       : "Postal address"
    email         : "my@email.com"
  - name          : "Ernst-August Doelle"
    affiliation   : "1,2"

affiliation:
  - id            : "1"
    institution   : "Wilhelm-Wundt-University"
  - id            : "2"
    institution   : "Konstanz Business School"

author_note: |
  Add complete departmental affiliations for each author here. Each new line herein must be indented, like this line.

  Enter author note here.

abstract: |
  Enter abstract here. Each new line herein must be indented, like this line.

keywords          : "keywords"
wordcount         : "X"

bibliography      : ["r-references.bib"]

figsintext        : no
figurelist        : no
tablelist         : no
footnotelist      : no
lineno            : yes
mask              : no

class             : "man"
output            : papaja::apa6_pdf
header-includes:
  - \usepackage[document]{ragged2e}
---

```{r load_packages, include = FALSE}

library(lipsum)
```
\justify

**Default**

`r lipsum[1]`

enter image description here

关于r - 在pdf输出中进行编织时,如何在R Markdown中对文本进行正反对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50041549/

相关文章:

r - zoo/xts 微秒读取问题

r - 如何从行名称查找时间差?

r - 使用 knitr 在 PDF 中制作动画,不仅支持 Adob​​e Reader

python - 生成 PDF 文件的 Web 服务器

r - 如何在 R 中创建包含目录和子目录名称的表?

r - 根据 R 中的 FDR (Benjamini & Hochberg)` 方法调整显着性阈值 (alpha)

r - 在 Windows 和 Mac 上交替使用 lubridate 包

c++ - 在 C 或 C++ 中从 LaTeX 制作 PNG|jpeg

r - 将 DT 表合并到 Rmd Markdown 文档中

r-markdown - 如何在此 Flexdashboard 布局中创建选项卡集