Fix some issues with data-latex handling and update tests to accommodate changes. (mathjax/MathJax#3459) #1385
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR fixes a number of problems with the
data-latexattribute. Most notably, it restores missing initial characters in several situations, like numbers in subscripts (e.g.,a_{123}), the initial motivation for this update. But it also adds missing backslashes in some cases (likea_\perp), missing macros like\beginin\begin{array}, and some other similar situations. Additionally, it removes unneeded spaces at the end of the attribute (but keeps them for\). Finally, it removes thedata-latex-itemattributes in thecleanAttributesfilter.All of this necessitated updates to the test files. Most of the changes are due to the removal of
data-latex-itemattributes, including of the missing initial characters, and the trimming of trailing spaces. There are a few substantive changes, mostly in thephysicspackage, where things like\diffdend up as{\rm d}instead, so I hope that is OK. Because some of those macros go through several other macros, it is hard to decide which one they should show.The most significant changes to the code are in
TexParser.ts, in theupdateResult()method, particularly the handling of super- and sub-scripts, which was the source of most of the problems, but there are also changes in a couple of other places.The main issue was in the
parse()method, wherethis.saveIis being set tothis.i, butthis.iis already past the character that initiated this parse method, so when that is used to get the substring used fordata-latex, it is one character short. So I've subtracted a conditional 1 (subtract when a character other than&, since we don't want&as part of thedata-latexfor the cell, and some instances of thecasesenvironment frommathtoolswould generate that). Right now, this is pretty ad hoc, and it probably needs a better mechanism where the handler (or maybeinput) tells whether to subtract 1 or not, but for now this does the trick.The other change to
parse()is to not callupdateResult()when the input is a macro. This was what was casing macros to be missing their backslashes in super- and subscripts, because the string would be just the macro name, and they would not be updated later when the backslash came through because the super- or subscript was not adjusted once it got a value. A macro first callsparse()for the backslash, which then callsparse()for the macro name, and sinceparse()callsupdateResult()after handling the input, the macro callsupdateResult()first, then the backslash calls it. So we will get the full macro name if we wait for the backslash to callupdateResult()rather than calling it early with the macro name.Given that, the main changes are in the
updateResults()method. You'll probably want to look at this function as a whole rather than trying to read the diffs. I'v e added comments that I hope will tell you what is what, but the main problems were in how_and^were handled. Rather than have the building of those latex strings depend on the currently saved latex being_or^(which will only be the case once, but sometimes we need to adjust the latex several times), I have used a property to say which part is being processed. (That necessitated a change toNodeUtil.copyAttributes(), which was usingsetProperties()to copy the properties; butsetProperties()sets attributes as well as properties, and that lead to properties becoming attributes unexpectedly.)The
mml()function is modified to only set thedata-latexattribute if there actually is a value for it, and atrimTex()method is added that trims spaces from the beginning and end of a latex string, but retains the final space if it is needed for a\macro. Finally, thecomposeLatex()method caches the value ofTexConstant.Attr.LATEXso as to reduce the references to it.The
data-latex-itemattributes are removed in thecleanAttributes()function inFilterUtil.ts, after they are no longer needed. To be honest, I'm not sure how they are actually being used, as they only seem to be used inupdateResult()in obtainingexisting, and sincedata-latexis set wheneverdata-latex-itemis, I'm not sure why you can't use that instead. I suppose there are times when a node hasdata-latexwithoutdata-latex-item, so that might be it. I haven't tried removing it.In
ParseMethods.ts, I moved two lines so that they follow an early return that didn't need them.I moved the
addLatexItem()function from a static function inBaseItems.tsto a method in theStackItemclass. That way, it can be called from other items, if needed, and can be overridden if need (I wanted to be able to do that while testing a patch for the poster of the issue, but couldn't).Also in
BaseItems.ts, lines 791-794 were collapsed since the?are unneeded. (I think I made that change in a previous PR, but it snuck in here, too.)Finally, in
BaseMappings.ts, I combined some strings that used to be separated for line breaks, but prettier put them on one line, so no need to have them as two strings added. I also used substitution strings for one where that helped.This resolves issue mathjax/MathJax#3459.