I am using the rmarkdown package to produce PDF slides with the Metropolis theme. Recently, I noticed that the equations started to appear differently - they use a different font.
Minimal example of *.Rmd file:
---
output:
beamer_presentation:
theme: "metropolis"
latex_engine: xelatex
keep_tex: true
---
## Problem with font
$$f(x_i\mid\mu,\sigma^2) = \exp\left\{-\frac{(x_i-\mu)^2}{2\sigma^2}\right\}$$
which, when knitted in RStudio, produces:
This looks different from what is obtained when compiling the same slide directly in LaTeX with xelatex:
\documentclass{beamer}
\usetheme{metropolis}
\begin{document}
\begin{frame}{Problem with font}
\[f(x_i\mid\mu,\sigma^2) = \exp\left\{-\frac{(x_i-\mu)^2}{2\sigma^2}\right\}\]
\end{frame}
\end{document}
which produces:
This does not look like a big difference, but in other equations some special characters are missing, and the font size is slightly different, affecting the whole layout of my slides.
After some investigation, it turns out that commenting out those two lines in the tex file produced by rmarkdown makes it better:
%\usepackage{unicode-math}
%\defaultfontfeatures{Ligatures=TeX,Scale=MatchLowercase}
Is the unicode-math package the (only) culprit here? How to solve this problem and make sure that Metropolis uses the right fonts without manually changing the tex files?
Thanks in advance for your help!
System configuration:
RStudio 1.2.1335
rmarkdown 1.15
pandoc 2.3.1 (through RStudio)
Metropolis theme 1.2
Fira Sans font 4.3
XeTeX 3.14159265-2.6-0.999991 (TeX Live 2019)
What you are seeing is, indeed, the effect of the unicode-math package. There is a simple way to work around this by forcing pandoc to use the mathspec package instead. This can be done by setting mathspec: true in your metadata
---
mathspec: true
output: …
---
or by setting the respective variable when calling pandoc
---
output:
beamer_presentation:
theme: "metropolis"
latex_engine: xelatex
pandoc_args: ["--variable=mathspec"]
---
There is only a minor, very subtle, and mostly inconsequential difference between the two. I'd suggest to use the first version, as it is simpler.
See TeX StackExchange for a discussion of the differences between unicode-math and mathspec.
Related
I want to use tikz as graphics device in RMarkdown and I want it to include the generated LaTeX preamble.
In the past, I already used tikzDevice within knitr documents. The tex file generated by tikzDevice usually included the whole preamble from my knitr/LaTeX document. When I use it with RMarkdown, I get the standard preamble (see below).
RMarkdown file:
---
title: "Title"
author: "Me"
fontsize: 12pt
documentclass: scrartcl
output:
bookdown::pdf_document2:
toc: true
fig_caption: true
keep_tex: true
---
# Introduction
```{r plot, dev="tikz"}
plot(rnorm(50))
``
Beginning of generated tex file (plot-1.tex):
% Created by tikzDevice version 0.12.3 on 2019-06-16 16:09:40
% !TEX encoding = UTF-8 Unicode
\documentclass[10pt]{article}
Desired/expected beginning of plot-1.tex:
% Created by tikzDevice version 0.12.3 on 2019-06-16 16:09:40
% !TEX encoding = UTF-8 Unicode
\documentclass[12pt]{scrartcl}
I'm not sure you really want what you're asking for. The figure will be produced as a separate document containing nothing except the figure, which will be rendered as a PDF. The differences between scrartcl and article shouldn't matter for the figure, they matter for the document as a whole.
But if you really do need that document class, you get it by specifying options(tikzDocumentDeclaration = "\\documentclass[12pt]{scrartcl}") in an R chunk early in your document. When I do that I can see in the source that it worked, but the output looks pretty much the same as it did with the default class. It's also possible to specify this using chunk options, but there's unlikely to be any advantage to doing that.
I think I figured it out:
My problem was that while using RMarkdown the options tikzDocumentDeclaration, tikzLatexPackages ... (nearly all options for tikzDevice) were not set automatically. When you use knitr the options for tikzDevice get set up in the process of splitting up markup and code chunks from the source file. With RMarkdown there is no LaTeX code to extract and use with tikz because pandoc generates it after the graphic is rendered. So one can either define the tikz... options manually or use the chunk option external=FALSE like user2554330 suggested.
Example minimal_knitr.Rnw:
\documentclass[fontsize=12pt]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\begin{document}
<<r plot, dev='tikz', echo=FALSE>>=
plot(rnorm(50))
#
\end{document}
I'd like to include LaTeX environments (e.g., algorithmic from algorithmicx, mini from optidef, dcases from mathtools, etc.) in my .Rmd bookdown file. This is no problem for pdf output. But these do not render for html or docx output.
My current hack solution:
Generate the .pdf output.
Screen shot, edit, save images of interest as png
Include images conditional on output not being LaTeX
Downsides:
Obviously doesn't scale
Images are ugly in docx and html output
Screws with figure cross-referencing
There has to be a better approach, right? I was thinking that there's a way to tell rmarkdown/LaTeX that, when rendering as pdf, certain code chunks should be saved in some image format. That way they could be added back into the document as images conditional on the output document being docx or html. Is that even possible?
UPDATE: An answer to Standalone diagrams with TikZ suggests an approach involving the LaTeX standalone package. But unfortunately, it's discovered over at standalone does not work with algorithms that this does not work for the algorithm environment. Any ideas?
index.Rmd
---
title: "Bookdown"
header-includes:
- \usepackage{float}
- \floatplacement{figure}{!htb}
- \usepackage{algorithm}
- \usepackage{algpseudocode}
output:
bookdown::gitbook:
split_by: none
bookdown::pdf_book:
fig_caption: yes
keep_tex: yes
toc: no
bookdown::word_document2: default
site: bookdown::bookdown_site
---
```{r setup, include=FALSE, }
knitr::opts_chunk$set(echo = TRUE)
```
Hello zero
# First Chapter
Hello one
\begin{algorithm}
\caption{My Algo}
\begin{algorithmic}[1]
\State Do this.
\State Do that.
\end{algorithmic}
\end{algorithm}
```{r myalgo, echo=FALSE, eval = !knitr:::is_latex_output(), fig.cap="Must have text here. For cross-referencing to work."}
knitr::include_graphics("myalgo.png")
```
Hello two.
Check out this picture: \#ref(fig:myalgo)
myalgo.png
For math, R Markdown uses MathJax, and only a subset of LaTeX is available. This subset includes the basic math macros and environments, and allows you to define new macros, but doesn't support everything necessary to let you use arbitrary LaTeX packages. See http://docs.mathjax.org/en/latest/tex.html for details.
You might be able to create an environment that looks something like algorithm or algorithmic, but it's going to be a lot of work, and likely won't look as nice.
You should probably choose between PDF output with all of LaTeX available for formatting, or some flavour of HTML output with less style. For example, you could write your algorithm as
******
**Algorithm 1**: My algo
******
1. Do this.
2. Do that.
******
and it will display as
Algorithm 1: My algo
Do this.
Do that.
I have taken the plunge and am preparing a manuscript for publication entirely in RStudio using bookdown. In the main text I would like to cross-reference figures in a separate supporting information .Rmd file.
Let's say this is my main text file called main.Rmd:
---
title: "Main text"
output:
bookdown::pdf_book:
toc: no
---
Here is the main text file. I would like to refer to \#ref(fig:supporting-figure).
Here is the supporting text called supporting.Rmd, and the figure to refer to, saved in the same folder:
---
title: "Supporting info"
output:
bookdown::pdf_book:
toc: no
---
Here is the supporting text.
```{r supporting-figure}
plot(cars)
```
How can I cross-ref supporting-figure in the main text?
I have checked the section on cross-references in Yihui's bookdown manual, but I cannot see how to extend this to cross-references between files.
I also found this question:
Cross-reference figure in a separate Rmarkdown (PDF) file
but the accepted answer does not work for me (perhaps because I am using bookdown rather than base Rmarkdown?)
I am not entirely sure how you are compiling these two files into a single bookdown document, because as it stands they are just two separate R Markdown documents. But there are two issues:
Figures need a caption to be cross-referenced
You can only cross-reference figures which have a caption assigned with fig.cap, as explained here:
If we assign a figure caption to a code chunk via the chunk option
fig.cap, R plots will be put into figure environments, which will be
automatically labeled and numbered, and can also be cross-referenced.
Incorrectly configured bookdown project:
From what I can tell, you have not got the project configured correctly for bookdown:
The main file should be called index.Rmd
Supporting files should not have any YAML
You should include site: bookdown::bookdown_site in the YAML of the main documene
Check out this answer for some tips on a minimal bookdown file.
Solution
index.Rmd
---
title: "Main text"
site: bookdown::bookdown_site
output:
bookdown::pdf_book:
toc: no
---
Here is the main text file. I would like to refer to \#ref(fig:supporting-figure).
supporting.Rmd
Here is the supporting text.
```{r supporting-figure, fig.cap= "Some Figure"}
plot(cars)
```
I ran into the same issue and came up with this solution if you aim at compiling 2 different pdfs. It relies on LaTeX's xr package for cross references: https://stackoverflow.com/a/52532269/576684
Here is simple RMarkdown document with two sections and two images.
---
output:
bookdown::html_document2: default
bookdown::word_document2: default
bookdown::pdf_document2: default
---
\newpage
# Part 1
Part 1 starts here. See Figure \#ref(fig:fig1-1)
![(\#fig:fig1-1) expected to be Figure 1.1.](/usr/lib/rstudio/www/images/rstudio.png)
# Part 2
Part 2 starts here. See Figure \#ref(fig:fig2-1)
![(\#fig:fig2-1) expected to be Figure 2.1.](/usr/lib/rstudio/www/images/rstudio.png)
I expect that two images will render with Knit to the following numbering - first is Figure 1.1, second is Figure 2.1. But I get this rendering only in html_document2 (see image below):
I use latest RStudio 1.1.414 with latest bookdown from Git (38efc82).
I have two questions here:
Why I don't have Figure 1.1 and Figure 2.1 in Word or PDF?
How can I get Figure 1.1 and Figure 2.1 in Word or PDF?
I can only address PDF output via LaTeX. By default the LaTeX documentclass article is used which uses continuous figure numbering. If your document is so long that it makes sense to number the figures within each top-level section, then you might want to use report or book instead, e.g.:
---
documentclass: book
output:
bookdown::pdf_document2: default
bookdown::word_document2: default
bookdown::html_document2: default
---
Alternatively you can use some LaTeX package to change the formatting of figure numbers, e.g.:
---
header-includes:
\usepackage{chngcntr}
\counterwithin{figure}{section}
output:
bookdown::pdf_document2: default
bookdown::word_document2: default
bookdown::html_document2: default
---
An alternative would be the \numberwithin command from amsmath in case you are using that package anyway.
I am writing a report in R markdown (using pdflatex to convert it into pdf) and I am trying to set the style of the different headers (color, size, interline before and after).
I am using this formalisation :
#H1
##H2
###H3
I have the basics of Markdown and LaTeX but I'm far to be an expert. Is there a way to do it without creating a LaTeX template ? For exemple with some YAML code in the head of the document ?
Mine for now :
---
title: "Guide"
output:
pdf_document:
fig_caption: yes
highlight: tango
toc: yes
toc_depth: 2
documentclass: report
---
Or by changing the R options ?
```{r set-options, echo=FALSE, cache=FALSE}
options(some stuff)
```
I've found some indications while searching that suggest it may be possible, but I could'nt manage to find any understandable exemple ...
Thanks for any feedback.
If you do not want to do any LaTeX, you could add your own CSS (see documentation) and set it that way, as in this answer.
You would then need to do: How to convert Markdown + CSS -> PDF?
However, your results may not be as faithfully rendered as if you create a LaTeX template.