DiagrammeR and R Markdown pdf: How to crop to the diagram? - r-markdown

I have made a nice flowchart in DiagrammeR. I have also plotted it into R Markdown and I am able to knitr it into a pdf. As you see in the following screenshot, the figure is not centered (even though I included fig.align = 'center'). But there is also a huge gap between my figure and the following text. How do I "crop" the diagram so I removes the space and center the diagram?

You will find problems generally with markdown problems so you will have to modify manually from your latex code so I would recommend that you put in your yaml:
---
output:
pdf_document:
latex_engine: pdflatex
keep_tex: true
---

Related

Why won't RMarkdown with Bookdown recognize Tufte formatting?

I am working on a Bookdown project and would like to use Tufte formatting.
I downloaded the Bookdown demo and the tufte package. Then, I simply changed the YAML in index.Rmd to:
---
title: "A Minimal Book Example"
author: "Yihui Xie"
date: "`r Sys.Date()`"
site: bookdown::bookdown_site
output:
bookdown::tufte_html_book:
toc: yes
css: toc.css
documentclass: book
bibliography: [book.bib, packages.bib]
biblio-style: apalike
link-citations: yes
github-repo: rstudio/bookdown-demo
description: "This is a minimal example of using the bookdown package to write a book. The output format for this example is bookdown::gitbook."
---
Where the only change is in the output: option, which is specified by the Bookdown book
And then tried both the Knit button and bookdown::render_book(). The demo built successfully.
However, it did not produce Tufte formatting. Instead I get default formatting, with a white background, non-italic section headings, and footnotes instead of margin notes (I added a footnote to the doc on a second run to make this visible):
I also tried this with different spacing before the "bookdown"/"toc" etc. stuff, and with tufte::tufte_html: default instead of any of the "bookdown"/"toc" stuff.
What do I need to do to get Bookdown to recognize the Tufte formatting request? I feel like I am missing something exceedingly simple.

Inline R Logo command in R markdown

