Escaping dollar sign in ipython notebook

PythonMarkdownIpythonMathjax

Python Problem Overview


I have a markdown cell in iPython that contains four dollar signs. iPython interprets anything between dollar signs as a MathJax expression, which is not what I want. How do I escape the dollar signs? Escaping them with a backslash prevents MathJax from kicking in, but the backslash shows in the compiled Markdown.

ANy ideas on how to get just the dollar sign?

Thanks

Python Solutions


Solution 1 - Python

Put two backslashes in front of dollar signs. For example:

Some prices: \\$3.10, \\$4.25, \\$8.50.

(running Jupyter notebook server 5.7.0)

Solution 2 - Python

You can escape $ with math mode by using a backslash. Try $\$$

Solution 3 - Python

If you use <span>$</span>, MathJax won't process it as a delimiter. You should be able to enter that in Markdown. For example, I've used that here: $ This is not math $.

Solution 4 - Python

I was not able to get most of these solutions working. One that did work, though, is explained here. It refers to a SO question here and is as simple as:

<span class="tex2jax_ignore">$900 vs $4,500</span>

Hope this helps someone!

Solution 5 - Python

I'm aware that this topic is old, but it's still somehow the first google result and its answers are incomplete.

You can also surround the $ with `backticks`, the same way that you would display code in Jupyter.

So $ becomes `$`, and should display without error

Solution 6 - Python

Did you try using the equivalent HTML entity instead?:

enter image description here

e.g.

enter image description here

Solution 7 - Python

Old post, but it's still at the top of Google! Here is a clarification I wish I had understood sooner:

Jupter Notebook Markdown

Jupyter Notebook "Markdown" cells allow you to write HTML in them, and will render the HTML as expected in most cases. This is different from some other Markdown editors where you can only use a very limited subset of HTML, or no HTML at all.

Why This Matters for the $ Literal

A very helpful aspect to this is when you want to use the literal version of a special character such as $. Meaning, you want a $ to appear in the rendered version of the cell, rather than using $ to start a MathJax block.

This goes beyond $ to other special characters. Maybe you want a * to appear in the rendered cell, rather than indicating a bullet point or bold/italic text. I'm having a harder time imagining the use case for # or _ or [ but there might be some reason you would want to write those literals as well (without surrounding them with backticks, causing them to be styled as inline code blocks like I have here).

HTML Approach 1: Create an HTML Block

A couple of the other answers describe surrounding the $ with an HTML <span> tag. What this is doing (if you don't include a newline in the middle like this GitHub comment shows) is telling Jupyter Notebook not to interpret the content as Markdown any more, and instead to interpret it as HTML. In HTML, $ doesn't mean that you're starting a MathJax block, so it will render literally.

Personally I don't like this approach because it's hard to tell what problem the span is solving, and it's using span as more of a hack/workaround than how it is intended to be used in the HTML spec.

HTML Approach 2: HTML Character Encoding

This is what the &#36; answer above is suggesting, although I don't think it's fully explained.

The idea here is that instead of putting the $ literal in your Markdown source, you put the HTML character code version &#36; instead. Here is a longer explanation of these character codes, if you're curious. But essentially all you need to know is that if you write the relevant character code when editing the source of the Markdown cell, it won't be interpreted as Markdown, but it will render correctly.

So instead of this Markdown source:

The price range is $100 to $200

(which will render 100 to as MathJax)

Simply replace the $s with &#36;s:

The price range is &#36;100 to &#36;200

(which will render the dollar signs literally)

I prefer this approach because the entire purpose of HTML character encoding is escaping special characters, so if someone in the future is trying to understand what your code is doing, they should be able to figure it out pretty quickly.

I also just in general try to avoid HTML opening and closing tags within my Jupyter Notebook Markdown since small typos (like forgetting a /) can have confusing cascading implications, but your mileage may vary :)

If you're looking for character codes other than $, I like this resource, although you can also just Google "HTML code" plus the character you want and get the right answer most of the time.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRobertoView Question on Stackoverflow
Solution 1 - PythonDonny WinstonView Answer on Stackoverflow
Solution 2 - PythonYuki LiuView Answer on Stackoverflow
Solution 3 - PythonDavide CervoneView Answer on Stackoverflow
Solution 4 - PythonktrView Answer on Stackoverflow
Solution 5 - PythonM. Gruben-TrejoView Answer on Stackoverflow
Solution 6 - PythonOke UwechueView Answer on Stackoverflow
Solution 7 - PythonErin HoffmanView Answer on Stackoverflow