如何从 R 中的 rticle 包将乳胶包添加到 R Journal 模板

How to add a latex package to R Journal template from rticle package in R

我正在使用 rticle package by RStudio to write an article in R using rmarkdown 包。 在草稿模板中有一个选项

...
preamble: >
  % Any extra latex you need in the preamble
output: rticles::rjournal_article
...

我怀疑我可以添加像 \usepackage{package_name} 这样的行或设置一个文件名,我可以在其中加载所有需要的 Latex 包。

第一种方法

我试过像这样将一个包添加到 preamble 部分,但出现错误

---
title: Capitalized Title Here
author:
  - name: Author One
    affiliation: Affiliation
    address:
    - line 1
    - line 2
    email:  author1@work
  - name: Author Two
    affiliation: Affiliation
    address:
    - line 1
    - line 2
    email:  author2@work
abstract: >
  An abstract of less than 150 words.
preamble: >
  \usepackage{rotating}
  % Any extra latex you need in the preamble
output: rticles::rjournal_article
---

\begin{sidewaystable}
    \centering
    \caption{Your caption here}
   \begin{tabular}{ll}
    First First & First Second\
    Second First & Second Second
    \end{tabular}
\end{sidewaystable}

第一种方法代码结束

BŁĄD: running 'texi2dvi' on 'RJwrapper.tex' failed

LaTeX errors:
D:\RTCGA.data\Untitled\Untitled.tex:11: LaTeX Error: Environment sidewaystable 
undefined.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help
D:\RTCGA.data\Untitled\Untitled.tex:13: Package caption Error: \caption outside
 float.

See the caption package documentation for explanation.
Type  H <return>  for immediate help
D:\RTCGA.data\Untitled\Untitled.tex:18: LaTeX Error: \begin{article} on input l
ine 20 ended by \end{sidewaystable}.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help
Dodatkowo: Warning message:
uruchomione polecenie '"C:\PROGRA~1\MIKTEX~1.9\miktex\bin\x64\texi2dvi.exe" --quiet --pdf "RJwrapper.tex" --max-iterations=20 -I "C:/PROGRA~1/R/R-32~1.0/share/texmf/tex/latex" -I "C:/PROGRA~1/R/R-32~1.0/share/texmf/bibtex/bst"' otrzymało status 1 
Wykonywanie wstrzymane

我也看到过相同的情况 preamble - 忽略此问题中的行为 https://github.com/rstudio/rticles/issues/11,但没有对此做出回应。 我还尝试指定名为 header.tex 的文件,该文件在这样的 yaml 的最后一个参数中包含一行 \usepackage{rotating},但也碰巧出现了错误。

---
title: Capitalized Title Here
author:
  - name: Author One
    affiliation: Affiliation
    address:
    - line 1
    - line 2
    email:  author1@work
  - name: Author Two
    affiliation: Affiliation
    address:
    - line 1
    - line 2
    email:  author2@work
abstract: >
  An abstract of less than 150 words.
preamble: > 
   \usepackage{rotating}
output: 
   rticles::rjournal_article:
   includes:
      in_header: header.tex
---
BŁĄD: running 'texi2dvi' on 'RJwrapper.tex' failed

LaTeX errors:
D:\RTCGA.data\rticle\rticle.tex:145: LaTeX Error: Environment sidewaystable und
efined.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help
D:\RTCGA.data\rticle\rticle.tex:147: Package caption Error: \caption outside fl
oat.

See the caption package documentation for explanation.
Type  H <return>  for immediate help
D:\RTCGA.data\rticle\rticle.tex:192: LaTeX Error: \begin{article} on input line
 20 ended by \end{sidewaystable}.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help
Dodatkowo: Warning message:
uruchomione polecenie '"C:\PROGRA~1\MIKTEX~1.9\miktex\bin\x64\texi2dvi.exe" --quiet --pdf "RJwrapper.tex" --max-iterations=20 -I "C:/PROGRA~1/R/R-32~1.0/share/texmf/tex/latex" -I "C:/PROGRA~1/R/R-32~1.0/share/texmf/bibtex/bst"' otrzymało status 1 
Wykonywanie wstrzymane

编辑

当我以某种方式指定 yaml 时,它旨在用于 pdf_document 输出

...
preamble: > 
   \usepackage{rotating}
output:
  rticles::rjournal_article:
    includes:
      in_header: header.tex
...

我收到一个错误

