2
votes

It's easy to write TeX equations in Markdown documents and convert them to actual typeset equations using pandoc (version 1.18), either in a PDF document (through LaTeX) or an HTML document (through MathJax): surround math in $...$ for inline equations and $$...$$ for block equations.

However, there seems to be a discrepancy between MathJax and TeX syntax with special characters like %. For instance, consider this sample document:

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 

$$\text{\% change} = \frac{x_2 - x_1}{x_1} \times 100$$

Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

Converting this to PDF via LaTeX using the command

pandoc test.md -o test.pdf

correctly produces an unescaped percent sign:

tex output

However, converting the same document to HTML with MathJax, using the command

pandoc test.md -s --mathjax -o test.html

incorrectly produces an escaped percent sign:

html output

For now, I've been manually escaping/unescaping percent signs before converting my documents to PDF/HTML, which seems to defeat the purpose of having a master Markdown document that can convert to any format.

What is the correct way to handle escaped percent signs in both TeX and MathJax? Is there a setting in MathJax that allows special characters to be escaped? Do I need to tell LaTeX to accept unescaped percent signs in math mode?

1
this sounds like a bug in MathJax, are you using the latest version?mb21
This happens in the current version, 2.7.1: github.com/mathjax/MathJax/releases/tag/2.7.1Andrew
The extraneous escape slash appears on the live demo at the main MathJax site too: mathjax.org/#modal-livedemo. This makes me think it's not necessarily a bug, but by design (and on this forum, it sounds like % is not treated a special, escapable character: groups.google.com/d/msg/mathjax-users/Ed1nKT-xtAE/IKvO_NZ9e1QJ)Andrew
probably has something to do with the \text{} it's in... why not use \percent instead?mb21
For the record, MathJax does not support macros inside \text{} (with the sole exception of $ to re-enter math mode), see the documentation.Peter Krautzberger

1 Answers

1
votes

(I know this is not tagged under [R] but I know Andrew is an avid R and Rmarkdown user, so I hope this will suffice.)

This is possible using Rmarkdown and knitr. The script below produces the desired outputs when rendered with rmarkdown::render. I also create a gist of the script.

---
title: TEST
output:
    pdf_document:
        latex_engine: xelatex
    html_document:
        fig_caption: true
---

```{r, echo = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  echo = TRUE,
  cache = FALSE,
  comment = "#>",
  fig.path = "fig-",
  fig.pos = "h"
)

# this will check what the output format is going to be
getOutputFormat <- function() {
  knitr::opts_knit$get("rmarkdown.pandoc.to")
}
```


Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 

```{r, warning=FALSE, message=FALSE, echo=FALSE, eval=TRUE, results='asis'}
s <- "$$\\text{\\% change} = \\frac{x_2 - x_1}{x_1} \\times 100$$"
if(getOutputFormat() == 'html') s <- gsub('\\\\%', '%', s) # dont escape if html
knitr::asis_output(s)
```

Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.