How can I use the LaTeX package bbm
in an R markdown for html/docx output?
Currently I'm using the hack solution below where, essentially, I just abandon using the package bbm
for .docx/.html output. Is there a hack solution in which I can still use the package?
Note, this question related to my question In an R Markdown document, “includes” for docx output? where there I'm specifically asking about how to move these special <!--- For DOCX Only --->
code chunk to a preamble-word.tex
file to be indcluded in the YAML header. This question is also related to the question How to get \bm{} to work in an R markdown (to HTML) file? My current hack for bbm
is basically an adaptation of one of the hacks proposed as an answer to that question.
---
title: "TinyTeX Test"
header-includes:
- \usepackage{bbm}
output:
pdf_document: default
html_document: default
word_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
<!--- For HTML Only --->
`r if (knitr:::is_html_output()) '
$\\newcommand{\\mathbbm}[1]{\\mathbf{#1}}$
'`
<!--- For DOCX Only --->
`r if (!knitr:::is_latex_output() & !knitr:::is_html_output()) '
\\newcommand{\\mathbbm}[1]{\\mathbf{#1}}
'`
Hello
\[\mathbbm{1}_{S}(x)\]
If you would like to create PDF documents from R Markdown, you will need to have a LaTeX distribution installed. Although there are several traditional options including MiKTeX, MacTeX, and TeX Live, we recommend that R Markdown users install TinyTeX.
To transform your markdown file into an HTML, PDF, or Word document, click the “Knit” icon that appears above your file in the scripts editor. A drop down menu will let you select the type of output that you want. When you click the button, rmarkdown will duplicate your text in the new file format.
Ok, another answer, I just realized, these are two slightly different questions - one by the question starter and one by the bounty starter.
I would need a solution to this issue as well. Here is my minimum example expected to work:
--- title: "Testing LaTeX packages in Docx format" output: word_document header-includes: - \usepackage{xfrac} --- $$ \sfrac{1}{2} $$ ```
In this case the outlined answer with MathJax will not work so well. sfrac is not supported by MathJax (http://docs.mathjax.org/en/latest/input/tex/macros/index.html)
I don't know if you need \sfrac specifically, but \frac or \tfrac would be supported. Maybe you can use these instead.
Here an example with \frac
---
title: "Frac Test"
header-includes:
- \usepackage{xfrac}
output:
pdf_document: default
html_document: default
word_document: default
---
$$
\frac{1}{2}+1
$$
Output of \frac example html:
This will work for .pdf / word and html.
Here an example with \tfrac
---
title: "TFrac Test"
header-includes:
- \usepackage{xfrac}
output:
pdf_document: default
html_document: default
word_document: default
---
$$
\tfrac{1}{2}+1
$$
Output \tfrac example html:
This will run for html, pdf - for word it will write 1/2 instead of a formula, since there is no \tfrac equivalent in word.
\cfrac and \dfrac are also supported by MathJax.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With