8

I am new to Rmarkdown and plan to use ioslides/slidy/xaringan to do my presentation.

I used to use the beamer to do presentations. In the beamer, I have theorem environment which is designed to mathematics theorems. I want to be able to have that kind of format in ioslides/slidy/xaringan. I know I can use $$...$$ to include latex code and can display equation. However, this is not enough to my needs.

I also know in bookdown one can have theorem environment. But I do not know how to do that in the ioslides/slidy/xaringan output format.

Joundill
  • 6,828
  • 12
  • 36
  • 50
Slm2004
  • 225
  • 1
  • 5
  • ioslides, slidy, and xaringan use MathJax which doesn't provide environments from amsmath like theorem. Maybe this helps: http://drz.ac/2013/01/17/latex-theorem-like-environments-for-the-web/ – Martin C. Arnold Sep 05 '20 at 09:37
  • Thank you for you explanation. I checked the link there. It uses Octopress. I do not want to use that. Where should I put these HTML css code in ioslides/slidy/xaringan? – Slm2004 Sep 05 '20 at 14:18
  • If you don't mind me asking, how did you get a theorem environment working in beamer? The below answered method doesn't work for beamer presentations it seems, so I was curious what working method you had. Thanks! – obewanjacobi Oct 12 '22 at 19:18

1 Answers1

8

This would be too lengthy for a discussion in the comments, so here is an answer. The following defines some styles inspired by the idea in the above-mentioned blog post:

styles.css

.theorem {
  display: block;
  font-style: italic;
  font-size: 24px;
  font-family: "Times New Roman";
  color: black;
}
.theorem::before {
  content: "Theorem. ";
  font-weight: bold;
  font-style: normal;
}
.theorem[text]::before {
  content: "Theorem (" attr(text) ") ";
}
.theorem p {
  display: inline;
}

To use these styles in rmarkdown presentations you can include them via the YAML header. For ioslides it works like this (works analogously for slidy and xaringan):

ioslides.Rmd (Note that this requires styles.css to be in the same folder as ioslides.Rmd)

---
title: "Theorem demo"
output:
  ioslides_presentation:
    css: styles.css
---

You can now create a theorem using a <div> element of the class theorem:

## CLT

<div class="theorem" text='CLT'>
  The CLT states that, as $n$ goes to infinity, the sample average $\bar{X}$
  converges in distribution to $\mathcal{N}(\mu,\sigma^2/n)$.
</div>

enter image description here

EDIT: Copenhagen style

Recreating beamer styles exactly is cumbersome but with a few CSS tricks you can get close. Here is an example that looks similar to theme: copenhagen.

.theorem {
  display: block;
  font-style: italic;
  font-size: 24px;
  font-family: "Times New Roman";
  color: black;
  border-radius: 10px;
  background-color: rgb(222,222,231);
  box-shadow: 5px 10px 8px #888888;
}
.theorem::before {
  content: "Theorem. ";
  font-weight: bold;
  font-style: normal;
  display: inline-block;
  width: -webkit-fill-available;
  color: white;
  border-radius: 10px 10px 0 0;
  padding: 10px 5px 5px 15px;
  background-color: rgb(38, 38, 134);
}
.theorem p {
  padding: 15px 15px 15px 15px;
}

enter image description here

Martin C. Arnold
  • 9,483
  • 1
  • 14
  • 22
  • This is so good. Thank you very much! I got an extra question. Remember in Beamer, the theorem environment has a frame around the statement and some background color and shades of the frame. Do you happen to know the HTML code to realize this? – Slm2004 Sep 06 '20 at 00:33
  • That is exactly what I wanted. Thank you !!!!! I wish the best for you. – Slm2004 Sep 06 '20 at 12:54
  • I am trying to include the style.css in xaringan. It seemed can not recognize the latex code in the theorem environment. Do you know how to solve that problem? – Slm2004 Sep 11 '20 at 19:48
  • I do: this is because remark.js interferes with MathJax. It should work if you use `\(` ... `\)` instead of `$` ... `$` for delimiting math. :) – Martin C. Arnold Sep 11 '20 at 20:13
  • I see. Thank you! – Slm2004 Sep 12 '20 at 01:04
  • The width is only well-defined for webkit based browsers (Chrome and Safari). To cover Firefox and other browsers I recommend: width: 100%; width: -moz-available; width: -webkit-fill-available; width: fill-available; – D G May 05 '23 at 15:15