title: Rmarkdown inline latex error
body: <p>I have an R function that returns text, including some LaTeX math mode, and I'm trying to use this text from an inline r chunk in an rmarkdown document. However, I get some quite strange behaviour regarding the use of math mode that I don't understand. It may be related to <a href="https://stackoverflow.com/questions/48942068/inline-code-and-inline-latex-r-markdown-pandoc-error-43">this question</a>.</p>
  
  <h1>The following example works:</h1>
  
  <pre><code>---
  title: "Untitled"
  output: pdf_document
  ---
  
  ```{r print_function}
  print_fun &lt;- function(x){
    return(paste0("$\\mathrm{p}(\\beta &lt; 0)$ &gt; ", x))
  }
  
  x &lt;- 0.5
  
  ```
  
  Testing our printing function, we get `r print_fun(x)`.
  </code></pre>
  
  <p>Producing output </p>
  
  <p><a href="https://i.stack.imgur.com/hxGam.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hxGam.png" alt="working output"></a></p>
  
  <h1>A small change breaks it</h1>
  
  <p>If I move the <code>$</code> symbol a few places to the right, in order to also include the <code>&gt;</code> sign within the math mode block, the document no longer compiles:</p>
  
  <pre><code>```{r print_function}
  print_fun &lt;- function(x){
    return(paste0("$\\mathrm{p}(\\beta &lt; 0) &gt; $", x))
  }
  
  x &lt;- 0.5
  
  ```
  </code></pre>
  
  <p>With the following error message:</p>
  
  <pre><code>Error producing PDF.
  ! LaTeX Error: \mathrm allowed only in math mode.
  
  See the LaTeX manual or LaTeX Companion for explanation.
  Type  H &lt;return&gt;  for immediate help.
   ...                                              
  
  l.137 ...g our printing function, we get \$\mathrm
  
  Error: pandoc document conversion failed with error 43
  Execution halted
  </code></pre>
  
  <p>This code works in an RSweave document, and I don't see why including the <code>&gt;</code> symbol into math mode would have this effect.</p>
  
  <h1>Closing math mode after the numerical reference compiles again</h1>
  
  <pre><code>```{r print_function}
  print_fun &lt;- function(x){
    return(paste0("$\\mathrm{p}(\\beta &lt; 0) &gt; ", x, "$"))
  }
  
  x &lt;- 0.5
  
  ```
  </code></pre>
  
  <p>Why?</p>