【问题标题】:How to justify (to both sides) text in R Markdown when knitting in pdf output在pdf输出中编织时如何在R Markdown中证明(两边)文本
【发布时间】:2018-10-07 01:38:28
【问题描述】:

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

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

【问题讨论】:

    标签: r latex r-markdown


    【解决方案1】:

    R Markdown 应该默认使用两端对齐的文本。但是,如果您只想导出为 PDF,我们可以直接在文档中使用 LaTeX 命令。使用标准参数\centering\raggedright\raggedleft,如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]`
    

    如果你想恢复对齐文本,你可以使用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]`
    

    【讨论】:

    • 是否可以只证明块和输出,而不是文本?
    • 我不确定@ForEverNewbie。您可能想打开一个新问题:)
    猜你喜欢
    • 2018-09-05
    • 2021-12-21
    • 2021-12-27
    • 2019-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多