title: Insert non-breaking space in [R]Markdown math expression for HTML output
body: <p>I am writing scientific reports in bookdown and I would like to use <a href="https://en.wikipedia.org/wiki/Non-breaking_space" rel="nofollow noreferrer">non-breaking space</a>s as thousands separators follwoing the <a href="https://en.wikipedia.org/wiki/ISO_31-0#Numbers" rel="nofollow noreferrer">SI/ISO 31-0 standard</a>.</p>
  
  <p>Actually, I would prefer the non-breaking <a href="https://en.wikipedia.org/wiki/Thin_space" rel="nofollow noreferrer">thin space</a> (<code>U+202F</code>/<code>&amp;#8239;</code>) but for simplicty let's consider <code>U+00A0</code>/<code>&amp;nbsp;</code> for here.</p>
  
  <p>I setup a <code>knitr</code> hook to do this on the fly:</p>
  
  <pre class="lang-r prettyprint-override"><code>knitr::knit_hooks$set(inline=function(output)
                                 ifelse(is.numeric(output),
                                        prettyNum(round(output, 1),
                                                  big.mark='&amp;nbsp;'),
                                        output))
  </code></pre>
  
  <p>This works as intended as long as I don't use any inline R-expressions returning numerical output > 999 within math expressions.</p>
  
  <p>The following bookdown MWE illustrates the problem:</p>
  
  <pre class="lang-none prettyprint-override"><code>---
  output:
    bookdown::html_document2: default
  ---
  ```{r set-output-hook, include=FALSE}
  knitr::knit_hooks$set(inline=function(output)
                                 ifelse(is.numeric(output),
                                        prettyNum(round(output, 1),
                                                  big.mark='&amp;nbsp;'),
                                        output))
  ```
  
  This works:
  The product of $\pi$ and `r 1000` is `r pi*1000`.
  
  This fails to render: 
  $\pi\cdot`r 1000`=`r pi*1000`$
  
  This renders but is cumbersome as it requires me to know *a priori* which
  values might exceed 999:
  $\pi\cdot1000=`r as.character(round(pi*1000, 1))`$
  </code></pre>
  
  <p>I tried to track it down and came up with the following rmarkdown MWE:</p>
  
  <pre><code>---
  output:
    rmarkdown::html_document:
      keep_md: true
  ---
  
  | Rmarkdown    | Render     | HTML                                                | Markdown     |
  |--------------|------------|-----------------------------------------------------|--------------|
  | `1000`       | 1000       |`1000`                                               | `1000`       |
  |`$1000$`      |$1000$      |`&lt;span class="math inline"&gt;\(1000\)&lt;/span&gt;`          |`$1000$`      |
  |              |            |                                                     |              |
  |  `100,0`     | 100,0      |`100,0`                                              | `100,0`      |
  |`$100,0$`     |$100,0$     |`&lt;span class="math inline"&gt;\(100,0\)&lt;/span&gt;`         |`$100,0$`     |
  |              |            |                                                     |              |
  |  `100 0`     | 100 0      |`100 0`                                              | `100 0`      |
  |`$100 0$`     |$100 0$     |`&lt;span class="math inline"&gt;\(100 0\)&lt;/span&gt;`         |`$100 0$`     |
  |              |            |                                                     |              |
  |  `100&amp;nbsp;0`| 100&amp;nbsp;0 |`100 0`                                              | `100&amp;nbsp;0` |
  |`$100&amp;nbsp;0$`|$100&amp;nbsp;0$|`&lt;span class="math inline"&gt;\(100&amp;amp;nbsp;0\)&lt;/span&gt;`|`$100&amp;nbsp;0$`|
  </code></pre>
  
  <p>The first two columns of the table are sufficient to see the problem:
  Each pair of rows shows the number <s>1000</s> 1&nbsp;000) in text and math context; without any space, with a comma, with a simple space, and with a non-breaking space as thousands separator.
  The latter fails to render in math context.</p>
  
  <p>To track down the problem, I inspected the resulting HTML and Markdown (<code>keep_md: true</code>) output and added the corresponding code as columns three and four for a better overview what's going on.</p>
  
  <p>For clarity, here is an adjusted version of the above rmarkdown MWE replacing simple spaces by <code>_</code> and non-breaking spaces by <code>-</code> in the HTML and Markdown output columns:</p>
  
  <pre><code>---
  output:
    rmarkdown::html_document:
      keep_md: true
  ---
  
  | Rmarkdown    | Render     | HTML                                                | Markdown     |
  |--------------|------------|-----------------------------------------------------|--------------|
  | `1000`       | 1000       |`1000`                                               | `1000`       |
  |`$1000$`      |$1000$      |`&lt;span_class="math_inline"&gt;\(1000\)&lt;/span&gt;`          |`$1000$`      |
  |              |            |                                                     |              |
  |  `100,0`     | 100,0      |`100,0`                                              | `100,0`      |
  |`$100,0$`     |$100,0$     |`&lt;span_class="math_inline"&gt;\(100,0\)&lt;/span&gt;`         |`$100,0$`     |
  |              |            |                                                     |              |
  |  `100 0`     | 100 0      |`100_0`                                              | `100_0`      |
  |`$100 0$`     |$100 0$     |`&lt;span_class="math_inline"&gt;\(100_0\)&lt;/span&gt;`         |`$100_0$`     |
  |              |            |                                                     |              |
  |  `100&amp;nbsp;0`| 100&amp;nbsp;0 |`100-0`                                              | `100&amp;nbsp;0` |
  |`$100&amp;nbsp;0$`|$100&amp;nbsp;0$|`&lt;span_class="math_inline"&gt;\(100&amp;amp;nbsp;0\)&lt;/span&gt;`|`$100&amp;nbsp;0$`|
  </code></pre>
  
  <p>So from what I can tell</p>
  
  <ol>
  <li>This is not a bookdown issue as it can be reproduced by plain rmarkdown.
  
  <ul>
  <li>I'm just mentioning bookdown as I would be happy with a bookdown-specific work-around.</li>
  </ul></li>
  <li>This is not an rmarkdown issue, as the generated Markdown looks exactly as I would expect it to look like.
  
  <ul>
  <li>I'm just mentioning rmarkdown as I would be happy with an rmarkdown-specific work-around.</li>
  </ul></li>
  <li>This is not a MathJax issue, as the HTML code has the plain <code>&amp;</code> replaced by <code>&amp;amp;</code> and I would not expect that to render properly.
  
  <ul>
  <li>Anyways,I would be happy with an MathJax-related work-around.</li>
  </ul></li>
  <li>I suspect it's pandoc that replaces <code>&amp;</code> by <code>&amp;amp;</code> in code and math context but not in text context.
  
  <ul>
  <li>I'm sure <em>if</em> there is a way to convince pandoc <em>not</em> to do this, it will be easy to configure this through the rmarkdown YAML header.</li>
  </ul></li>
  </ol>
  
  <p>Any idea on how to get the <code>&amp;nbsp;</code> transferred literally from Markdown to HTML in math context would probably help me to figure out the rest.</p>
  
  <hr>
  
  <p><strong><em>Addendum:</em></strong></p>
  
  <p>As <a href="https://stackoverflow.com/a/48234992/2451238">pointed out</a> by <a href="https://stackoverflow.com/users/2425163/tarleb">@tarleb</a>, <code>$100&amp;nbsp;0$</code> is not valid Latex.
  However, modifiying the HTML manually to contain <code>\(100&amp;nbsp;0\)</code> works just fine as <a href="https://github.com/dpvc/MathJax/commit/8fb4157eb11e476e675486e8ac9d1a1a4074ce15" rel="nofollow noreferrer">MathJax treats non-breaking spaces as spaces</a>.
  As I am not concerned about PDF output via LaTex, this means simply not converting <code>$100&amp;nbsp;0$</code> to <code>\(100&amp;amp;nbsp;0\)</code> but to <code>\(100&amp;nbsp;0\)</code> (just as <code>100&amp;nbsp;0</code> is not converted to <code>100&amp;amp;nbsp;0</code> either) when converting the Markdown to HTML would be all that I need.</p>