Having monkeyed around with the recommended solutions of Add inline image in Rmarkdown as applied to the problem of wanting to display an inline R logo, (for example https://images.app.goo.gl/4C57fs4D6AtS3D9D6) I found that while the recommended solutions didn't work for me for some reason, I could get an approximate solution to my problem using the command ![](R_logo.png){#id .class width=auto height=16px}.
Being an avid user of LaTeX, though rather newly introduced to R Markdown I thought it prudent to try to define the inclusion of the image as a command in a LaTeX-like fashion as recommended by R Markdown similar feature to "newcommand" in LaTex?, as I might be using it from time to time.
R Markdown seemed rather ambivalent to this approach however, having defined the command with \newcommand{\Rlogo}{![](R_logo.png){#id .class width=auto height=16px}}.
Do you have any recommendations as to what I'm doing wrong, and how/If I might define such a command within R markdown?
The icon package works really well to insert icons.
Here is an example, extracted from the package's page:
---
title: "Untitled"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(icon)
```
# Icon package
The `ìcon` package works really well:
`r icon::fontawesome("r-project", style = "brands")`
For a bigger icon:
```{r icon-style}
icon_style(fontawesome("r-project", style = "brands"), scale = 4)
```
-output

use of tikzdevice in RMarkdown with beamer output

I was wondering whether tikzdevice package can be used for producing R plots in RMarkdown, to be exported to a beamer presentation.
I would have thought that the solution I am looking for might be similar to the example with Sweave on page 15 of the tikzdevice manual.
What I have tried, without knowing exactly what I am doing, is to amend Yihui's example from the RMarkdown book:
---
title: "Habits"
author: John Doe
date: March 22, 2005
output: beamer_presentation
---
# In the morning
## Getting up
- Turn off alarm
- Get out of bed
---
```{r, echo=FALSE, results='tex'}
require(tikzDevice)
tikz(console=TRUE)
plot(sin, -pi, 2*pi, main="A Stand Alone TikZ Plot")
dummy <- dev.off()
```
Unfortunatly, this does not result in the tikz plot being rendered in the beamer presentation. Does anyone see how this can be achieved? Thanks.
Regards,
Michael
There are a few problems in what you wrote. LaTeX needs to be told to use the tikz package; and in knitr, you use results='asis' instead of results='tex'. Finally, you would need to tell tikz() what size figure to use.
However, rather than fix those things, it's easier to tell knitr to handle everything, by using the 'tikz' graphics device. So this works:
---
title: Demo with dev='tikz'
output: beamer_presentation
---
## A Tikz plot
```{r echo=FALSE, dev='tikz'}
plot(sin, -pi, 2*pi, main="A Stand Alone TikZ Plot")
```

RMarkdown to PPT: not loading reference_doc()

I am trying to use a PPT template to format a RStudio > RMarkdown > PPT file. Here's my testing file (saved as testing.Rmd):
---
title: "Aarduous Aardvarks"
author: "Aanonymous Aardvark"
date: "5/4/2019"
output: powerpoint_presentation
reference_doc: template.pptx
---
## My title
```{r myplot}
plot(mtcars)
```
When I first knit this, a PPT file was generated. I opened that file, made the Slide Master background red and the font blue (deliberately hideous for testing), saved it as "template.pptx" in the same folder as above.
However, when I re-knit the document, the background is still white and the font is still black. What do I need to get my template file recognized?
System:
Windows 7 Professional
Rstudio 1.2.1335
Rmarkdown 1.12
Pandoc: 2.6
PPT 2010 v14.0.6129.5000
Indentation in YAML is important. Use the structure as described in the official docs:
---
title: "Aarduous Aardvarks"
output:
powerpoint_presentation:
reference_doc: template.pptx
---
I have had this same problem, but it does not repair when the YAML above is inserted. The additional changes helped me solve the problem:
Save the reference_doc as a .potx file format, and refer to that extension in the YAML header
Go into View -> Slide Master to make sure the slides in the master are the format you expect (one time I thought I made the changes, but they showed up on the Home menu and not the Slide Master)
Under the Home menu, click the Dropdown under Layout and make sure AT THE LEAST that you see these four elements:
Title
Section Header
Title and Content
Two Content
When all three of these are taken care of, my slides knit fine from the Rmd.
I was facing a similar error when knitting to a powerpoint template- the indentation as documented (indenting 'reference_doc:...') was giving me an error: "Scanner error: mapping values are not allowed in this context", which suggested that the indentation was wrong in the YAML. I was able to resolve (through trial and error) with the following formatting:
---
title: "TITLE"
author: "Mike Harris"
date: "4/9/2021"
resource_files:
- template.pptx
output:
powerpoint_presentation:
reference_doc: template.pptx
---
Hopefully this saves someone a little extra time.

Use additional Latex packages for math expressions in RMarkdown `output = "html_document"`

I`m aware how to use additional Latex-packages for pdf-format output from .Rmd files using
---
header-includes:
- \usepackage{mathtools}
---
in the YAML header.
However, this does (of course) not work if one specifies output: html_document.
---
output: html_document
header-includes:
- \usepackage{mathtools}
---
Using additional Latex-Packages could be of interest for output: html_document, too - especially in math expressions (MWE below)
---
title: "MWE"
output: html_document
header-includes:
- \usepackage{mathtools}
---
## Use "Defined by" Symbol
$$sin(x) \coloneqq \frac{opposite}{hypothenuse}$$
MathJax offer a number of extensions, and there are third-party extensions as well. If your desired package is not available in this way, then things get difficult.
Simple commands, such as \coloneqq, can be recreated using \newcommand. The simplest way is to add these via the include-before option. Using your MWE with a solution from Mathematics Meta SE, one gets:
---
title: "MWE"
output:
html_document: default
include-before:
- '$\newcommand{\coloneqq}{\mathrel{=}}$'
---
## Use "Defined by" Symbol
$$sin(x) \coloneqq \frac{opposite}{hypothenuse}$$
Output:
Background
RMarkdown is build around pandoc, which performs most of the format conversions. Pandoc creates PDF via LaTeX (by default), and will simply include any raw LaTeX commands which are given in the source. When seeing \usepackage{mathtools}, the package is not parsed, but the command is simply added verbatim to the intermediate LaTeX. However, when exporting to HTML, it wouldn't make sense to pass through LaTeX commands, so any such command will simply be omitted from the output, so any \usepackage in your document won't effect the HTML output.
Alternative solution
If you are using very complex LaTeX-packages, then you could consider setting up a complex pipeline to still use it: E.g, one could use a pandoc filter to extract all equations, compile each equation as a separate document, and then convert the resulting PDF to SVG. Finally, that SVG can then be included in the HTML output. This is non-trivial and probably not worth the effort. A similar approach is recommended to include TikZ pictures.