4

In my answer to "listings - How to expand special characters inside lstinline?" I have two almost identical code-snippets. The only difference is that the first code-snippet additionally contains the command \normalfont.

Syntax-highlighting produces different coloring.

Why?

enter image description here

6
  • Shouldn't this be in the main list, not in meta? Commented Jan 19, 2022 at 14:59
  • 1
    The \normalfont is unrelated. You would have the same syntax highlighting with two identical code blocks. The syntax highlighter just does not notice that your first block is finished. Commented Jan 19, 2022 at 16:07
  • 1
    @barbarabeeton I think it is correct here. It is an erroneous rendering on the site, not of LaTeX output.
    – CarLaTeX
    Commented Jan 19, 2022 at 16:09
  • 1
    To report problems with the syntax highlighting: github.com/highlightjs/highlight.js/issues Commented Jan 19, 2022 at 16:16
  • MWE to reproduce the incorrect highlighting: Text \verb{test} Text (the "Text" shouldn't be marked as math code) Commented Jan 19, 2022 at 16:35
  • 5
    It seems that highlight.js knows that \lstinline is a verbatim macro, but doesn't know it accepts an optional argument, so it treats the following character ([) as a begin-of-verbatim delimiter, and stops verbatim mode at the next [. Not sure if a bug or a feature. Same for \verb: the highlighter doesn't know that \verb{test} is valid, so it thinks it has to stop only when it finds another { Commented Jan 19, 2022 at 22:10

1 Answer 1

7

Author of the highlight.js grammar for LaTeX here. Phelype Oleinik identified the issue correctly in his comment: The grammar doesn't know about the possible optional argument of \lstinline.

A bit of history: Back in 2020 the powers changed the SE parser to highlight.js without much warning which totally broke all highlighting here. After collecting issues, I completely rewrote the grammar and that is pretty much still what we have today. Sadly, the powers weren't very responsive when it came to adapting the colors to match the old site theme, even after repetitive prodding. Maybe it's worth trying again, but I don't have high hopes. I say all this just to clarify that even if the issue is resolved in highlight.js, there is no guarantee that the fix will be used here any time soon.

Now, for the issue at hand. Feel free to open an issue on the highlight.js issue tracker and ping me there. I'm currently rather swamped but I can try to find some time to look at it. One difficulty is that LaTeX has quite a varied syntax and covering all of it is impossible (and out-of-scope for highlight.js). I tried to cover the most common verbatim cases back then, as that will otherwise often mess up highlighting, but obviously not all of it.

Another difficulty is that putting all the possible sequences of arguments into a highlight.js grammar is rather cumbersome due to it's mode of operation. I don't quite remember the limitations of what I came up with back then. I'll have to check if this can be easily fixed. There was already quite some discussion about adding a feature to hightlight.js that would make this much simpler (and I was kind of hoping that would be available when I finally sat down and went through my list of improvements for the grammar), but it isn't here yet.

If you decide to open an issue over on GitHub, it would be great if you could include a list of all the common verbatim environments with all the possible usages. That way we could discuss beforehand what makes sense to include and then try to address all of it at once. Maybe it even reignites the sequence discussion. You are of course welcome to contribute to this yourself.

2
  • Phew, this got quite long. I expect similar questions in the future and hope this is a place to point them. Honestly, I'm surprised it took so long for an issue like this to be brought up (or maybe I just missed it before).
    – schtandard
    Commented Jan 25, 2022 at 10:42
  • 2
    Maybe it is also useful to mention the fact that when an improvement is made to highlight.js this is not automatically included on the Stack Exchange websites. There was a request to incorporate updates automatically but it seems currently new versions are only applied manually. On the positive side they are being applied so improvements are not lost on SE (and obviously also useful for other websites using highlight.js).
    – Marijn
    Commented Jan 25, 2022 at 13:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .