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

Problems syntax coloring TeX and LaTeX files #1088

Closed
edreamleo opened this issue Feb 28, 2019 · 9 comments
Closed

Problems syntax coloring TeX and LaTeX files #1088

edreamleo opened this issue Feb 28, 2019 · 9 comments
Assignees
Labels
Milestone

Comments

@edreamleo
Copy link
Member

edreamleo commented Feb 28, 2019

From this thread:

  • .tex files imports create @language latex directives.
    [EKR]: The .tex and .latex extensions should be not be treated differently, which may mean adding or changing existing importers.

  • The constructs \lae, \cbstart and others color as expected, but words after the closing \ are improperly colorized. This needs fixing, because the "closing" "\ " designates [a real?] macro, which ends with the blank (and expands to a blank), since latex macros gobble up any trailing blank. I have such "\ " (backslash followed by a blank) macros all over the text.

@edreamleo edreamleo added the Bug label Feb 28, 2019
@edreamleo edreamleo self-assigned this Feb 28, 2019
@edreamleo edreamleo added this to the 5.8.1 milestone Feb 28, 2019
@dalcolmo
Copy link

dalcolmo commented Mar 1, 2019

"The .tex and .latex extensions should not be treated differently. tex and latex share the same syntax. Indeed, latex is tex with some additional macros.

@dalcolmo
Copy link

dalcolmo commented Mar 1, 2019

clarification to the "\ ": yes this is a real macro, which expands to a (non-reducible) blank.
Most latex macros start with a backslash and then consist only of letters, which means the macro ends before any special character, before any number and before any blank etc.

However every latex macro consumes a blank that may follow, which is why you have to use such a non-reducible blank after a macro, if you want a blank in the output.

I would say the rule for highlighting macros should be kept simple: any backslash followed by any sequence of (upper- or lowercase) ASCII letters should be a considered a macro.
Exceptions to this rule may be some single-character macros like "\\" and "\_", but I would not even bother highlighting them.

Alternatively one could highlight only the backslash and the first character after the backslash if the first character after the backslash is not an ASCII letter. This would cover all single-character macros.

Of course, with these simple rules one cannot know the difference between a valid macro and something which just resembles a macro, but is not defined - but there is no easy way to achieve that.

It is possible to define macros in latex, which defy these rules, but in that case it becomes impossible to decide which is a macro and which is not. Some macros are only valid inside certain environments, for example in math mode. There are some tech editors which try to scan all \newcommand, \def, ... , but that too is prone to fail because there are many ways to define a new macro in latex. It would require to scan all loaded packages too. Fortunately most user commands follow the simple rule I named above and I would just leave it at that.

What may be much more useful than a complicated syntax highlighter is highlighting the brace pair, parenthesis or brackets which are immediately before the cursor, as long as the cursor is there, not just flashing the pair shortly when typing a closing brace.

  • Josef

@edreamleo
Copy link
Member Author

edreamleo commented Mar 1, 2019 via email

@largo84
Copy link
Contributor

largo84 commented Mar 14, 2019

I agree with Josef that tex and latex syntax should work the same way. Possible solution would be to have only one @language directive (suggest tex) and only one mode file, tex.py. Implications of that solution:

  • Change importers to only use the valid @language tex.
  • Might cause confusion for users who already have @language latex if it goes away.
  • Only one file to maintain, not two.

Is it possible to have Leo refer both @language tex and latex to one mode file?

@dalcolmo
Copy link

dalcolmo commented Jun 23, 2019

I would like this to be re-opened. Although pygments coloring does a decent job on latex code, I mix many different languages in the same Leo file, and find Leo's own syntax coloring better for languages other than latex. So, why not fixing the syntax highlighting for latex in Leo, and stick with that colorizer?

The coloring of tex/latex on Leo's own colorizer seems based on a rather incomplete command list. Such a list is always going to be incomplete, because latex allows to define new commands via various mechanisms. Therefore, either one allows the user to easily extend the list (globally, and per-project separately, since the valid command list may be different for each project), or one bases the coloring on a simple rule instead of a list of commands. TexStudio tries to figure out the command list, by scanning the code (also in loaded packages), but this does not work for me as I also \input several files which define (lots of) new commands, and TexStudio does not seem to follow these.

The simplest coloring of latex commands would be to color everything that starts with a backslash and ends with a non-alphabetic, non-ascii character or whitespace. That non-alphabetic, non-ASCII character termination character should NOT be highlighted any more (it does not belong to the command).

EKR: The following should be easy enough to do with regex-based rules.

A command will start with a backslash followed by either:

  1. One or more characters in the set [a-zA-Z], or,
  2. Exactly one non-alphabetic, non-ASCII character.
    This single character should be highlighted too, because this covers the single-character macros.

This would not cover all possible latex commands and this would highlight some stuff which isn't defined as a latex command (e.g. a misspelling), covering everything right would be an enormous task, so let's keep it simple.

All the other highlighting for tex/latex (e.g. comments, ...) should remain as it is.

@edreamleo
Copy link
Member Author

@dalcolmo Ok. I'll put this on the list for 6.1

@edreamleo edreamleo reopened this Jun 23, 2019
@edreamleo edreamleo modified the milestones: 5.9, 6.1 Jun 23, 2019
@edreamleo
Copy link
Member Author

edreamleo commented Jun 23, 2019

From the TeX FAQ: "Macro names are conventionally built from a \ followed by a sequence of letters, which may be upper or lower case (as in \TeX, mentioned above). They may also be any single character, which allows all sorts of oddities, [including] \space meaning “insert a space here”.

This seems clear enough. The point is that we can ignore "unconventional" macros.

@edreamleo
Copy link
Member Author

@largo84 Yes, it would be possible to use a single file for both TeX and LaTeX. It would involve a special case in Leo's colorizer, but that's no big deal.

@edreamleo edreamleo modified the milestones: 6.1, 6.0 Jun 23, 2019
edreamleo added a commit that referenced this issue Jun 23, 2019
Both @language tex and @language latex use leo/modes/tex.py.

Only slight changes were needed to lex.py.

I'll retain leo/modes/latex.py: conceivably it may be useful later.

- Added special case to bjc.init_mode.
- Added jedit.match_tex_backslash.
@edreamleo
Copy link
Member Author

edreamleo commented Jun 23, 2019

Completed at 5e467b6. The highlights:

  • Both @language tex and @language latex use leo/modes/tex.py. This ignores the many special cases in latex.py. I'll retain leo/modes/latex.py in case this choice becomes controversial.
  • Only slight changes were needed to tex.py. Similar changes could be added to latex.py.

Code changes:

  • bjc.init_mode makes @language latex be equivalent to @language tex.
  • The new jedit.match_tex_backslash easily handles the nuances of conventional latex macros.

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

No branches or pull requests

3 participants