I'd like to use the bib2gls latex package in my rmarkdown document to insert a list of acronyms from a .bib file when I knit to a pdf document.
Doesn't work:
Using a .bib file to store my acronyms. Example:
abbreviations.bib file:
#abbreviation{ecoli,
short={E.~coli},
long={Escherichia coli}
}
#abbreviation{raustralis ,
short ={R.~ australis},
long={Rickettsia australis}
}
preamble.tex
% fixes problem with glossaries causing mathspec
% to return an error asking for amsmath to be loaded first
\makeatletter
\let\RequirePackage\original#RequirePackage
\let\usepackage\RequirePackage
\makeatother
% using bib2gls
\usepackage[record,abbreviations,style=index]{glossaries-extra}
\setabbreviationstyle{long-short}
\GlsXtrLoadResources[src={abbreviations},selection={all}]
rmarkdown file:
---
output:
pdf_document:
latex_engine: xelatex
includes:
in_header: preamble.tex
title: "mwe"
---
## R Markdown
This is the first instance \gls{ecoli}, \gls{raustralis}. This is the second instance: \gls{ecoli}, \gls{raustralis}.
\printunsrtglossary[title={Abbreviations},type=abbreviations]
The output when I knit the rmarkdown documents looks like:
What works:
Removing \GlsXtrLoadResources[src={abbreviations},selection={all}] and adding my entries into the preamble.
% using bib2gls
\usepackage[abbreviations,style=index]{glossaries-extra}
\setabbreviationstyle{long-short}
\newabbreviation{ecoli}{E.~coli}{Escherichia coli}
\newabbreviation{raustralis}{R.~australis}{Rickettsia australis}
I'd like to use the separate .bib file in the first approach if possible. Unless there is a better approach to do this in R. Session info is below:
R version 3.5.0 (2018-04-23)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C LC_TIME=English_United States.1252
The Problem is that bib2gls requires an additional program to be run, which extracts the glossary entries from the bib file, but rmarkdown does not know about that. In general, if you want to do something more complicated on the LaTeX side, there is a good chance that latexmk already knows how to do that. I am not sure if this is here the case, c.f. https://tex.stackexchange.com/questions/400325/latexmkrc-for-bib2gls. However, you can give it a try using
```{r echo=FALSE}
Sys.setenv(RSTUDIO_PDFLATEX = "latexmk")
```
Related
I am trying to run a Lua filter for multiple bibliographies to create a separate list of references for each chapter of my thesis.
This is my YAML header:
title: ''
output:
pdf_document:
latex_engine: pdflatex
number_sections: TRUE
pandoc_args: --lua-filter=multiple-bibliographies.lua
always_allow_html: true
geometry: "left = 4cm, right = 2cm, top = 2cm, bottom = 2cm"
fontsize: 11pt
header-includes:
- \usepackage{float}
- \usepackage{sectsty}
- \usepackage{paralist}
- \usepackage{setspace}\spacing{1.5}
- \usepackage{fancyhdr}
- \usepackage{lastpage}
- \usepackage{dcolumn}
- \usepackage{natbib}
- \usepackage[nottoc, numbib]{tocbibind}
- \usepackage{ragged2e}
- \usepackage{pdflscape}
- \usepackage{textgreek}
- \usepackage{graphicx}
classoption: twoside
bibliography_Intro: Intro.bib
bibliopgraphy_ch1: Endozoochory.bib
bibliopgraphy_ch2: Vysevovy_clanek.bib
bibliopgraphy_ch3: Seeds.bib
bibliopgraphy_ch4: Landscape.bib
citation_package: biblatex
csl: journal-of-ecology.csl
At the end of each chapter I have:
## References
::: {#refs_Intro}
:::
I am not very experienced with pandoc and LaTeX so this might be a simple error on my side. However, when I try to knit the document, it gives me an error:
Error running filter multiple-bibliographies.lua: PandocFilterError
"pandoc" "Could not find executable pandoc" stack traceback:
multiple-bibliographies.lua:50: in upvalue 'run_citeproc'
multiple-bibliographies.lua:82: in function
<multiple-bibliographies.lua:68> Error: pandoc document conversion
failed with error 83
I've been struggling with the bibliographies for several days and I think this is the last option but I am open to other possibilities in RMarkdown.
I am running this on Ubuntu 20, pandoc version 2.19.2, rmarkdown_2.18, R version 4.2.2
The problem is that the filter tries to call pandoc, but cannot find it, as it does not check the environment variables set by RStudio. This is fixed in the updated version of this filter, multibib, which uses some newer pandoc features to work around this. The new version can also be installed as a Quarto extension.
Note, however, the slightly changed syntax used in multibib, as everything must now be nested below the bibliography key:
---
bibliography:
Intro: Intro.bib
ch1: Endozoochory.bib
ch2: Vysevovy_clanek.bib
ch3: Seeds.bib
ch4: Landscape.bib
---
Please also try the section-bibliographies filter, which might be a better fit for your use-case.
I have written a manuscript I would like to submit to a journal in Rmarkdown. The journal accepts word and latex files, so I am looking for a way to generate a working .tex file out of my .Rmd file.
I have read some posts that allude to this being possible (e.g., How to generate LaTeX file without preamble in R markdown?) and this is getting me some of the way, but I am still having problems.
For example, using the method mentioned in the post above, I can convert a test .Rmd into something with a .tex filetype. This is the test Rmarkdown (just the usual template for new files):
---
title: "Test document"
author: "Me"
date: "23 7 2020"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r cars}
summary(cars)
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
This compiles fine to PDF as it should. Then, in the console I run:
knitr::knit("test.Rmd")
to get a markdown file test.md in my working directory, and then I can apparently convert this .md file to .tex with
rmarkdown::pandoc_convert("test.md", to = "latex", output = "test.tex")
This produces a .tex file that, when I double click on it, pops up a PDF view of the file that looks fine. Taking a look at the file though, it is incomplete or at least unfamiliar to me:
\hypertarget{r-markdown}{%
\subsection{R Markdown}\label{r-markdown}}
This is an R Markdown document. Markdown is a simple formatting syntax
for authoring HTML, PDF, and MS Word documents. For more details on
using R Markdown see \url{http://rmarkdown.rstudio.com}.
When you click the \textbf{Knit} button a document will be generated
that includes both content as well as the output of any embedded R code
chunks within the document. You can embed an R code chunk like this:
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{summary}\NormalTok{(cars)}
\end{Highlighting}
\end{Shaded}
\begin{verbatim}
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
\end{verbatim}
\hypertarget{including-plots}{%
\subsection{Including Plots}\label{including-plots}}
You can also embed plots, for example:
\begin{figure}
\centering
\includegraphics{figure/pressure-1.png}
\caption{plot of chunk pressure}
\end{figure}
Note that the \texttt{echo\ =\ FALSE} parameter was added to the code
chunk to prevent printing of the R code that generated the plot.
As far as I can tell, it is missing the preamble, \begin{document}, \end{document}, and I have no idea what is going on with hypertarget bit of the section headers. Unsurprisingly, it does not "re-compile" when I hit run in MiKTeX. The verbatim bits for the code chunks look to be what I'm after, though.
So, is there a way to generate a .tex file that compiles out of an .Rmd file? Or will I have to manually write the preamble and all that? If the answer to my problem is "read up pandoc", then fair enough, I will have to bite the bullet and finally have a look at it. But I find it hard to imagine that there is no good (easy) way to prepare submittable manuscripts in Rmarkdown.
Pandoc generates LaTeX snippets by default, i.e., not a full document. This can be changed by calling pandoc with the --standalone option:
rmarkdown::pandoc_convert(
"test.md",
to = "latex",
output = "out.tex",
options = "--standalone"
)
You can let R do the work and shorten your commands to
render("test.Rmd", output_format = "latex_document")
A project of interest might be rticles.
I am creating pdf slides using rmarkdown and beamer. When using latex instead of rmarkdown, I can include arbitrary pages from pdf files into my slides with
\usepackage{pdfpages}
...
\includepdf[pages={1-10}]{another_file.pdf}
Is it possible to achieve this with rmarkdown? Note that when just issuing the \includepdf command, intended to be located between slides, pandoc wraps it between \begin{frame} and \end{frame}.
Not a perfect solution but it works (adapted from https://tex.stackexchange.com/questions/11458/error-when-inserting-a-pdf-page-into-a-beamer-presentation?newreg=10fd6a4a46c642118eb4ec905cf87303): use \includegraphics instead of includepdf. This works inside of frames but you have to manyally create a frame for each page you want to insert as it only allows to insert a single page at time.
---
title: "Foo"
output:
beamer_presentation:
---
# Stolen image
---
\includegraphics[page=1,width=\paperwidth]{lecture-1-terminal.pdf}
# My own work
## My slide 1
Great stuff!
In rmarkdown you can use pdfpages like this:
---
output:
beamer_presentation:
keep_tex: true
header-includes:
- \usepackage{pdfpages}
- \setbeamercolor{background canvas}{bg=}
- \makeatletter\beamer#ignorenonframefalse\makeatother
---
test
``` {=latex}
\end{frame}
\includepdf[pages=1-10]{example-image-duck}
\begin{frame}
```
test
(the line \setbeamercolor{background canvas}{bg=} is necessary for beamer versions < 3.64, a patch has been added in 9e3bb9)
As you mentioned, you can inlcude raw TeX in Pandoc Markdown. For me the following works:
$ pandoc -t beamer
foo
\includepdf[pages={1-10}]{another_file.pdf}
bar
^D
which results in:
\begin{frame}
foo
\includepdf[pages={1-10}]{another_file.pdf}
bar
\end{frame}
Maybe you need to update your pandoc version? (I'm currenlty using 2.0 from pandoc nightlies)
I am trying to edit the latex file generated by RStudio but it seems the file generated by RStudio does not compile out of the box.
Here is the template rmarkdown (edited a little as I want to show code in my slides)
---
title: "Untitled"
author: "SN248"
date: "April 17, 2016"
output:
beamer_presentation:
keep_tex: true
theme: "CambridgeUS"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
Some code
```{r eval=FALSE, echo=TRUE}
install.packages("mypackage")
library(mypackage)
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=TRUE}
plot(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
Note, that I added keep_tex: true to get the intermediate tex file, the generated tex file is pasted below:
\begin{frame}[fragile]{R Markdown}
This is an R Markdown document. Markdown is a simple formatting syntax
for authoring HTML, PDF, and MS Word documents. For more details on
using R Markdown see \url{http://rmarkdown.rstudio.com}.
Some code
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{install.packages}\NormalTok{(}\StringTok{"mypackage"}\NormalTok{)}
\KeywordTok{library}\NormalTok{(mypackage)}
\end{Highlighting}
\end{Shaded}
\end{frame}
\begin{frame}[fragile]{Including Plots}
You can also embed plots, for example:
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{plot}\NormalTok{(pressure)}
\end{Highlighting}
\end{Shaded}
\includegraphics{Untitled_files/figure-beamer/pressure-1.pdf}
Note that the \texttt{echo\ =\ FALSE} parameter was added to the code
chunk to prevent printing of the R code that generated the plot.
\end{frame}
I noticed that the generated tex file does not compile out of the box. So, I added the necessary bits (i.e., package listings for code highlighting) so that it compiles
\documentclass[hyperref={pdfpagelabels=false}]{beamer}
\usepackage{lmodern}
\usepackage{listings}
\usetheme{CambridgeUS}
\begin{document}
...
\end{document}
I am still not able to compile this tex file as the bit begin{Shaded} does not compile, I get the following error:
LaTeX Error: Environment Shaded undefined
My question is, how to generate tex file from RStudio which compiles out of the box. If not, which packages should I use to compile the tex code (especially to show code with highlighting) shown below
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{install.packages}\NormalTok{(}\StringTok{"mypackage"}\NormalTok{)}
\KeywordTok{library}\NormalTok{(mypackage)}
\end{Highlighting}
\end{Shaded}
Just to give some context, I am trying to compile the tex file so that I can add sections and subsections in the tex code, I am not sure how to do that in the rmarkdown file.
Thank you very much for your help.
SN248
I can see that you are using RMarkdown and want to generate a stand alone .tex file that can be compiled without knitr.
I do the same thing with SWeave. As SWeave is a latex document with R code, you can easily compile it once SWeave is run and figures are created.
For Example, I have created this document in RStudio and compile it in Rstudio.
\documentclass{article}
\title{Best title ever}
\author{XTriMa, PhD}
\begin{document}
\maketitle
\tableofcontents
\section{Method} \label{intro}
Let me make two vectors with random numbers and plot them.
<<>>=
x<-runif(100)
y <- x^(sin(x))
plot(x, y)
#
\end{document}
My folder looks like this
figure test.log test.Rnw test.tex
test-concordance.tex test.pdf test.synctex.gz test.toc
Now if you run
$pdflatex test.tex
It works as you intended. However command "latex" will not work as it can not work with pdf graphics in figures.
Advantage: I can work with R and latex very efficiently and get rid of intermediate software/clients like RStudio and Sweav.
How can I show page numbers (preferably like 4/10, 5/10 etc.) on an rmarkdown beamer presentation?
In the front matter of the document, you can include a .tex file with extra commands as shown in RStudio's documentation here.
I created a new .tex file that I called "header_pagenrs.tex" which only includes the top 2 lines from #user4281727's answer:
\setbeamertemplate{navigation symbols}{}
\setbeamertemplate{footline}[page number]
Then I referenced this file at the top of my .Rmd file:
---
title: "Things and Stuff"
author: "Me"
output:
beamer_presentation:
includes:
in_header: header_pagenrs.tex
---
If this still gives you errors, you might also be missing some required TeX packages (a separate problem from RStudio and rmarkdown).
Here's another option that worked for me. Didn't need to add a .tex file to my folder. Just included the following (based on above code from #civilstat) at the top of my Markdown doc.
---
title: 'Your Title'
author: "Your Name"
date: "July 4, 1776"
output:
beamer_presentation(keep_tex = TRUE): default
header-includes:
- \setbeamertemplate{navigation symbols}{}
- \setbeamertemplate{footline}[page number]
---
If your beamer version is reasonable up to date (>= v3.48), you can adjust the format of the frame numbers while keeping the footline of your chosen beamer theme unchanged otherwise.
---
output:
beamer_presentation:
theme: "Berlin"
keep_tex: true
header-includes:
- \setbeamertemplate{page number in head/foot}[totalframenumber]
---
test
Try to put the lines below into the template
\setbeamertemplate{navigation symbols}{}
\setbeamertemplate{footline}[page number]
~/Library/R/3.1/library/rmarkdown/rmd/beamer/default.tex