How to set up animation plot in pdf output of Knitr - r

I want to test whether the animation works in pdf output from .Rmd file (see the figure below).
I have installed the FFmpeg with brew install ffmpeg --with-libvpx on my Mac OS and sudo tlmgr install animate typed in terminal indicates that the animate package is already available for my Tex (tlmgr install: package already present: animate). However, the command listed below seems not work yet,although it works fine if I replace pdf_document with html_document. What might be the problem for this? I have pasted the error information in the final section (I have referred to a related question here: Plot animation in knitr rmarkdown).
---
title: "Example"
author: ""
header-includes:
- \usepackage{animate}
output:
pdf_document
---
```{r,fig.show='animate', out.width = ''}
for(i in 1:10) plot(rnorm(10))
```
---
output file: test.knit.md
! Missing number, treated as zero.
\relax
l.133 ...les/figure-latex/unnamed-chunk-1-}{1}{10}
pandoc: Error producing PDF
Error: pandoc document conversion failed with error 43
Execution halted

PDF doesn't support animations, except MOV apparently. The first couple of google results for 'PDF gif support' are informative, including this stackoverflow question: Is it possible to embed animated GIFs in PDFs?

Related

RMarkdown xelatex !Missing & Inserted

I originally posted with issues here:
Rmarkdown with xelatex
I have since reinstalled, but now I'm having an entirely new issue.
Using the following code (inside a .Rmd file):
---
header-includes:
- \usepackage{graphicx}
output:
pdf_document:
latex_engine: xelatex
---
```{r}
knitr::include_graphics("F:/Big Folder/My Folder/image.png")
```
I get the following error:
! Missing $ inserted.
<inserted text>
$
l.134 .../Big Folder/My Folder/image}
pandoc.exe: Error producing PDF
Error: pandoc document conversion failed with error 43
I think it has something to do with the space in the path, because when I put the image directly in F, it works fine.
Unfortunately, this project requires that I use folders with spaces in the name.
I'm on Windows 10 64 bit, running MikTex 2.9 with updated packages (including fontspec). Miktex is installed in program files, but I also tried an install to a different folder but had the same result.
I'm also running a recently installed/updated rstudio with updated knitr and rmarkdown packages.
This does work on pdflatex, even with spaces in the file name.
Got it, the actual image file was formatted as image_something.png.
The "_" was throwing it off and removing it fixed it.
Also, if I want to keep the underscore I need to add two escape characters "\\".
F:/Big Folder/image\\_something.png
This is somewhat different than the non rmarkdown latex solution in that it only requires a single escape character.
This solution was modified from https://tex.stackexchange.com/questions/266575/how-can-i-write-an-under-dash-in-text-without-invoking-italics-or-some-weird-fon.
I would still be curious as to why spaces are allowed on pdflatex but not xelatex.

Using pandoc-crossref with R bookdown

Is it possible to use pandoc-crossref in bookdown?
I tried to change the yaml header to:
output:
bookdown::tufte_book2:
toc: yes
highlight: tango
pandoc_args: -F /usr/local/bin/pandoc-crossref
Which should pass the filter to pandoc, but I get the error:
pandoc: Error running filter pandoc-crossref:
Could not find executable ' pandoc-crossref'.
The above error does not make sense, as I entered the correct path. What kind of env is bookdown using, which is precluding the access to the filter file?
Here is an example
---
output: bookdown::html_document2
---
# Section name {#id}
```{r pressure, echo=FALSE, fig.cap='test plot'}
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. To cross-reference the figure, use `\#ref(fig:pressure)` to produce Figure \#ref(fig:pressure). All this is found within the section \#ref(id).
Which produces...
See https://bookdown.org/yihui/bookdown/figures.html for the official documentation.
I had a similar issue while I was trying to put numbers to equations in Word files (SO question). I got the same error Could not find executable 'pandoc-crossref'.
My RStudio installation (on Windows) did not come with pandoc-crossref. Here is what I did:
Downloaded pandoc-crossref from here.
Find the path where RStudio saved the file pandoc.exe:
rmarkdown::find_pandoc()
Put the pandoc-crossref.exe in the folder I got in (2).

