Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Math formatted columns in tabular environment handled incorrectly #155

Open
shardulc opened this issue Nov 15, 2017 · 6 comments
Open

Math formatted columns in tabular environment handled incorrectly #155

shardulc opened this issue Nov 15, 2017 · 6 comments

Comments

@shardulc
Copy link

This issue is about a very specific use case but I think it is common enough to make an issue. The screenshot below demonstrates the problem.

image

That is, when specifying columns to be in math mode using the tabular environment of the array package, you have to write >{\(}c<{\)} but the syntax highlighter does not recognize the \) that ends math mode. Perhaps this occurs because the { starts a new scope before \) but I do not know how the highlighter works. Can someone please take a look at it?

@Aerijo
Copy link
Collaborator

Aerijo commented Nov 26, 2017

You're right, the issue is caused by the curly brackets containing the end mathmode delimiter. As curly brackets are scoped to their own rules, their contents are effectivlely hidden from any outside rules. For example, look at the following (broken) latex:

\( this is math {\)} this is still math \) this is not math

Pasting that into atom will display the same issue as yours.

Unfortunately, it is difficult to avoid this behaviour. A possible general solution would be to stop scoping individual appearances of{ and }, but that could be annoying for other reasons. In your case, the begin{tabular} grammar could be extended to look for an opening curly brace immediately following the begin statement. If it finds one, it will scope contents as verbatim until the next (balanced) closing curly bracket. This would not fix the issue I provided, but it would fix yours.

@yudai-nkt I don't mind the second option. It's not the best solution, and could lead to invalid highlighting for other reasons, but I think mathmode tables are common enough to justify it.

@Aerijo
Copy link
Collaborator

Aerijo commented Dec 6, 2017

Alternatively, mathmode could be optionally ended by an (unbalanced) }. As far as I can tell, it is never valid to end mathmode in a different group to the one it begins in. For example, the following is valid (using \text fromamsmath; $ can be replaced by \( and \)):

text $ math $
text $ math { still math } $
text $ math { still math \text{text $ deeper math $ }} $

but the following will break

text $ math {$}
text $ math {$ deeper math $} $
text {$} math {$}

Therefore, I believe that ending mathmode with } would be fine, as if the user has not already balanced it, they either are doing something like for the table, or are going to get an error anyway. As we cannot reliably predict which is the case, I would say letting the user run into the error is preferable to ruining syntax highlighting for the rest of the document.

However, this approach hinges on being able to reliably balance { and }. Otherwise, highlighting would break on some of the valid examples. As it is, I believe the current scoping will hide matching {} pairs from the math pattern, but would need to test to confirm.

And whatever fix is implemented, this is the kind of thing that needs ... shudders in apprehension ... specs.

@Aerijo
Copy link
Collaborator

Aerijo commented Dec 6, 2017

End result would look something like this:
screen shot 2017-12-06 at 7 39 55 pm

screen shot 2017-12-06 at 7 46 08 pm

This was done by just adding |(?=\\}) to the end pattern of the mathmodes.

(Interesting to note: if the end math command occurs by itself, it will only be scoped as a generic control symbol)

@shardulc
Copy link
Author

shardulc commented Dec 7, 2017

@Aerijo Isn't there any way to simply ignore scopes when trying to start and end math mode, just highlighting everything between \( and \) regardless of what comes in between? While typesetting math, it is anyway possible that some notation requires an unmatched {/}, so this is bound to come up again.

@Aerijo
Copy link
Collaborator

Aerijo commented Dec 7, 2017

It's true that math can need unbalanced {/}, but these would need to be escaped (like \{), and that is already handled properly. I would not like it ignore everything within math mode, as I like seeing \frac highlighted differently to the product frac.

screen shot 2017-12-07 at 12 27 58 pm

Plus, it would still look wrong in your example, as the }c<{ would appear as math (which I don't think it is).

@Aerijo
Copy link
Collaborator

Aerijo commented Dec 14, 2017

@yudai-nkt What do you think about this? I've yet to find a problem with it, and it's quite easy to implement. It serves as a sort of 'failsafe', such that even if the construction is valid (like in this issue), the error does not propagate across the file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants