9

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.

tinytextest.Rmd

---
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)\]

.pdf output

enter image description here

.docx/.html output

enter image description here

1
  • I have just run into this same issue. Apparently, there's no solution yet that anyone knows about... Commented Feb 26, 2021 at 13:42

3 Answers 3

3
+50

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:

enter image description here

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:

enter image description here

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.

Sign up to request clarification or add additional context in comments.

Comments

2

You can somehow solve this for most of the important symbols/characters via Mathjax. Here is a list of supported symbols/characters:http://docs.mathjax.org/en/latest/input/tex/macros/index.html Just look there for the symbol you need.

Then you use it like this:

$$
\require{mhchem}
\begin{equation}
  [C] + [R] 
  \xrightleftharpoons[k_{-1}]{k_1}
  [CR] + [C] 
  \xrightleftharpoons[k_{-2}]{k_2}
  [C2R]
(\#eq:multiplebinding)
\end{equation}
$$

So you need to enclose your command in $$. In this example \xrightleftharpoons was used. From the table I linked you above you can see that you need to add \require{mhchem} in this case. For your case this isn't needed, you just need to write \mathbb.

Example:

  ---
title: "TinyTeX Test"
header-includes:
  - \usepackage{mathbbol}
output:
  pdf_document: default
  html_document: default
  word_document: default
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

Hello Mori, this should work


$$
    \mathbb{1}_{S}(x)
$$

Greetings, Steffen

HTML OUTPUT: enter image description here

WORD OUTPUT: enter image description here

Also note, that I replaced bbm with \usepackage{mathbbol} - otherwise it had errors rendering the pdf for me. Here a discussion about bbm on tex.stackexchange ... most people advise against using bbm, since there are better packages to get the symbols https://tex.stackexchange.com/questions/26637/how-do-you-get-mathbb1-to-work-characteristic-function-of-a-set

Comments

0

Ok, so after Steffen's answers, I've come to the conclusion that the proper answer would be: Including LaTeX packages in docx documents cannot be done (at least up to now).

The only solution is to use the equivalent MathJax-supported command/symbol. Nevertheless, Steffen's answer pointed me in the correct direction of the commands supported by MathJax (many thanks!), so I awarded him the bounty.

Also, as my specific problem was with diagonal fractions, I tried all the frac variants allegedly supported by MathJax. There are six of them, out of which the following worked: \dfrac, \frac, and \dfrac, and the following did not: \cfrac, \flatfrac, and \genfrac.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.