# Org-mode overline emphasis not able to be followed by other characters

I use a lot of overline (in statistical formulae) and so I have the following in my .emacs:

(custom-set-variables
'(org-emphasis-alist
(quote (("*" bold)
("/" italic)
("_" underline)
("=" org-verbatim verbatim)
("+" (:strike-through t))
("~" (:overline t) verbatim))))
'(org-hide-emphasis-markers t)
'(org-pretty-entities t))


In other words, I want ~ to define overline and I want it to render in-line in Emacs.

This works great, however I cannot follow that emphasis (or any emphasis for that matter) with more characters such as superscript or subscript numbers. For example, ~X~_1 renders as literally ~X~ followed by a subscript 1. The X is overlined until I type the underscore, then the emphasis tildes reappear.

I assume the solution has something to do with modifying org-emphasis-regexp-components and maybe use of a zero-width space between the emphasis and the subscript, but there I get lost.

Also I would love my PDF exports to render the same (at the moment, since I am using the ~ for overline, my overlined segments get rendered as code) so for a part 2, if we can update the PDF export to render overlines properly too, that would be amazing.

You are right about having to modify org-emphasis-regexp-components. In particular, you have to add _ and ^ to the regexp for the post component (be careful where you add them: leave the dash at the very beginning and leave the opening square bracket at the very end - I added them after the initial dash below):

  #+begin_src emacs-lisp
(setf (nth 1 org-emphasis-regexp-components) "-_^[:space:].,:!?;'\")}\\[")

(setf (nth 4 org-emphasis-alist) '("~" (:overline t) verbatim))

#+end_src


After setting the latter, you need to reload Org mode as its doc string states: M-x org-reload RET.

That should take care of subscripts and superscripts in the buffer.

For PDF rendering, a possible implementation is to replace the current rendering of the code markup with an overline rendering. The relevant data structure is org-latex-text-markup-alist: we replace the code element with one of our own devising:

  #+begin_src emacs-lisp
(require 'ox-latex)

(delete (assoc 'code org-latex-text-markup-alist) org-latex-text-markup-alist)

That uses the \overline macro (which is only defined in math environments, hence the \ensuremath). That seems to work in the simple cases in your question. No guarantees however: there may be more complicated cases that break this.
The require might be needed in order to pick up the definition of the variable org-latex-text-markup-list.
• Ah, you may be running an older version of Org mode that does not have that variable. What version are you running? M-x org-version RET will tell, C-u M-x org-version RET will insert it in a buffer so you can cut-and-paste and you should update your question with that information ... or you might need to (require 'ox-latex) to get the definition of the variable. – NickD Sep 7 '20 at 20:23
• The require will probably resolve it: the variable was introduced way back before 8.3.7. – NickD Sep 7 '20 at 20:37