# Putting a user defined Latex environment into R markdown or knitr

I am getting an error when I try to include a user defined environment in my R markdown document. I have at the top:

header_includes:
- \usepackage{amsmath}
- \usepackage{amssymb}
- \usepackage{amsthm}
- \newtheorem{assumption}{Assumption}


And then in the document itself, if I write

\begin{assumption}
$a > b$
\end{assumption}


I get the error "! Undefined Control Sequence \begin{assumption}" when I click compile.

I don't get an error if I use a regular proof environment or equation environment. That is,

$$a^2 + b^2 = c^2$$


does not give me an error. Of course, including the "header_includes" part in the preamble of a latex document and then using the assumption environment in that latex document is no problem. It would just be nice if I could copy and paste Latex directly into R markdown, since TexStudio has many nice autocompletion features and coloring schemes that make it the better environment to write latex, but Markdown is the easier way to integrate LaTex and code. Any help would be great. Thanks!

• Could you please post a minimal (non)working example? Mar 25 '17 at 5:51

You don't need to list the \newtheorem macro as a header_includes argument, just put it at the top of your document:

---
title: "Test"
- \usepackage{amsmath}
- \usepackage{amssymb}
- \usepackage{amsthm}
---

\newtheorem{assumption}{Assumption}

Here's my assumption:

\begin{assumption}
$a > b$
\end{assumption}

And done.

• But why this does not have any effect on html? or notebook? how to tweak this to work for both? Aug 8 '18 at 5:48
• that is when I present this in html slides also this should be visible. how to do that? Aug 8 '18 at 5:49
• LaTeX code is only processed when your output is generated by the LaTeX program. That means pdf or Beamer slides. html slides are generated by a different backend, and that backend doesn't know LaTeX Aug 8 '18 at 10:09