簡體   English   中英

使用 rmarkdown::render() 將 markdown 文件渲染為 pdf 並調整頁邊距和字體

[英]render a markdown file to pdf with rmarkdown::render() and adjust page margins and font

我想將之前由另一個進程創建的簡單 markdown 文件渲染到 pdf 文件中。

命令:

rmarkdown::render(input = "my_report.md", 
                  output_format = rmarkdown::pdf_document(latex_engine = "xelatex"))

只是做這個工作。

但是我想更改邊距和主要字體。 使用.Rmd文件,可以像這樣在 Yaml header 中定義這些設置:

---
output: 
  pdf_document:
    latex_engine: xelatex
mainfont: LiberationSans
geometry: "left=5cm,right=3cm,top=2cm,bottom=2cm"
---

但是我想轉換的 markdown 文件沒有 Yaml header。有沒有辦法將這些 Yaml 選項作為 function 參數或以間接方式傳遞給render器 function?

這是可能的,但只是使用了一些技巧,它可能不適用於所有可能的 arguments。它似乎適用於mainfontgeometry

這些設置傳遞給 LaTeX 的方式是作為插入模板的 Pandoc 變量。 問題是,如果您的 YAML 中沒有pdf_document驅動程序硬編碼 1in 的邊距,並且這些邊距會在接近尾聲的地方添加到 Pandoc 命令行中。 您需要在以后覆蓋該設置。

這是一種方法。 創建您自己的 output 格式,該格式基於pdf_document ,但在末尾添加了自己的變量設置。 例如,

my_pdf_document <- function(geometry, mainfont, ...) {
  force(geometry)
  force(mainfont)
  result <- rmarkdown::pdf_document(...)
  oldpreproc <- result$pre_processor
  result$pre_processor <- function(...) {
    c(oldpreproc(...),
      "--variable",
      paste0("geometry:", geometry),
      "--variable",
      paste0("mainfont:", mainfont))
  }
  result
}

當您想使用此預處理器進行渲染時,請提供您本應提供給pdf_document的 arguments 以及使用 Pandoc 變量設置的那些,例如

rmarkdown::render("test.md",
              output_format = my_pdf_document(
                geometry = "left=5cm,right=3cm,top=2cm,bottom=2cm", 
                mainfont = "Helvetica", 
                latex_engine="xelatex")
             )

當我進行上述調用時,我看到一個 Pandoc 命令,如下所示:

/Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/pandoc
  +RTS -K512m -RTS test.md --to latex --from
  markdown+autolink_bare_uris+tex_math_single_backslash 
  --output test.tex 
  --lua-filter /Library/Frameworks/R.framework/Versions/4.2/Resources/library/rmarkdown/rmarkdown/lua/pagebreak.lua 
  --lua-filter /Library/Frameworks/R.framework/Versions/4.2/Resources/library/rmarkdown/rmarkdown/lua/latex-div.lua 
  --embed-resources --standalone --highlight-style tango 
  --pdf-engine xelatex --variable graphics 
  --variable 'geometry:margin=1in' 
  --variable 'geometry:left=5cm,right=3cm,top=2cm,bottom=2cm' 
  --variable 'mainfont:Helvetica' 

(盡管它們都排在一條長線上,沒有包裹得那么整齊)。 倒數第三行是有問題的邊距設置,最后兩行是我們特殊的 output 格式的結果。

編輯添加:

一個更簡單、更簡單的方法是讀取整個 Markdown 文件並用 YAML header 再次寫出它。然后你可以把你喜歡的任何東西放在 header 中,如果硬編碼版本可以工作,它就會工作。

選項 01

geometrymainfontPandoc variables ,您可以通過pandoc_args參數將它們傳遞給rmarkdown::pdf_document

為了方便起見,您可以使用rmarkdown::pandoc_variable_arg來指定 pandoc arguments 的名稱-值對。並且您需要使用template=NULL以便不使用 rmarkdown 默認模板(否則傳遞給pandoc_arg的任何變量值都會被geometry:margin=1in ,因此您指定的幾何值將被 rmarkdown 默認值覆蓋)。

library(rmarkdown)

render(
  input = "input.md",
  output_format = rmarkdown::pdf_document(
    latex_engine = "xelatex",
    template = NULL,
    pandoc_args = c(
      pandoc_variable_arg("geometry", "left=5cm,right=3cm,top=2cm,bottom=2cm"),
      pandoc_variable_arg("mainfont", "LiberationSans")
    )
  )
)

我們可以看到變量被正確地傳遞給了 pandoc,

 /usr/lib/rstudio-server/bin/quarto/bin/tools/pandoc +RTS -K512m -RTS input.md --to latex --from markdown+autolink_bare_uris+tex_math_single_backslash --output input.tex --lua-filter /cloud/lib/x86_64-pc-linux-gnu-library/4.2/rmarkdown/rmarkdown/lua/pagebreak.lua --lua-filter /cloud/lib/x86_64-pc-linux-gnu-library/4.2/rmarkdown/rmarkdown/lua/latex-div.lua --embed-resources --standalone --highlight-style tango --pdf-engine xelatex --variable geometry=left=5cm,right=3cm,top=2cm,bottom=2cm --variable mainfont=LiberationSans

選項 02

或者,您可以通過--metadata-file pandoc 參數從 yaml 文件(位於與輸入 markdown 文件相同的目錄中)傳遞那些 YAML 選項。

因此,在 yaml 文件中指定pandoc 變量名稱和值,如下所示,

主.yml

author: Jphn Doe
mainfont: LiberationSans
geometry:
  - left=5cm
  - top=2cm
  - right=3cm
  - bottom=2cm
linestretch: 1.5
pagestyle: headings

然后運行rmarkdown::render ,(這里你仍然需要使用template=NULL ,否則你在main.yml中指定的幾何將被 rmardown default geometry:margin=1in覆蓋)

library(rmarkdown)

render(
  input = "test_yaml.md",
  output_format = rmarkdown::pdf_document(
    latex_engine = "xelatex",
    template = NULL,
    pandoc_args = c("--metadata-file", "main.yml")
  )
)

input.md (用於測試代碼的markdown文件)

# Rmarkdown

Hello from rmarkdown.

Lorem ipsum dolor sit amet, odio nec ornare tempor semper eget. Ligula lorem,
torquent ante mauris torquent feugiat dis finibus vitae.

Output 對於第一個選項

pdf輸出

Output 來自第二個選項

pdf輸出

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM