Does R Markdown have a similar construct to LaTex's "newcommand"? I would like to be able to define things like \var to be \mathrm{Var} to avoid the extra typing in math mode. If not, what do people do to reduce repetition in typesetting equations in markdown?
Use \newcommand{\var}{\mathrm{Var}} exactly like you would in LaTeX:
---
title: "Untitled"
author: "An Author"
date: "January 15, 2017"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
\newcommand{\var}{\mathrm{Var}}
## R Markdown
This is an R Markdown document. $\var+2$ 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>.
Note that in order for it to be processed correctly in the output, you'll have to use $...$.
I'm using bookdown and need to have something that works consistently across pdf, html, and docx output. None of the above solutions worked for my case. Here is the hack I settled on:
preamble.tex
\usepackage{amsthm}
\DeclareMathOperator*{\argmin}{argmin}
\newcommand{\var}{\mathrm{Var}}
YAML Header:
---
title: "A Minimal Book Example"
author: "Yihui Xie"
date: "`r Sys.Date()`"
site: bookdown::bookdown_site
output:
bookdown::pdf_book:
includes:
in_header: preamble.tex
toc: no
bookdown::word_document2:
reference_docx: template.docx
bookdown::gitbook:
split_by: none
documentclass: article
bibliography: [book.bib, packages.bib]
biblio-style: apalike
link-citations: yes
---
<!--- For HTML Only --->
`r if (!knitr:::is_latex_output()) '
$\\DeclareMathOperator*{\\argmin}{argmin}$
$\\newcommand{\\var}{\\mathrm{Var}}$
'`
<!--- For DOCX Only --->
`r if (!knitr:::is_latex_output() & !knitr:::is_html_output()) '
\\DeclareMathOperator*{\\argmin}{argmin}
\\newcommand{\\var}{\\mathrm{Var}}
'`
# Prerequisites
This is a _sample_ book written in **Markdown**.
To get around the requirement of \DeclareMathOperator needing to be in the preamble, use \operatorname:
\newcommand{\Var}{\operatorname{Var}}
$\Var(X)$
(\operatorname handles spacing better than \mathrm)
To use \newcommand properly in HTML output, your LaTeX should be in-line with single $ or in double $$. This applies to environments like \begin{align*} too.
---
title: "Test"
author: "qwr"
date: "January 22, 2019"
output: html_document
---
\newcommand{\Var}{\operatorname{Var}}
$\Var(X)$
$$
\begin{align*}
\Var[Y] &= x \\
&= 3
\end{align*}
$$
I had issues with the above solution when outputting as a beamer presentation, particularly when using the equation mode ($$.$$ rather than $.$). Putting the new commands in a separate file fixed the issue for me.
---
title: Title
author: Author
date: "8/22/2018"
output:
beamer_presentation:
includes:
in_header: preamble.tex
---
Where preamble.tex contains your user defined command(s)
\newcommand{\var}{\mathrm{Var}}
Then you can use the command both inline ($\var$) and in equation mode ($$\var$$)
You can also put other stuff in preamble.tex like frame numbering, etc.
Related
I use Rmarkdown to write python code and knit to pdf file for some reason...
I want to use and display greek characters like lambda in my code.
e.g.
def function(X,Y,λ,h):
# do stuff
return w
With default engine, it shows error message as below:
! LaTeX Error: Unicode character λ (U+03BB)
not set up for use with LaTeX.
Error: LaTeX failed to compile report.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See report.log for more info.
Execution halted
If I change the latex engine to xelatex or lualatex, it can knit to a pdf but the greek character disappear. Like this:
def function(X,Y, ,h):
# do stuff
return w
Do folks have any idea how to address that? Really appreciate it if someone could help.
Edited: add a minimal reproducible example as asked by the comment.
---
title: "Minimal reproducible example"
date: "2022-11-08"
output:
pdf_document:
latex_engine: xelatex
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{python}
def function(X,Y,λ,h):
# do stuff
return w
```
You'll need to choose a font which does have Greek letters. For example with Arial (using Arial here because many people have it installed, choose a nicer looking font if you have it):
---
title: "Minimal reproducible example"
date: "2022-11-08"
output:
pdf_document:
latex_engine: xelatex
keep_tex: true
header-includes:
- \setmonofont{Arial}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{python}
def function(X,Y,λ,h):
# do stuff
return w
```
I am trying to use Rmarkdown to automatically label Figure's caption, but instead of counting Figure 1, I want it to be Figure S1, i.e. just adding S there. An example here and here suggests that this is not possible using pdf output. Ok, I am fine with .doc file, but still my figure caption is not print out? I wonder what could be wrong?
Minimal example for R markdown:
---
title: Supporting Information
subtitle: "Iron(I) etc"
author: "Some people here"
abstract: "Added the addresses here since there is no abstract in the SI"
output:
word_document:
fig_caption: yes
---
```{r, include=F}
library(captioner)
figS<- captioner(prefix = "Figure S", auto_space = TRUE, levels = 1, type = NULL, infix = ".")
figS("Figure S1", "Single-crystal X-ray structure of some text (1)", display=FALSE)
```
```{r Xray, fig.cap=figS("This is my figure description"), echo=FALSE}
plot(cars)
```
This prints correctly out Figure S1. But, now my actual figure description is missing?
My desired output is pdf, but if not, I can do with word. Thanks for suggestion how to fix this!
You can use some LaTeX commands under header-includes: in the YAML section, as follows:
---
title: "Untitled"
output: pdf_document
header-includes:
- \renewcommand{\figurename}{Figure S}
- \makeatletter
- \def\fnum#figure{\figurename\thefigure}
- \makeatother
---
```{r, fig.cap="This is my figure description", fig.height=3}
plot(pressure)
```
```{r, fig.cap="Another figure description", fig.height=3}
plot(iris$Sepal.Length, iris$Petal.Width)
```
(The argument fig.height in the R code chunks is not necessary; I used it only to get both plots in the same page and take the screenshot)
Instead of Chapter X when creating a PDF from bookdown, I would like it to be "Módulo X" (in Spanish).
So I would like to know how to change chapter name using bookdown.
My YAML is:
---
title: "TITLE"
author: "Mario Modesto-Mata"
date: "`r Sys.Date()`"
output: pdf_document
description: This is a minimal example of using the bookdown package to write a book.
The output format for this example is bookdown::gitbook.
documentclass: book
link-citations: yes
bibliography: book.bib
site: bookdown::bookdown_site
language:
label:
chapter_name: "Módulo"
---
I tried with the last three line codes, with no success. Any idea?
From the bookdown documentation we can learn two things:
There is no language.label.chapter_name but language.ui.chapter_name.
This setting is meant for HTML output. For PDF output one should configure LaTeX.
Configuring LaTeX is quite simple. You only need to add lang: es to your header.
However, this will use "Capítulo" instead of "Módulo". One can adjust this by redefining the LaTeX command \chaptername. BTW, at the moment you are not using bookdown but the standard pdf_docuemnt from rmarkdown. If you wont to use bookdown features, you should use bookdown::pdf_book or bookdown::pdf_document2.
Putting everything together:
---
title: "TITLE"
author: "Mario Modesto-Mata"
date: "`r Sys.Date()`"
output: bookdown::pdf_book
description: This is a minimal example of using the bookdown package to write a book.
The output format for this example is bookdown::gitbook.
documentclass: book
lang: es
link-citations: yes
bibliography: book.bib
site: bookdown::bookdown_site
header-includes:
- \AtBeginDocument{\renewcommand{\chaptername}{Módulo}}
---
Result:
Note that header-includes is nice for simple stuff in single file documents like this minimal example. In most cases one is better off including a tex into the header via output.<your-format>.includes.in_header, c.f. Include TeX header in R package for RMarkdown documents.
I would like to make the following work
---
title: "Untitled"
author: "SQC"
date: "21 September 2018"
output: html_document
---
\newcommand{\short}{AreallylongwordIhavetotypefrequently}
# My Test
I would like to write \short which does not work, $\short$ however is close...
Snippets do not work in Rmd plain text (= Rstudio's "Shift", see link below).
But I could not find a solution. It would be great if there is something around!
The following links are helpful, but didn't suggest a solution: pandoc doc, \newcommand in Rmd formula and RStudio snippets.
How about using R instead:
---
title: "Untitled"
author: "SQC"
date: "21 September 2018"
output: html_document
---
```{r, include = FALSE}
short <- "AreallylongwordIhavetotypefrequently"
```
# My Test
I would like to write `r short` instead ...
If you still need to define a \newcommand using LaTeX in Rmarkdown, you can do so by e.g.
Some text with the following equation:
\begin{equation} \newcommand{\matr}[1]{\mathbf{#1}}
\matr{Y} =
\begin{pmatrix}
\matr{y_1} \\
\matr{y_2} \\
\end{pmatrix}
\end{equation}
```{r}
y1 + y2
```
Note, the bm package must be loaded for this to work, e.g., by using this in the YAML header:
---
title: "My R Report"
author: "Me"
date: "2020-01-31"
header-includes:
- \usepackage{bm}
output:
pdf_document:
toc: true
number_sections: true
---
How can I show page numbers (preferably like 4/10, 5/10 etc.) on an rmarkdown beamer presentation?
In the front matter of the document, you can include a .tex file with extra commands as shown in RStudio's documentation here.
I created a new .tex file that I called "header_pagenrs.tex" which only includes the top 2 lines from #user4281727's answer:
\setbeamertemplate{navigation symbols}{}
\setbeamertemplate{footline}[page number]
Then I referenced this file at the top of my .Rmd file:
---
title: "Things and Stuff"
author: "Me"
output:
beamer_presentation:
includes:
in_header: header_pagenrs.tex
---
If this still gives you errors, you might also be missing some required TeX packages (a separate problem from RStudio and rmarkdown).
Here's another option that worked for me. Didn't need to add a .tex file to my folder. Just included the following (based on above code from #civilstat) at the top of my Markdown doc.
---
title: 'Your Title'
author: "Your Name"
date: "July 4, 1776"
output:
beamer_presentation(keep_tex = TRUE): default
header-includes:
- \setbeamertemplate{navigation symbols}{}
- \setbeamertemplate{footline}[page number]
---
If your beamer version is reasonable up to date (>= v3.48), you can adjust the format of the frame numbers while keeping the footline of your chosen beamer theme unchanged otherwise.
---
output:
beamer_presentation:
theme: "Berlin"
keep_tex: true
header-includes:
- \setbeamertemplate{page number in head/foot}[totalframenumber]
---
test
Try to put the lines below into the template
\setbeamertemplate{navigation symbols}{}
\setbeamertemplate{footline}[page number]
~/Library/R/3.1/library/rmarkdown/rmd/beamer/default.tex