15

When MathJax is used within the alt text of an image, like this:

![test $2$][1]

the rendered result will show the MathJax 'outside' the image:

wrongly rendered image which displays 2"> after the site logo.

Interestingly, the preview renders the image correctly, presumably because it doesn't try to render the MathJax:

correctly rendered image


Related bug reports:

6
  • While an interesting bug, I wonder what the usecase for mathjax in an alt attribute is
    – Luuklag
    Commented Oct 2, 2020 at 6:32
  • 1
    @Luuklag See e.g. mathoverflow.net/a/357241 . (The triangle dimensions in the alt texts should be in MathJax, but this breaks the formatting due to the bug here.) Commented Oct 2, 2020 at 7:23
  • @EmilJeřábek I'd classify that as a nice to have, not a need to have. The alt could just as well be: "image of the mentioned triangle with 5 congruent triangles inside" as the dimensions of the triangle are already clearly defined in the text of the post.
    – Luuklag
    Commented Oct 2, 2020 at 8:04
  • @Luuklag With that logic, can you point me to any image whose alt text is a need to have? You can always just put the description in the surrounding text of the post. Commented Oct 2, 2020 at 8:11
  • @EmilJeřábek some people have a tendency to write very elaborate alt texts, but it is basically only needed to describe what is in the image. If yhat is also in the text you could refer to that, otherwise you need a more rlaborate alt text.
    – Luuklag
    Commented Oct 2, 2020 at 8:52
  • 1
    Is there any chance of this being fixed? I agree with others that $ in alt text isn't needed, but that's not the issue to me; this is the second time that I have tripped this bug and almost not noticed it because it doesn't show up in preview. I would be fine if I got a message like "Please do not use $ in alt text; it may appear incorrectly in the rendered post", but silently accepting it with every appearance that it will be fine tempts people into introducing errors in posts and having no idea that they've done so.
    – LSpice
    Commented Oct 24, 2021 at 13:54

1 Answer 1

8

Unfortunately, there are differences between the code that runs on the server to make the final version of a post and the code that runs in the browser during the preview. These were once pretty close to the same, but they have drifted apart over time.

There have been some recent changes to the server-side code in order to insert <span class="math-container">...</span> around MathJax delimiters (I'm not sure why, but I spect it is so that other parts of the system can recognize the math for things like hot questions and the difference displays for past edits). It seems that the code that is doing this is produce incorrect results, as the page you linked to includes

<img src="https://cdn.sstatic.net/Sites/math/img/logo.svg?v=2ad3f9715b74" alt="test <span class="math-container">$2$</span>"></p>

which is invalid HTML. The quotation marks for the math-container element end the quotation marks for the alt attribute, which means the > before $2$ is parsed as the end of the img tag, and so the $2$ now falls outside the alt text, and is displayed by MathJax. This doesn't happen in the preview because the math-container tags are not inserted during previews.

1
  • 2
    The "math-container" code is inserted to fix another bug, where the \newcommand directive was affecting other posts on the same page, in order to make sure that that directive only affects that one post. Commented Oct 2, 2020 at 7:29

You must log in to answer this question.

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