Equation Numbering in Rmarkdown - For Export to Word - r

When kniting a Rmarkdown file to MS Word, is there a way to have equations labled with a number that is flushed to the right?
For example:
When typing this in Rmarkdown:
$$a + b = c$$
I need it to end up in Word as:
a + b = c (1)
I saw the code below somewhere else, but it does not seem to work when kniting to Word...
\begin{equation}
\label{eq-abc}
a + b = c
\end{equation}
Thanks!

#Sholom's comment regarding pandoc-crossref inspired me to have a read, I was not previously aware of it and it is not available as an extension to rmarkdown currently.
It is entirely possible to achieve basic equation numbering with this "filter".
Markdown
---
output:
word_document:
pandoc_args: ["-Fpandoc-crossref"]
---
$$a^2 + b^2 = c^2$$ {#eq:eqn1}
$$\log xy = \log x + \log y$$ {#eq:eqn2}
$$\frac{df}{dt} = \lim_{h\to0}\frac{f(t+h)-f(t)}{h}$$ {#eq:eqn3}
Output
pandoc-crossref
The binary releases of the code are located here. From a windows machine, I dropped the pandoc-crossref.exe into my Pandoc install folder alongside pandoc.exe and that was sufficient to allow my markdown to render.
N.B. I did this with a standalone install of Pandoc, if you are using the version bundled with RStudio you may need to install differently.

Related

Is $$ deprecated in R Markdown LaTeX?

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.

Unable to make sense out of "Rstudio pdf knit fails with 'Environment Shaded undefined' error" [duplicate]

This question already has answers here:
Rstudio pdf knit fails with "Environment Shaded undefined" error
(5 answers)
Closed 4 years ago.
I am creating a report using the rticles package and I get the following error when knitting an rticle IEEE paper which includes code-chunks:
output file: rticles_IEEE.knit.md
! LaTeX Error: Environment Shaded undefined.
I tried to understand how to fix the issue and played around with this question/answer. But I fail to see where to put the fix into the files created by knitting (e.g. mydoc.tex, IEEE_trans.cls).
Here is my minimal example:
---
title: Cool IEEE ArticleS
author:
- name: Ray
abstract: |
The abstract of my first IEEE paper with rmarkdown
output: rticles::ieee_article
---
Introduction
=============
This is the introduction. And here comes a cool code chunk.
```{r}
x <- seq(-3,3,0.1)
y <- x^3
plot(x,y)
```
Notes:
It was commented that this is a potential duplicate of another question. The problem with the answers given in that thread is that it does not explain where to make the changes.
I fail to understand the pointer to Yihui's fix.
The aforementioned files, but also the knitted .tex does not come with labels of the form \makeatother or \makeatletter.
The overall issue was that displaying a code chunk in the rticles IEEE paper throws an error pointing at the undefined Shaded environment.
The solution given in the older post works, if you add the following to the underlying LaTeX template.tex. My problem was that I tried to edit the tex or cls template in the working directory. The Eureka-moment came when I stumbled over the following (related) stackoverflow question.
For the rticles package, the template.tex is not located in your directory you work on the article R Markdown file. Instead, they are stored within the package library folder (i.e. the folder that .libPaths() uses). The file can be found in rmarkdown/templates/ and search for the rticles folder you are using (in my case: ieee_article). In the resources sub-folder you will find the target template.tex. Make a copy of it, for recovery purposes.
Open the template.tex and insert the following anywhere before the LATEX command line \begin{document} (note: aka the preamble of that template.tex file). For example, I inserted it just before the \begin{document} line:
% code to insert to fix environment Shaded undefined issue with
% showing code chunks in rticle IEEE template.
$if(highlighting-macros)$
$highlighting-macros$
$endif$
\begin{document}
Save the change and go back to R/RStudio and your IEEE paper R Markdown file and hit the knit button. R code chunks will now be displayed (if you want to).
I have not tested it thoroughly, but this recipe should work for all rticle templates missing an environment definition for showing r-code chunks (i.e. environment{Shaded})
Rather than duplicating the template, you can put the required code for allowing the highlighting in a separate header.tex file. This is essentially what replaces $highlighting-macros$ when you include this in the template:
header.tex
\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}
Make a copy of this file in your directory and then refer to this header in the YAML:
---
title: Short Paper
output:
rticles::ieee_article:
includes:
in_header: header.tex
---
# Introduction
This is the introduction. And here comes a cool code chunk.
```{r}
x <- seq(-3,3,0.1)
y <- x^3
plot(x,y)
```
This approach should work for any template which includes the $header-includes$ tag in thetemplate.tex`

R code inside math notation R Markdown

In R Markdown, I can get an inline r code chunk to run inside single $ math notation (only with a * before), but not double $$ math notation:
* $H_o = `r 1 + 1`$
works, but:
$H_o = `r 1 + 1`$
doesn't work, and neither does:
$$H_o = `r 1 + 1`$$
The double $$ is more flexible being able to put math notation on multiple lines, but how can you add inline code chunks inside?
I ran your example and it worked fine for me! Here is a link to the pdf and github .md documents I generated. And here is a link to the rmarkdown document.
I'm using R 3.4.1, knitr 1.16, and rmarkdown 1.6.
Perhaps you should check your version of rmarkdown and knitr. I have noticed over the years that version updates, and sometimes even using the most recent dev version, can fix a litany of small issues that arise.

"Erroneous nesting of equation structures" in using "\begin{align}" in a multi-line equation in rmarkdown to knit+pandoc pdf

I am writing some multi-line equations in R Markdown - LaTeX, using auto-numbering and \begin{align}. Here's a working the example:
---
title: "test"
output: html_document
---
(#eq01) $$
\begin{align}
y = x^2 \\
y = x^3 \\
y = \sqrt[2]{x}
\end{align}
$$
This works great when the output is html_document. Here's the result:
But when I change the output document to pdf:
output: pdf_document
I get the following error (I am using RStudio latest Version 0.98.1056):
I've been trying to read the documentation as suggested in the error message, but I do not seem to get a handle on it. I've checked Stack Overflow and Google and although there are some related posts/questions (for example here, here, here), none of them solve the problem (or apply to my problem).
I've also tried to tweak everything. The most evident solution would be to get rid of the \begin{align} environment,
(#eq01) $$
y = x^2 \\
y = x^3 \\
y = \sqrt[2]{x}
$$
but it does not work for two reasons. First, the html version does not work as nicely because the auto-numbering does not appear centered in the multi-line equation, but rather on the first line (and I don't like it like that).
Second, although the pdf version in this case does compile and produce the pdf, it does not recognize that it is a multi-line equation (it's like it does not recognize the new line command \).
Any ideas are really appreciated. I've been struggling with this for a while and I cannot find a solution. I kinda love R Markdown because it really integrates the analysis with writing and communicating in a single tool (rather than using many different tools going back and forth). However, it seems there is still a long way to go before we can write one single source file and that it renders appropriately in several different output formats.
I was receiving the same error when trying to send an aligned block to PDF. Try changing the following:
$$
\begin{align}
y = x^2 \\
y = x^3 \\
y = \sqrt[2]{x}
\end{align}
$$
to the following:
$$
\begin{aligned}
y = x^2 \\
y = x^3 \\
y = \sqrt[2]{x}
\end{aligned}
$$
\begin{align} is a self-contained math environment, whereas \begin{aligned} needs to be placed inside an existing math environment. Since Rmd delineates math sections with $$...$$, it seems like \begin{align} was trying to start a second math environment within the first and causing problems.

problems plotting special characters (slovene) to knitr latex reports

I'm having trouble printing special characters (ščž) to a pdf report made by knitr.
\documentclass[a4paper, 12pt, oneside]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[slovene]{babel}
\begin{document}
<<>>=
plot(runif(100), main = "ŠČĆŽ ščćž")
#
\end{document}
Based on comments by Yihui (see also comments below), here's a solution that worked for me. The .Rnw document I'm working with is encoded as UTF-8.
The key is to specify encoding to knit.
Notice that we're still missing the "č" (for non-native speakers, you can read this character as "ch").
The issue can be resolved by specifying a different printing device. Consider specifying dev = "CairoPDF" (requires additional CairoPDF package) or dev = "cairo_pdf" (requires no additional packages) in your chunk option.
<<dev = "CairoPDF">>=
plot(runif(100), main = "ŠČĆŽ ščćž")
#
or
<<dev = "cairo_pdf">>=
plot(runif(100), main = "ŠČĆŽ ščćž")
#
If you're using Eclipse + StatET to weave your reports, you can set these options to automate the workflow.

Resources