skd
skd

Reputation: 1977

xaringan: add custom Latex file with macros

Is it possible to import a Latex macro file, for instance

\newcommand{\Xcal}{\mathcal{X}

so that I can then use it between $...$ as $\Xcal$?

Upvotes: 4

Views: 754

Answers (2)

Fei YE
Fei YE

Reputation: 431

MathJax allows you to define macros. In Xaringan, you simply put your macros in double dollar signs.

---
title: "Presentation Ninja"
subtitle: "⚔<br/>with xaringan"
author: "Yihui Xie"
date: "2016/12/12 (updated: `r Sys.Date()`)"
output:
  xaringan::moon_reader:
    lib_dir: libs
    nature:
      highlightStyle: github
      highlightLines: true
      countIncrementalSlides: false
---

$$\newcommand{\Xcal}{\mathcal{X}}$$

# Math Macros

You can define your own macros by putting them in double dollars signs.

```
$$\newcommand{\Xcal}{\mathcal{X}}$$
```

This symbol $\Xcal$ is a calligraphic X.

Upvotes: 5

Martin Schmelzer
Martin Schmelzer

Reputation: 23919

Yes, this seems to work:

---
title: "Presentation Ninja"
subtitle: "⚔<br/>with xaringan"
author: "Yihui Xie"
date: "2016/12/12 (updated: `r Sys.Date()`)"
output:
  xaringan::moon_reader:
    lib_dir: libs
    nature:
      highlightStyle: github
      highlightLines: true
      countIncrementalSlides: false
---

<script type="text/x-mathjax-config">
MathJax.Hub.Config({
  TeX: {
    Macros: {
      Xcal: "{\\mathcal{X}}",
      water: "{H_2O}"
    }
  }
});
</script>

$\water$    
$\Xcal$

enter image description here

It is important to use type=text/x-mathjax-config on the script tag, so mathjax finds the block. Details on defining macros in MathJax can be found here.

An alternative is to include the definition using the before_body YAML option:

---
title: "Presentation Ninja"
subtitle: "⚔<br/>with xaringan"
author: "Yihui Xie"
date: "2016/12/12 (updated: `r Sys.Date()`)"
output:
  xaringan::moon_reader:
    lib_dir: libs
    nature:
      highlightStyle: github
      highlightLines: true
      countIncrementalSlides: false
    includes:
      before_body: local.html
---

Upvotes: 5

Related Questions