How to add a latex package to R Journal template f

2019-02-25 10:43发布

问题:

I am using rticle package by RStudio to write an article in R using rmarkdown package. In the draft template there is an option

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

Where I suspect I can add lines like \usepackage{package_name} or set a file name in which I load all desired Latex packages.

The first approach

I've tried to add one single package to preamble section like this but have stacked on an error

---
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}

The first approach code end

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

I've seen the same preamble - ignoring behavior in this issue https://github.com/rstudio/rticles/issues/11, but there was no response for that. I've also tried to specify file named header.tex that contains a line \usepackage{rotating} in a last argument of a yaml like this but also there happend to be an error.

---
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

EDIT

When I specify yaml in a way it is intended to work for pdf_document output

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

I receive an error that

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

EDIT 2

Even for an empty new document I get the same error

---
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

The header.tex document (which is in the same directory as saved empty new file) contains only 1 line:

\usepackage{rotating}

I am aware of in_header option in regular pdf_document output format (see even my files with RMD extensions on github) but it looks like to does not work with rticles::rjournal_article

EDIT 3

I am interested in adding other latex package than those that are included automatically in the RJwrappter.tex file which is generated after render of the .RMD file.

\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}

Anyone maybe knows how to solve this? I am using MiKTeX 2.9.

回答1:

I know this question's old but I just had the same problem as Marcin and wanted to share my solution.

What worked for me was changing the template.tex file in the rticles package for the ASA article. At the top of the template, where they have all the other \usepackage commands, just add your package(s). I had the same problem that you did and neither of the typical solutions as provided in the first answer worked for me.

To edit the template file, go to the rticles directory in your package library folder (.libPaths() in R), then open up rmarkdown/templates/rjournal_article/resources/RJWrapper.tex. Add your package amongst all the others. This should work for any of the other *_article/resources/template.tex files.



回答2:

No, that is wrong. In the YAML header declare that you want an include.

See the rmarkdown reference:

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

I have used header.tex for this purpose in several documents.

Edit: As you appear to get lost in the woods, here is a simplyfied example started from the default useR abstract template:

---
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.

which I coupled with the simplest possible header called marcin_header.tex (placed in the same directory) and containing just

\usepackage{booktabs}

Works for me.

Edit 2: Oh boy.

---
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.


回答3:

As dash2 mentioned you need to edit one of your rticles templates to take advantage of the header-includes or preamble clause. For example for the rticles::aea_article format the template could be in: /home/user/R/x86_64-pc-linux-gnu-library/3.3/rticles/rmarkdown/templates/aea_article/resources/template.tex

This is for a linux system, but there's a location on your computer, whatever the OS, where R libraries are stored and you need to edit this file to include the snippet that dash2 provided.



回答4:

There is no easy way to do this, as of Oct 16. What you need to do is find the relevant template and edit it to include the lines:

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

You can find the template by looking at the source code for e.g. rjournal_article.