I was reading here that $$ is deprecated in LaTeX and replaced with \[ and \]. It seems the opposite when I use R Markdown in R Studio though.
If I wrap an equation in $$ it will display block style, live preview, in my R Markdown (in R Studio). If I use \[ and \] it will still knitr fine, but it won't showup R Studio live preview. See below.
---
title: "Untitled"
author: "March 2019"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
\[x = R + F\]
$$x = R + E$$
The linked Q&A is correct. $$ is deprecated for LaTeX. However, you are not writing LaTeX but Rmarkdown, which is processed by pandoc. In the pandoc manual we find:
TeX math will be printed in all output formats. How it is rendered depends on the output format:
LaTeX
It will appear verbatim surrounded by \(...\) (for inline math) or \[...\] (for display math).
So you can use $$...$$ in Rmarkdown documents and still have the correct output when converting to PDF via LaTeX. The other form works due to another pandoc extension. If you need cross-references for equations, you should use bookdown, though.
Related
I have an R markdown file with several custom latex commands, defined via \newcommand. I really like the preview in RStudio, but it doesn't seem to be able to render these commands or those imported from packages. In the following example, neither the custom command \expect nor the package command \bm is shown in the preview. Instead, they are shown in red letters in the preview.
Is there any way to have them rendered in the preview?
---
header-includes:
- \usepackage{bm}
output:
pdf_document
---
\newcommand{\expect}[1]{\mathbb{E}\left[ #1 \right]}
$$
\expect{\bm{X}} = \lambda
$$
I think the question is quite self-explanatory but for avoidance of doubt I'll explain with more detail below:
I have an R Markdown document that works well if converted to HTML or uploaded to GitHub. When converting to PDF (using Latex), the results are not so pretty. I find that the biggest problem in a Latex PDF document are line breaks. I can fix the line breaks issue on the PDF document by adding "\ " characters, but that throws my HTML document out of whack too.
Is there a way to manually add line breaks (or "space before/after paragraphs") for the PDF output only?
Thank you!
You can redefine the relevant spacings in the YAML header. \parskip controls the paragraph spacing. Code blocks are shaded using a snugshade environment from the framed package. We can also redefine the shaded environment for code blocks to have some vertical space at the start. Here's a reproducible example. Note: I also added the keep_tex parameter so you can see exactly what the generated tex file looks like, in case this is useful:
title: "test"
author: "A.N. Other"
header-includes:
- \setlength{\parskip}{\baselineskip}
- \renewenvironment{Shaded}{\vspace{\parskip}\begin{snugshade}}{\end{snugshade}}
output:
pdf_document:
keep_tex: true
---
```{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.
Once you output to HTML, you can just print the HTML webpage as PDF. that might be an easy way keep the original format
Trying to knit a doc into html, RStudio would hang on the conversion to html part. I looked around and it looked like this happened to other folks when they were using an out of date pandoc version. I updated pandoc to 2.8. I received the error in the title. Looking around again, it appeared that v 2.5 seemed stable. I installed that, restarted my computer, and still get the same error.
repex is the default example when you create an RMarkdown file in RStudio: file -> New file -> R Markdown -> html.
---
title: "repexpandocerror23"
author: "Adam Korejwa"
date: "11/22/2019"
output: html_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.
I have fixed this issue two months ago. Please make sure your rmarkdown version is at least v1.16. Once again, when in doubt, consider updating your packages:
update.packages(ask = FALSE, checkBuilt = TRUE)
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'm getting started with rmarkdown and knitr. In the sample document provided by RStudio, I can successfully use render() to generate an HTML file that views fine in Chrome. However, when I click on the knit button, it generates a .markdown file and then returns the following error without rendering the preview:
Error generating HTML preview for ~/path/to/file/report.rmarkdown system error 2 (The system cannot find the file specified)
I think it's getting hung up at the pandoc stage. Is it possible that RStudio is looking for pandoc in the wrong place? Pandoc was already installed at C:\Program Files (x86)\Pandoc\pandoc.exe, but RStudio installed its own instance at C:\Program Files\RStudio\bin\pandoc\pandoc.exe, so maybe it's looking in the wrong place and/or confusing settings from one with the other?
Any help would be greatly appreciated. Thanks!
And just in case, here's the RMarkdown template I'm starting with:
---
title: "Monthly Report"
author: "Kris Shaffer"
date: "February 17, 2017"
output: html_document
---
```{r setup, include=FALSE}
library(knitr)
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.
R version 3.3.2
RStudio 1.0.136
rmarkdown 1.3
knitr 1.15.1
pandoc 1.17.2 (both installations)
Turns out it was as simple as a file extension. I changed the filename from report.rmarkdown to report.Rmd. Knit works fine now.