Converting Rmarkdown to PDF without RStudio

I would like to convert a *.Rmd to document to PDF without rstudio being available.
Current approach
Current approach follows the following steps:
*.Rmd document is passed to knitr: knit(input = "report.Rmd"))
Obtained md is converted via pandoc:
# Convert
pandoc --smart --to latex \
--latex-engine pdflatex \
-s report.md \
-o report.PDF
Problems
This results in the following problems, the top section of the Rmarkdown document:
---
title: "Report Title"
author: "Person"
output: pdf_document
classoption: landscape
---
and shows as:
all text is centered, whereas I would like for it to be left-aligned:
Possible approach
I would like to make use of the rmarkdown::render; however, despite setting RSTUDIO_PANDOC (as discussed here), the command fails on pandoc not being available.
Desired outcome
I don't care much whether the utilised mechanism makes use of the rmarkdown::render, what I want to achieve is:
Landscape page layout across all pages
Left-aligned text
Ability to exercise minimum control over the document by controlling default fonts
Ideally, I would like to do as much as in the *.Rmd file as possible without the need to add parameters to the pandoc command.
Updates, following comments
I'm working on Linux and pandoc is installed, I can execute pandoc command pass files and generate exports with no problems. It only doesn't work with the rmarkdown::render package.
Concerning the hooks and *.Rmd files, this is what I'm trying to understand as I see that that the first section of my *.Rmd file is ignored. The current process looks as follows:
*.Rmd (not much in it, just title section and dummy text and code that renders but wrongly justified) >
*.R file running one line knit(input = "report.Rmd")) >
*.sh file running pandoc command and generating PDF
Concerning:
if all that is in place, it is indeed just a call to
rmarkdown::render(...)
The rmarkdown::render(...) fails:
Error: pandoc version 1.12.3 is required and was not found ...
However:
>> rmarkdown::pandoc_available()
[1] TRUE
and:
$ pandoc -v
pandoc 1.9.4.1 (...)
The RSTUDIO_PANDOC points to pandoc.
A few things:
"the command fails on pandoc not being available." well you must have pandoc installed in order to call it -- but you didn't say what OS you have. On Linux it is pretty trivial to install pandoc from the package manager; otherwise jgm has binaries for you on the site; "should" be similar on OS X
for different styling you need to modify the LaTeX code which you can via numerous hooks to include macro files; see the RMarkdown cheat sheets for detail
if you want to exercise more control, you can supply your own template; I have done so in the tint package
(which is also on CRAN)
if all that is in place, it is indeed just a call to rmarkdown::render(...)
Error: pandoc version 1.12.3 is required and was not found
I think the error says it plainly: you need pandoc 1.12.3 and you have pandoc 1.9.4.1
I do not know, however, why such a specific version is required.

Rstudio pdf knit fails with "Environment Shaded undefined" error

