2
1. An amount between $5 and $10.  
2. An amount between \$5 and \$10.  
3. An amount between \\$5 and \\$10.  

Please include the code chunk above in a blogdown .Rmd file, and make sure your Hugo theme supports LaTeX equations. Then Save and serve your site with blogdown::serve_site(). When I do this none of the three options shown above properly escape LaTeX's dollar sign $.

Here's what happens to me (and yes I did update all packages with update.packages(ask = FALSE, checkBuilt = TRUE) prior to trying this). These three results below are from the same three attempts above:

  1. The and in this statement is formatted in LaTeX style while everything else is formatted "normal".
  2. The and in this statement is formatted in LaTeX style while everything else is formatted "normal" (in other words the results are exactly identical to number 1).
  3. The LaTeX $ is escaped but the sentence now displays as "An amount between \$5 and \$10." with one slash before each dollar sign.

How can I properly esacpe the dollar signs in this sentence "An amount between $5 and $10." with a Hugo theme that accepts LaTeX input?

I should mention that if I knit the same .Rmd file with Ctrl+Shift+K the dollar sign does get escaped "properly" if I utilize the solution shown in example #2. The issue only appears when blogdown::serve_site() serve my site, leading me to believe the issue is with Hugo themes accepting LaTeX input.

Display name
  • 4,153
  • 5
  • 27
  • 75

3 Answers3

1

A simple trick is to put in a space between the $ and the number.

4. An amount between $ 5 and $ 10. 

MathJax should only triggered and activate math mode when it is (first) followed by a non-whitespace character and should end at a $ preceded by something that isn't white space.

It doesn't always work quite right but I tried it on my blogdown setup and that seemed okay.

Also ... (and I have no idea if this could be the reason) ... but which version of MathJax are you loading? I'm including the following code in my pages, and that renders your third example okay too.

  <script type="text/x-mathjax-config">
    MathJax.Hub.Config({
    tex2jax: {
      inlineMath: [ ['$','$'], ["\\(","\\)"] ],
      displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
      skipTags: ['script', 'noscript', 'style', 'textarea', 'pre'],
      processEscapes: true
    }
    });
  </script>
  <script type="text/javascript"
      src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
  </script>
ekstroem
  • 5,957
  • 3
  • 22
  • 48
  • how do I determine what version of MathJax I am using, and how do I update this value to say `2.7.5`? Thanks – Display name Jul 16 '20 at 00:24
  • 1
    If you render your blog post and look at the resulting html code then you should be able to see the MathJax-version 8and arguments) that you are linking to. Depending on the theme you are using try looking at one of the files under `themes/your_theme_name/layouts/partials/` and see which of the html-files that include the MathJax code. For my setup it is in `footer.html`. You update by just changing the link to, say, `src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML"`. You don't need to install anything. Just link to it – ekstroem Jul 16 '20 at 00:48
  • I tried your trick #4 and also updated MathJax to 2.7.5 but neither worked. I'm realizing I don't really have an issue escaping dollar signs, and really have an issue with hugo themes (that don't properly follow Pandoc rules and escape dollar signs). You answered this question here so thanks. – Display name Jul 16 '20 at 12:33
  • 1
    Bummer. Sorry I couldn't help more. Which Hugo theme are you running? – ekstroem Jul 16 '20 at 17:24
  • 1
    For other's reference I've got another similar question/answer here https://stackoverflow.com/questions/62927411/pandoc-rules-regarding-latex-not-followed-on-r-blogdown-hugo-themes – Display name Jul 20 '20 at 19:48
0

In MathJax.Hub.Config set processEscapes: true

Then, use \\\$ to display the dollar sign - triple backslash....

orrymr
  • 2,264
  • 4
  • 21
  • 29
-2

your cost is $C=\mathrm{$}6$
...works for me in ipython notebook

metrprof
  • 27
  • 3