Codemirror: LaTeX code displays as italic between underscores in Markdown mode

Created on 23 Nov 2014  路  6Comments  路  Source: codemirror/CodeMirror

This is a very minor nuisance I have noticed while editing markdown cells in IPython Notebooks. The problem appears to also persist on the MarkDown Demo on CodeMirror's site.

In Markdown, underscores toggle italics, but in LaTeX they also serve to indicate subscripts. Unfortunately, while editing with CodeMirror, underscores used in LaTeX mode sometimes cause italics to be toggled in the edit view.

Steps to reproduce:
1) Start editing a markdown cell.
2) Type the following: $$a_{n+1} = 2a_{n} + b$$
3) The text will display as a__{n+1} = 2a__{n}+b` where the characters in bold here have been italicized in the editor.

Non-example:
1) Enter the following into a Markdown cell: $$a_n = 2a_n+b$$

It appears that italics are ignored when there is a single alphanumeric character after the underscore, but the case in which the subscript consists of a bracketed group of characters is not handled correctly.

This is particularly annoying, because in italic monospaced fonts, pipes | look like forward slashes / and backslashes \ look like pipes |.

Thanks!

PS: WIndows 7, 64-bit with Chrome, but I doubt it matters!

Most helpful comment

@williamstein A bug in your solution is if I type a $ in a inline code or code block, it'll be highlighted as TeX.

like

$('#div').html();

See https://github.com/Moeditor/Moeditor/issues/5

Maybe we can find a way to set the "priority" of the marks?

All 6 comments

Why do you expect the Markdown mode to correctly highlight LaTeX syntax?

Ah, I see your point. I suppose this is more of a problem specific to the use of CodeMirror on sites that support MathJax. Still, it's interesting to me that italic are ignored for single character subscripts in LaTeX but not more than that, which suggests there may be an easy fix to the problem. Any chance this can be easily resolved somehow? If not, where in the repo would I begin looking if I want to make a contribution of my own?

Thanks!

I have precisely the same problem with SageMathCloud and users complaining
about this. Maybe a multimode that switches to proper latex on $ would
work well enough?

  • William Stein (cell phone)
    On Nov 24, 2014 11:20 AM, "Ben Bray" [email protected] wrote:

Ah, I see your point. I suppose this is more of a problem specific to the
use of CodeMirror on sites that support MathJax. Still, it's interesting to
me that italic are ignored for single character subscripts in LaTeX but not
more than that, which suggests there may be an easy fix to the problem. Any
chance this can be easily resolved somehow? If not, where in the repo would
I begin looking if I want to make a contribution of my own?

Thanks!

Reply to this email directly or view it on GitHub
https://github.com/codemirror/CodeMirror/issues/2942#issuecomment-64247669
.

A multiplexing mode that switches to the stex mode between $$ markers would be very easy to set up, and probably solves your problem.

On Mon, Nov 24, 2014 at 2:24 PM, Marijn Haverbeke [email protected]
wrote:

A multiplexing http://codemirror.net/doc/manual.html#addon_multiplex
mode that switches to the stex mode between $$ markers would be very easy
to set up, and probably solves your problem

For what it is worth, here's what I did for SageMathCloud (in coffeescript).

CodeMirror.defineMode "gfm2", (config) ->
    options = []
    for x in [['$$','$$'], ['$','$'], ['\\[','\\]'], ['\\(','\\)']]
        options.push
            open  : x[0]
            close : x[1]
            mode  : CodeMirror.getMode(config, 'stex')
    console.log('gfm2', options)
    return CodeMirror.multiplexingMode(CodeMirror.getMode(config,

"gfm"), options...)

It's really _awesome_ how easy it is to do things like this using
CodeMirror.

William


Professor of Mathematics
University of Washington
http://wstein.org

@williamstein A bug in your solution is if I type a $ in a inline code or code block, it'll be highlighted as TeX.

like

$('#div').html();

See https://github.com/Moeditor/Moeditor/issues/5

Maybe we can find a way to set the "priority" of the marks?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Azmisov picture Azmisov  路  4Comments

blgm picture blgm  路  4Comments

mtaran-google picture mtaran-google  路  5Comments

droidenator picture droidenator  路  3Comments

hameddhib-dydu picture hameddhib-dydu  路  5Comments