When trying to knit a PDF using a template from package rticles output: rticles::acm_article I get the following error:
! LaTeX Error: Environment Shaded undefined.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help.
...
l.76 \begin{Shaded}
This appears to only happen when I include code chunks within the output document.
Reproducible example:
You will need to start a new R Markdown document using the New Document -> From Template -> Association for Computing Machinery. Here is the R Markdown file:
---
title: Short Paper
author:
- name: I Am Me
email: me#email.com
affiliation: Fictional University
abstract: |
This is the abstract.
It consists of two paragraphs.
output:
rticles::acm_article:
keep_tex: true
---
## Simple test
Code chuck follows:
```{r}
plot(rnorm(10))
```
This above example, however, works if I set echo=FALSE in the header. You won't get code in the output, but for an academic paper you probably don't need it anyway, an if you do you can display it in a different manner.
Notes:
R version 3.3.1 (2016-06-21)
Platform: x86_64-pc-linux-gnu (64-bit),
running Ubuntu 16:10
I made sure I have the texlive-latex-base, texlive-latex-recommended, and texlive-latex-extra package installed, but still no luck
I also tried generating a pdf from the intermediate .tex file, as suggested here, but I get the same error.
I considered this approach, but it didn't work, I still got an error (maybe I need to tweak to work in my context, but not sure how)
So, the issue here is the rticles templates sometimes omit a means for pandoc to inject code highlighting environments, e.g. Shaded. To get around this, you would have to insert into the template.tex preamble:
$if(highlighting-macros)$
$highlighting-macros$
$endif$
I struggled with fixing the error and actually raised another question which I was able to answer after quite some experimentation and searching for other related questions/answers.
In short the solution presented above by coatless works perfect given you know the location of the template.tex file. Obviously, I played around with the wrong files before I came across a related question on rticles and how to embed (missing) LATEX.
The longer answer can be found in my "another question". For reference here are the key steps:
locate the template.tex file in your R package rticles library. If you do not know where you have your package library use .libPaths() in your RStudio console. Then work your way to the resources subfolder of your rticle template. In my case:
R-3.5.0/library/rticles/rmarkdown/templates/ieee_article/resources.
add the fix proposed by coatless above in the preamble of the template.tex. The preamble is anything before the line \begin{document}:
$if(highlighting-macros)$
$highlighting-macros$
$endif$
save template.tex and go back to R/RStudio and hit the knit button. R code chunks are now nicely displayed, if you do not suppress their printing with echo = FALSE.
I know the post is old, but I'll put this here for reference for others who have the same problem.
The reason is that in the latex template the environment Shaded is trying to be redefined. However the environment only exists if you are using R chunks in your pdf. So if you don't have any r chunks in the pdf, it tries to redefine something which was never defined - causing the error.
The solution is to modify the \renewenvironment{Shaded} command in the latex template.
https://github.com/yihui/bookdown-chinese/commit/a3e392593b464ba31a7eceb0cd60f7e0bd112798
I found a work around. If I hide the code with chunk option echo = FALSE, a PDF is generated with no issues.
Reproducible example 1:
title: Short Paper
author:
- name: I Am Me
email: me#email.com
affiliation: Fictional University
abstract: |
This is the abstract.
It consists of two paragraphs.
bibliography: sigproc.bib
output:
rticles::acm_article:
keep_tex: true
---
## Simple test
Code chuck follows:
```{r}
plot(rnorm(10))
```
Knit to PDF fails with ! LaTeX Error: Environment Shaded undefined.
Example 2:
---
title: Short Paper
author:
- name: I Am Me
email: me#email.com
affiliation: Fictional University
abstract: |
This is the abstract.
It consists of two paragraphs.
bibliography: sigproc.bib
output:
rticles::acm_article:
keep_tex: true
---
## Simple test
Code chuck follows:
```{r echo=FALSE}
plot(rnorm(10))
```
Knit to PDF works!.
The only difference between the two examples is adding echo=FALSE to the code chunk header. You won't get code in the output, but for an academic paper you probably don't need it anyway, an if you do you can display it in a different manner.
The rticles templates typically do not directly allow shading of code. As a result,
the required $highlighting-macros$ which is used to control the shading is not included.
If for any reason, you cannot (or do not) want to directly edit the template.tex file as suggested by #coatless, you can alternatively consider include the code directly in the acm_proc_article-sp.cls file:
\usepackage{color}
\usepackage{fancyvrb}
\newcommand{\VerbBar}{|}
\newcommand{\VERB}{\Verb[commandchars=\\\{\}]}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
% Add ',fontsize=\small' for more characters per line
\usepackage{framed}
\definecolor{shadecolor}{RGB}{248,248,248}
\newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}}
\newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{#1}}
\newcommand{\DecValTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\FloatTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\CharTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\StringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\ImportTok}[1]{#1}
\newcommand{\CommentTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\OtherTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{#1}}
\newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\VariableTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.81,0.36,0.00}{\textbf{#1}}}
\newcommand{\BuiltInTok}[1]{#1}
\newcommand{\ExtensionTok}[1]{#1}
\newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.77,0.63,0.00}{#1}}
\newcommand{\RegionMarkerTok}[1]{#1}
\newcommand{\InformationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\WarningTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\AlertTok}[1]{\textcolor[rgb]{0.94,0.16,0.16}{#1}}
\newcommand{\ErrorTok}[1]{\textcolor[rgb]{0.64,0.00,0.00}{\textbf{#1}}}
\newcommand{\NormalTok}[1]{#1}
You may also wish to see my answer to a similar question, whereby the same code is put into a header.tex file in the same directory as your project.

Set margin size when converting from Markdown to PDF with pandoc

I have created an RMarkdown file in RStudio and managed to knit it with knitr into an HTML and .md file. Next, I used pandoc to convert the .md file into a PDF file (I get an error if I try and convert from the .html file). However, the PDF that is produced have massive margins (like this http://johnmacfarlane.net/pandoc/demo/example13.pdf). How can I get pandoc to produce something with smaller margins? I have looked through the pandoc user guide, but haven't found anything useful.
Recent versions of rmarkdown and pandoc
In more recent versions of rmarkdown, the settings of margins can be done in the YAML header via the top-level element geometry. What you specify in the geometry tag will be piped into the LaTeX template that ships with Pandoc via the following LaTeX snippet
$if(geometry)$
\usepackage[$for(geometry)$$geometry$$sep$,$endfor$]{geometry}
$endif$
For example, to specify margins that are 2cm in width one would include
---
title: "Habits"
author: John Doe
date: March 22, 2005
geometry: margin=2cm
output: pdf_document
---
For more complex specifications to be passed to the geometry LaTeX package, string options together as you would with LaTeX:
---
title: "Habits"
author: John Doe
date: March 22, 2005
geometry: "left=3cm,right=3cm,top=2cm,bottom=2cm"
output: pdf_document
---
Original answer
This is a LaTeX question as Pandoc is rendering to PDF via LaTeX - what you linked to represents the default margins on a LaTeX document.
The geometry LaTeX package for example can be used to alter the margins of the page. However you'll need a way to tell Pandoc to use this by including it ins the LaTeX header applied to the converted md file.
How you do this is documented in the Pandoc User Guide. See in particular the --template=FILE command line argument and the Templates section. Essentially, either find and modify the default template to include the LaTeX instructions you want to use or start your own template from scratch and place it in the appropriate location; see the --data-dir command line argument.
Another alternative if you are using a recent version of Pandoc is to use the variable argument (set either with -V KEY[=VAL] or --variable=KEY[:VAL]). The geometry package was added to the default LaTeX template in May 2012 (see this discussion). As such, if you wanted to change the page margins, you can use:
pandoc -V geometry:margin=1in -o output.pdf input.md
You can specify multiple variable values too. For instance, if you wanted to create a 4 by 6 inch pdf with half-inch margins, you can use:
pandoc -V geometry:paperwidth=4in -V geometry:paperheight=6in -V geometry:margin=.5in -o output.pdf input.md
In more recent versions of pandoc, you can set a number of parameters in the YAML header. You can set the geometry here, for example:
---
title: Pandoc nice margins example
author: naught101
geometry: margin=3cm
---
body text of document
When pandoc converts it to a pdf, it should have correct margins.
FTR: "recent version" above seems to include Knitr 1.30, before I needed following header:
---
pdf_document: null
geometry: margin=1cm
output: pdf_document
---
Without the additional pdf_document: null the geometry setting had no effect. With it, settings from https://bookdown.org/yihui/rmarkdown-cookbook/latex-variables.html worked.

Resources