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.
Related
I am trying to use Rmarkdown to create answer/solutions twin PDF documents. I embedded the solutions in a frame using the tcolorbox LaTeX package. Also, I am using LaTeX's comment package to easily toggle the solutions on and off. However, R chunks inside the comment environment are not well rendered. Here's a MWE:
---
title: "Untitled"
output:
pdf_document
header-includes:
- \usepackage{tcolorbox}
- \usepackage{comment}
- \includecomment{comment} % toggle include / exclude
- \newcommand{\benum}{\begin{enumerate}}
- \newcommand{\eenum}{\end{enumerate}}
---
```{r, include=FALSE}
library(tidyverse)
```
\benum
\item The R chunk inside the \verb+comment+ environment is not rendered correctly:
\begin{comment}
\tcolorbo
```{r, echo=TRUE, eval = TRUE, results='asis'}
a <- rnorm(20)
a
```
\endtcolorbox
\end{comment}
\item But the same chunk outside the \verb+comment+ environment is rendered fine:
\tcolorbox
```{r, echo=TRUE, eval = TRUE, results='asis'}
a <- rnorm(20)
a
```
\endtcolorbox
\eenum
Here's the result:
How can I achieve what I want?
Many thanks in advance!
I am writing my thesis in Rmarkdown and Latex with a seperate literature.bib file that contains the bibtex sources. I now have a footnote and I would like to have a citation like in the normal text, but within the footnote. Is there any package to achieve that? When i just try it, like I would have a citation in the normal text, the footnote stops beeing a footnote when i render to pdf. The citation style I use is the Harvard citation style, so only something like this appears and should appear in the footnote:
The rest of information is than provided under the heading References at the end of the document. Thanks in advance! I appreciate any help...!
Here a minimal reproducable example:
--> This is the code for the main .rmd file
---
output:
bookdown::pdf_document2:
toc: no
papersize: a4
geometry: margin = 1in
fontsize: 11pt
bibliography: literatur.bib
---
#TEST
This is just an example #test.\footnote{I would also like to have a citation here in this footnote!!}
\newpage
# References
<div id="refs"></div>
-->This is the code for the literatur.bib file
#online{test,
author = {PACER},
title = {Service Center},
url = {https://pcl.uscourts.gov},
urldate = {2021-09-10}
}
Hope that is helpful?!
If you use markdown syntax for the footnote, you can also insert the citation via markdown syntax:
---
output:
bookdown::pdf_document2:
toc: no
papersize: a4
geometry: margin = 1in
fontsize: 11pt
bibliography: literatur.bib
---
#TEST
This is just an example #test.^[I would also like to have a citation here in this footnote!! See [#test]]
\newpage
# References
<div id="refs"></div>
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.
I'm building a presentation using rmarkdown and LaTeX/Beamer. I would like to reduce the spacing between the displayed R-commands and R-output. I believe this is related to the paragraph spacing options in LaTeX/Beamer.
Is this something I should do in rmarkdown (chunk options, knit_hooks, or something else?), in the pandoc Yaml header (some pandoc option?), or in the LaTeX beamer template file? I feel like it should be in the LaTeX template file.
Below is a working example of a minimal markdown file, and a .tex template file I'm using to control some beamer settings.
example.Rmd
---
title: "Untitled"
author: "Ryan"
date: "March 1, 2016"
output:
beamer_presentation:
pandoc_args: '--latex-engine=xelatex'
includes:
in_header: latex-topmatter.tex
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Vertical Spacing is too much
Here is a working example.
- some
- bullets
Example code:
```{r, echo = TRUE}
a <- 1
a
a+a
```
latex-topmatter.tex
% declare overall beamer theme to use as baseline
\usetheme{default}
% make code-output smaller
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{fontsize=\tiny,commandchars=\\\{\}}
% make console-output smaller:
\makeatletter
\def\verbatim{\tiny\#verbatim \frenchspacing\#vobeyspaces \#xverbatim}
\makeatother
% set vertical spacing between paragraphs:
% \parskip{0pt}
% \addtobeamertemplate{blocks}{}{\setlength{\parskip}{0pt}}
% \addtobeamertemplate{block begin}{}{\setlength{\parskip}{0pt}}
% \addtobeamertemplate{block end}{}{\setlength{\parskip}{0pt}}
% % \setlength{\emergencystretch}{0em}
\setlength{\parskip}{0pt}
I've tried making the font of the R-commands or R-output smaller, which does not seem to affect the paragraph spacing.
I've tried using knit_hooks() as in this example:
https://github.com/ramnathv/slidify/issues/189, which mostly works - but then i can't seem to reduce the fontsize of the code and output.
I've also tried using \parskip{0pt}, and several other beamer options or parskip options, which are commented in the above latex-topmatter.tex section. None of them seem to change the spacing between chunks of text, R-code, or R-output. Am I even looking in the right place?
Here is a working example. Notice the definitions at the end of the header file:
Source code chunks are contained inside a Shaded environment which in turn uses \OuterFrameSep for its spacing. So we need to redefine that.
With \preto we prepend the commands \topsep=-10pt \partopsep=-10pt to every verbatim environment. This affects the spacing of output chunks.
example.Rmd
---
title: "Untitled"
author: "Martin"
date: "January 4, 2017"
output:
beamer_presentation:
keep_tex: yes
pandoc_args: --latex-engine=xelatex
includes:
in_header: latex-topmatter.tex
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Vertical Spacing is just right
Here is a working example.
- some
- bullets
Example code:
```{r, echo = TRUE}
a <- 1
a
a+a
```
latex_topmatter.tex
% declare overall beamer theme to use as baseline
\usetheme{default}
% make code-output smaller
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{fontsize=\tiny,commandchars=\\\{\}}
% make console-output smaller:
\makeatletter
\def\verbatim{\tiny\#verbatim \frenchspacing\#vobeyspaces \#xverbatim}
\makeatother
\setlength{\parskip}{0pt}
\setlength{\OuterFrameSep}{-4pt}
\makeatletter
\preto{\#verbatim}{\topsep=-10pt \partopsep=-10pt }
\makeatother
Why does the following R Markdown minimal (non)-working example not compile to PDF?
---
header-includes:
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \lhead{}
- \chead{}
- \rhead{The performance of new graduates}
- \lfoot{From: K. Grant}
- \cfoot{To: Dean A. Smith}
output:
pdf_document:
keep_tex: yes
latex_engine: xelatex
---
# Test
In particular, the problematic conversion happens to -\lfoot{From: K. Grant} and -\cfoot{To: Dean A. Smith}, as seen in the output .tex file:
\usepackage{fancyhdr}
\pagestyle{fancy}
\lhead{}
\chead{}
\rhead{The performance of new graduates}
true
true
For some reason, both of these lines are converted to true causing
LaTeX error: Missing \begin{document}
thereby preventing the document from compiling to PDF.
Changing \lfoot and \cfoot to just about anything else seems to lead to them being converted correctly. So what's going on here? I take it that there must be a problem with either knitr or pandoc in the conversion process.
NB: I'm not too familiar with R Markdown, and this is a follow-up question to Headers and footers created in Fancyhead not shown in PDF on TeX.SX on Tom's behalf.
The : character is the problem. pandoc seems to be trying to parse the header-includes content as if it were variables, and : is used to separate variables and values. It compiles if you quote the lines in question (don't forget, then, to escape the leading backslash)
---
header-includes:
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \lhead{}
- \chead{}
- \rhead{The performance of new graduates}
- "\\lfoot{From: K. Grant}"
- "\\cfoot{To: Dean A. Smith}"
output:
pdf_document:
keep_tex: yes
latex_engine: xelatex
---
# Test
Stitching the answer here with trial and error, I found that only by deleting the title key and using YAML pipe-denoted multiline string syntax would it compile:
---
output:
pdf_document:
keep_tex: yes
latex_engine: xelatex
header-includes: |
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhead{}
\fancyfoot{}
\lhead{My Title}
\rhead{My Note}
\lfoot{\today}\rfoot{Page \thepage}
---
I recommend using pandoc's raw_attribute feature (enabled by default) to mark the raw LaTeX section as such. This is the most robust solution.
header-includes: |
```{=latex}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhead{}
\fancyfoot{}
\lhead{My Title}
\rhead{My Note}
\lfoot{\today}\rfoot{Page \thepage}
```