[solved] Prevent HTML escaping for LaTeX

Is there a way to fence off some content so that it’s not processed by markdown, and is output truly verbatim? I’m trying various things but I keep running into problems.

I use KaTeX (similar to MathJax) for embedding LaTeX expressions (equations, etc) in my blog posts. LaTeX uses a lot of backslashes, and I’ve found that I need to double those to prevent them from being escaped. The syntax I use usually looks like this in the Markdown:

y = x^2

Note that I had to double-slash. Then, within the LaTeX markup, I often find that I have to double-escape backslashes there too, for example a command like \frac{a}{b} has to be entered as \\frac{a}{b}.

This is workable, although tedious, but I’ve found a problem that I can’t seem to fix, and is NOT workable. LaTeX uses two slashes at the end of a line to do things like terminate an equation in a set of equations that are supposed to be aligned over each other. Like this:

a_1x+b_1y+c_1z=d_1 \\ 
a_2x+b_2y+c_2z=d_2 \\ 

The problem I’m finding is that the double backslashes at the end of the lines are converted into BR tags (line breaks). I’ve tried a bunch of things but I can’t seem to find a way to prevent this. Doubling-up on backslashes just adds more backslashes followed by a BR tag.

So I thought, I wonder if there’s a way to prevent markdown processing entirely, so that not only will the double backslashes work correctly, but also I won’t have to double-escape all these characters in the first place? I’m looking through the documentation and I see things like the safeHTML function, but it strikes me that using a shortcode for this is probably doing it wrong (and might be really painful because writing all this stuff inside quoted strings might cause yet other types of pain related to quote-escaping.)

Is there a way to fence off some content so that it’s not processed by markdown, and is output truly verbatim?

1 Like

You need to escape the EOL \\ with \\\\\\ :wink:

Here’s an example. Click the source link on that page for raw Markdown.

No, it’s a known bug in Blackfriday.

Someone spent a lot of effort to fix this in Blackfriday (albeit for v2 and not v1 that Hugo uses). That PR is just sitting there for past 8 months, and no one from the Blackfriday team has even commented on it. Also interest in that PR seems zero in general, seeing the lack of reactions added to that (likes, comments) from non-Blackfriday- devs too (apart from me).

Thanks! I +1’ed that PR in Blackfriday.

I use KaTeX in my theme, BluestNight, with a math shortcode. Using the {{< >}} syntax for the shortcode seems to make everything work. You can find the contents of the shortcode here with example usage here.

The shortcode detects if it is being used inline or not based on if there are newlines between the inner content and the tags. It also wraps any lines of text in delimiters, so you don’t need to type a bunch of dollar signs everywhere.

Feel free to use or adapt the shortcode for yourself, if you find it useful.

Edit: Should also include the related JavaScript. This is used on top of including the various KaTeX files:

var elems = document.getElementsByClassName('katex-container');
for (var i = 0; i < elems.length; i++) {
        { delimiters: [
            {left: "$$", right: "$$", display: true},
            {left: "\\[", right: "\\]", display: true},
            {left: "$", right: "$", display: false},
            {left: "\\(", right: "\\)", display: false}

Wonderful, thanks! I’ll mark this thread as fixed: I think this is an excellent reference now for anyone with the same questions I had.

The link is broken. Is this theme dead now?:sweat_smile:

No, the server I was self-hosting Git on was DOS-ing itself randomly, so I moved as much hosted stuff off of it as I could. Also I’ve rebranded the theme as Midnight and am using BluestNight as an umbrella name for various website-related projects.

@Shadow53 Thanks for sharing. I would love see how it work, but your first link gives a 502 error.