Error in (function ()  : 
  unused (includes = list(in_header = "header.tex"))

编辑 2

即使是一个空的新文档,我也会得到同样的错误

---
title: Capitalized Title Here
author:
  - name: Author One
    affiliation: Affiliation
    address:
    - line 1
    - line 2
    email:  author1@work
  - name: Author Two
    affiliation: Affiliation
    address:
    - line 1
    - line 2
    email:  author2@work
abstract: >
  An abstract of less than 150 words.
preamble: >
  % Any extra latex you need in the preamble
output:
  rticles::rjournal_article:
    includes:
      in_header: header.tex
---

Hello World

header.tex 文档(与保存的空新文件位于同一目录)仅包含 1 行:

\usepackage{rotating}

我知道常规 pdf_document 输出格式中的 in_header 选项(甚至用 RMD extensions on github 查看我的文件)但它看起来不适用于 rticles::rjournal_article

编辑 3

除了 .RMD 文件渲染后生成的 RJwrappter.tex 文件中自动包含的那些之外,我有兴趣添加其他乳胶包。

\documentclass[a4paper]{report}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{RJournal}
\usepackage{amsmath,amssymb,array}
\usepackage{booktabs}

%% load any required packages here


\begin{document}

%% do not edit, for illustration only
\sectionhead{Contributed research article}
\volume{XX}
\volnumber{YY}
\year{20ZZ}
\month{AAAA}

\begin{article}
  \input{rticle}
\end{article}

\end{document}

有人可能知道如何解决这个问题吗? 我正在使用 MiKTeX 2.9.

不,那是错误的。在 YAML header 中声明您想要包含。

参见rmarkdown reference

---
title: "Habits"
output:
  pdf_document:
    includes:
      in_header: header.tex
      before_body: doc_prefix.tex
      after_body: doc_suffix.tex
---

我已经在多个文档中使用 header.tex 来达到这个目的。

编辑: 由于您似乎迷失在树林中,这里是一个从默认 userR 抽象模板开始的简单示例:

---
title: "Title of Your Submission"
author:
 - name: FirstNameA LastNameA
keywords: First, Second, ... up to 5 keywords
output: 
  pdf_document:
    includes:
      in_header: marcin_header.tex
---

Content to be added here.

我将其与最简单的 header 相结合,称为 marcin_header.tex (放置在同一目录中)并且仅包含

\usepackage{booktabs}

适合我。

编辑 2: 天哪。

---
title: Capitalized Title Here
author:
  - name: Author One
    affiliation: Affiliation
    address:
    - line 1
    - line 2
    email:  author1@work
  - name: Author Two
    affiliation: Affiliation
    address:
    - line 1
    - line 2
    email:  author2@work
abstract: >
  An abstract of less than 150 words.
preamble: >
  % Any extra latex you need in the preamble
  \usepackage{booktabs}
output: rticles::rjournal_article
---

## Introduction

Rest omitted.

自 10 月 16 日起,没有简单的方法可以做到这一点。您需要做的是找到相关模板并对其进行编辑以包含以下行:

$for(header-includes)$                                                                                                 
$header-includes$                   
$endfor$

您可以通过查看源代码找到模板,例如rjournal_article.

正如 dash2 提到的,您需要编辑您的文章模板之一以利用 header-includes 或序言子句。例如,对于 rticles::aea_article 格式,模板可以是: /home/user/R/x86_64-pc-linux-gnu-library/3.3/rticles/rmarkdown/templates/aea_article/resources/template.tex

这是针对 linux 系统的,但是无论 OS 是什么,您的计算机上都有一个位置存储了 R 库,您需要编辑此文件以包含 dash2提供。

我知道这个问题很老,但我遇到了和 Marcin 一样的问题,想分享我的解决方案。

对我有用的是更改 ASA 文章的 rticles 包中的 template.tex 文件。在模板的顶部,他们有所有其他 \usepackage 命令,只需添加您的包。我遇到了和你一样的问题,第一个答案中提供的典型解决方案都不适合我。

要编辑模板文件,请转到包库文件夹中的 rticles 目录(.libPaths() 在 R 中),然后打开 rmarkdown/templates/rjournal_article/resources/RJWrapper.tex。将您的包裹添加到所有其他包裹中。这应该适用于任何其他 *_article/resources/template.tex 文件。

我通过添加

设法包含了浮动包

header-includes: - \usepackage{float}

在 frontiers-template 的开头进入我的 yaml-header