Search code examples
rr-markdownmathjaxxaringan

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$?


Solution

  • 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
    ---