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
---
Related
I've been using vanilla latex documents for a long time and have recently switched over to Rmarkdown. I'm struggling to get varioref to work, or any in-document referencing for that matter. See below example Rmarkdown file contents. I've tried to make it a minimally reproducible example.
---
title: "vref"
output:
pdf_document:
keep_tex: true
latex_engine: pdflatex
header-includes: \usepackage{varioref}
---
Please see \vref{section}
\newpage
\section{This is a section} \label{section}
It seems like the reference is being created with a clickable link, but there is no reference to the section being created..
Rmarkdown producing unnumbered sections has nothing to do with varioref. You can make them numbered like this:
---
title: "vref"
output:
pdf_document:
keep_tex: true
latex_engine: pdflatex
header-includes:
- \usepackage{varioref}
- \setcounter{secnumdepth}{4}
---
Please see \vref{section}
\newpage
\section{This is a section} \label{section}
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)
Is there a way to include an entire bibliographic entry in the main text of my Rmarkdown document? If I were using LaTeX, I'd use \fullcite{Smith2000}. And then it would print, wherever I wanted:
Smith, J. 2000. A nice book. Oxford: Oxford University Press.
Here's a sample .Rmd file
---
title: "hi"
author: "dmt"
date: "30/04/2020"
output: html_document
bibliography: bib.bib
biblio-style: apalike
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
```{r cars}
summary(cars)
```
Here is the citation of a book I am reading:
#Smith2000
But I would like to put the whole reference here like this:
Smith, J. 2000. A nice book. Oxford: Oxford University Press.
How can I do this without writing it out?
And here's a sample .bib file:
#book{Smith2000,
Address = {Oxford},
Author = {Smith, J},
Date-Added = {2020-04-30 17:03:32 +0100},
Date-Modified = {2020-04-30 17:04:03 +0100},
Publisher = {Oxford University Press},
Title = {A nice book},
Year = {2000}}
This works fine for me:
Execute this chunk in your RMD file:
```{r, echo=FALSE}
biblio <- bibtex::read.bib("bib.bib")
```
Then you can use the following line in your script:
`r capture.output(print(biblio["Smith2000"]))`
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.
Below is an rmarkdown document with a LaTex equation. The default for these equations is centre. How do I left-align this equation? I have searched the similar answers but none of the suggestions work on this simple problem.
---
title: "Untitled"
author: "Llew Mills"
date: "1 May 2016"
output: pdf_document
---
## R Markdown
\begingroup\Large
\begin{equation*}
\Delta PsuedoR^2_{\zeta_{0}} = \frac {0.488 - 0.624}{0.488} = -0.219
\end{equation*}
\endgroup
This latex solution on tex.SE gives an answer that we can adapt to knitr. To have all equation of the document aligned on the left, you can use classoption fleqn and write equation in regular pandoc latex-math style (surrounded with $$):
---
classoption: fleqn
header-includes:
- \setlength{\mathindent}{0pt}
---
$$\Delta PsuedoR^2_{\zeta_{0}} = \frac {0.488 - 0.624}{0.488} = -0.219$$
Note that you don't have to add \usepackage{amsmath} because the default latex template already includes it.