1

In LaTeX mode, the face font-latex-sedate-face highlights commands of the form \foo which are not known by AUCTeX. I'd like commands of the form \foo which appear inside a math environment such as \(\) to respect font-latex-sedate-face. In a math environment, the face used is font-latex-math-face. The command C-u C-x = with the cursor over a \foo inside a math environment shows me both faces are used:

There are text properties here:
  face                 (font-latex-math-face font-latex-sedate-face)

I've tried using the ( ... &override) when customizing such faces but without success. What would be the correct way to make one face have priority over another? If I checked it correctly, none of those faces uses the overlay mechanism, so it should be possible to make font-latex-sedate-face override fojnt-latex-math-face instead of the other way around.

2
  • 1
    It depends on which is added first, but you could play around with the OVERLAY flag and the values append and prepend. Commented Dec 8, 2021 at 10:16
  • Thanks, @Lindydancer. I'll try those.
    – Tera
    Commented Dec 8, 2021 at 11:22

1 Answer 1

1

I ended up editing the AUCTeX file font-latex.el. I did not find a way to change this via my personal configuration file. Roughly, I had to (1) alter the order in which "ocurrences" of font-latex-sedate-face and font-latex-math-face are added in a list and (2) change what the function font-latex-match-simple-command finds by editing the variable font-latex-match-simple-exclude-list. When the editing of font-latex.el is done, I needed only to recompile the package. [If you are a Doom Emacs user, running doom build (usually run after upgrading Emacs) takes care of that.]

(1) In font-latex.el, in the end of the definition of the function font-latex-make-user-keywords we have:

  (dolist (item
           '((font-latex-match-math-env
              (0 'font-latex-warning-face t t)
              (1 'font-latex-math-face append t))
             (font-latex-match-math-envII
              (1 'font-latex-math-face append t))
             (font-latex-match-simple-command
              (0 'font-latex-sedate-face append))
             (font-latex-match-script
              (1 (font-latex-script (match-beginning 0)) append))
             (font-latex-match-script-chars
              (1 (font-latex-script-char (match-beginning 1)) prepend))))
    (add-to-list 'font-latex-keywords-2 item t))

I reordered this so that the result of the search done by the function font-latex-match-simple-command receiving the face font-latex-sedate-face takes precendence over the result of the searches font-latex-match-math-env and font-latex-match-math-envII receiving the face font-latex-warning-face.

Remark: changing append to prepend in

font-latex-match-simple-command
 (0 'font-latex-sedate-face append))

does force font-latex-sedate-face precedence over font-latex-math-face. However, it establishes precedence over other faces which we would not like it to override such as comments or the \foo commands known by AUCTeX.

(2) I had to edit the variable font-latex-match-simple-exclude-list adding parentesis and brackets to it:

(defvar font-latex-match-simple-exclude-list
  '("%" "-" "," "/" "&" "#" "_" "`" "'" "^" "~" "=" "." "\"" "(" ")" "[" "]"))

The reason is to avoid TeX excerpts like \(X\) being incorrectly fontified with font-latex-sedate-face.

10
  • 1
    So you have basically "forked" AUCTeX: that's fine, although there are some downsides, the main one being that you will now have to carry those modifications forward after every update of AUCTeX, which is extra maintenance for you. I don't know if it is possible, but you might want to submit your changes to the project and even if they don't accept them, they might be able to either tell you how to do what you want or make whatever modifications they need to in order to accommodate your use case (after all, if you have a problem, then it is possible that other people will have the same ...
    – NickD
    Commented Dec 8, 2021 at 15:46
  • ... problem). If that happens, then you are off the hook: you don't need to worry about carrying those changes forward and your maintenance tasks become that much lighter.
    – NickD
    Commented Dec 8, 2021 at 15:47
  • You're right. I wondered about submitting this to AUCTeX but it would be a significant change to the current feel of TeX syntax highlighting. I just tried to implement this because I was used to the way many other editors highlight tex math environment. The delimiters \( \) \[ \] are set in a different color from the math contents and in the contents commands such as \hat or \frac are also colored differently from simple variables, X, a, etc. The default AUCTeX behaviour is to have everything with the same color, for example, \(\hat{X}\) would have a single face
    – Tera
    Commented Dec 8, 2021 at 19:09
  • As I could not manage to change the current behaviour via my private config, I believe my changes do not give a way to reset the current behaviour via private config either since setting the same text properties for font-latex-math-face and font-latex-sedate-face would make unknown commands outside math environments be displayed with the same face as math stuff. I believe some users would be bothered by that solution.
    – Tera
    Commented Dec 8, 2021 at 19:11
  • But it would indeed be good to have this merged: I imagine many users coming from other editors would like that and not needing to worry about carrying those changes forwards. I'll see if I can contact one of the maintainers to ask what they think instead of sending a pull request directly.
    – Tera
    Commented Dec 8, 2021 at 19:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.