2016-01-30 Dimming out tildes in AUCTeX

I often write LaTeX in Emacs with AUCTeX. One thing I use a lot is tildes (these little ~ signs, which for LaTeX mean “unbreakable space”). This is justified; first of all, when writing in Polish, which has a few one-letter connectives (like i, which means and) or prepositions (like w, which means in), there is a well-known rule that you shouldn’t left them at the very end of the line. (While there is no such rule for English, I also avoid putting the indefinite preposition a at the end of the line. Probably I’m just trained to consider that ugly; OTOH, it is practically always tied to the next word, so it seems to make sense.) Secondly, when I write about maths (and I do a lot), I don’t like my lines to start with symbols, so I write e.g. things like consider the function~$f$ etc.

The problem with tildes is that they don’t look like spaces, and if you use them a lot, it becomes a bit more difficult to actually read the LaTeX source. There is a way to help with that, though: font-locking (it’s called syntax coloring in modern tools – but Emacs had that before it was cool, in fact even before it got its mainstream name!).

There is one problem with that approach. Font-locking is a messy business. I spent some time skimming through font-latex.el to see whether there exists some knob I could turn for that, but alas, either there isn’t one, or it is well hidden.

Fortunately, there is a universal (i.e., not LaTeX-specific) kind of knob for such things in Emacs: font-lock keywords. Of course, I don’t want my tildes highlighted (actually, dimmed) in modes other than TeX-related ones, so I’ll also need to use a hook.

  (add-hook
   'TeX-mode-hook
   (lambda ()
     (font-lock-add-keywords
      nil
      '(("~" . 'font-latex-sedate-face)))))

Interestingly, the second argument to font-lock-add-keywords is a mode, but most of the time you shouldn’t be using it. As the docstring says, if you use that instead of putting the whole thing in a hook (as above), you will only add your keywords to the mode you specified, not in any derived mode, which is most probably not what you want.

CategoryEnglish, CategoryBlog, CategoryEmacs, CategoryTeX, CategoryLaTeX