57

In Org-mode I can surround text with = or ~ to place text in inline verbatim or code mode.

However this fails if the text has quotes, and I couldn't find an explanation or workaround in the documentation.

For example:

  • =hello= Renders as verbatim
  • ~hello~ Renders as code
  • =hello'= Fails to render as verbatim
  • ~hello'~ Fails to render as code

The only solution that I found so far is inserting invisible characters, e.g. typing C-x 8 <RET> 200b <RET>, as explained in this question.

Why is this (is it a bug?) and how can I avoid this problem without inserting invisible characters?

Amelio Vazquez-Reina
  • 5,157
  • 4
  • 32
  • 47

3 Answers3

66

First of all: This is Emacs. It's not a bug, it's a setting!

Secondly, @glucas is correct in saying that you need to modify org-emphasis-regexp-components to get what you want. However, I'd like to suggest two modifications to his (@Malabarba's) code and provide a bit more context:

  1. You don't need to copy the entire value of org-emphasis-regexp-components to your init-file to modify a single component. For your use case the following is sufficient:

    (setcar (nthcdr 2 org-emphasis-regexp-components) " \t\r\n,\"")
    
  2. You don't have to modify org-emphasis-regexp-components before loading org-mode via (require 'org). Just add the following line after your modifications:

    (org-set-emph-re 'org-emphasis-regexp-components org-emphasis-regexp-components)
    

More context

In case you want to customize other aspects of how emphasis markup works in org-mode, know this:

The value of org-emphasis-regexp-components is a list with five entries.

  1. The first entry controls which characters are allowed to immediately precede markup characters. If you want to be able to have something like why=hello= render correctly, you'll need to modify this entry.

    (setcar org-emphasis-regexp-components "...")
    
  2. The second entry controls which characters are allowed to immediately follow markup characters. If you want to be able to have something like =hello=there render correctly, you'll need to modify this entry.

    (setcar (nthcdr 1 org-emphasis-regexp-components) "...")
    
  3. The third entry specifies the characters that are not allowed as border characters, i.e., characters that immediately follow an opening markup character or precede a closing markup character. You'll need to modify this to make things like ='hello'= render correctly.

    (setcar (nthcdr 2 org-emphasis-regexp-components) "...")
    
  4. The fourth entry lists characters that are allowed in the body of your marked up string, i.e., characters that appear between the border characters. You'll rarely need to modify this; by default, any character is allowed as a body character.

    (setcar (nthcdr 3 org-emphasis-regexp-components) "...")
    
  5. The fifth entry specifies how many newlines are allowed inside a marked up expression. By default, org-mode allows a single newline. So if you want to be able to add markup to text that spans more than two consecutive lines, you'll need to modify this entry.

    (setcar (nthcdr 4 org-emphasis-regexp-components) N)
    

    ... where N is the number of newlines you want to allow.

Related posts

People seem to run into this issue fairly frequently. Aside from the post you mentioned, there are at least two more questions on StackOverflow that deal with very similar issues:

itsjeyd
  • 14,586
  • 3
  • 58
  • 87
  • 9
    I upvoted your answer but would also like to commend on the quality of the answer. – Kaushal Modi Jul 09 '15 at 16:53
  • Thanks @itsjeyd This works great If I run those two lines interactively once Emacs has fully loaded with my buffers already open. However, and strangely enough, I run into `"Symbol's value as variable is void: org-emphasis-regexp-components"` even if I place those two lines all the way at the bottom of my `.emacs` file. Any thoughts on why that could be the case? – Amelio Vazquez-Reina Jul 13 '15 at 13:17
  • 1
    @AmelioVazquez-Reina You're welcome :) Just to make sure: Do you `(require 'org)` in your `.emacs` file before making any customizations to `org-emphasis-regexp-components`? – itsjeyd Jul 13 '15 at 20:24
  • Thanks @itsjeyd I can't believe that was the problem. I was not `(require 'org)`. Strangely enough, I was still able to work with org files without problem. Maybe that's because they were already open and I was using the desktop feature? (which I believes recalls what package to load on what buffer)? – Amelio Vazquez-Reina Jul 14 '15 at 17:36
  • 1
    @AmelioVazquez-Reina I'm glad everything is working properly for you now :) Your observations are correct -- you don't need to `(require 'org)` in your init-file to be able to use it. If you don't, Emacs will load it automatically when you: (a) open an `.org` file for the first time in the current session, or (b) enable the mode manually via `M-x` `org-mode` for the first time in the current session, or (c) when it restores a saved desktop that includes at least one `.org` file. (contd.) – itsjeyd Jul 15 '15 at 05:51
  • 1
    @AmelioVazquez-Reina (contd.) **The thing is**: Emacs *won't* load `org-mode` automatically just because you put some customizations for it in your init-file. That's why you need to `(require 'org)` before customizing any of its variables. Without fully loading the mode, Emacs won't know what to do with variables like `org-emphasis-regexp-components`; they are not defined when Emacs first encounters them, hence the `Symbol's value as variable is void` message. – itsjeyd Jul 15 '15 at 05:58
  • Any thoughts on why this wouldn't take effect when exporting to LaTeX/PDF? – phils Oct 13 '15 at 06:58
  • This does not seem to work any longer. I even see, that I upvoted it earlier, so it must have worked for me at some point. However, now it does not. I have the require and then the 2 lines suggested, but it does not work. Even after reloading the org buffer, ending with a double quote is a no-go. – Zelphir Kaltstahl Sep 22 '19 at 11:08
  • Correction: It does seem to work, but only, if after modifying your `init.el` file, you close all org buffers, close Emacs and then start it again and reopen the org buffer of that file. Total wipe, otherwise no go. – Zelphir Kaltstahl Sep 22 '19 at 11:30
16

There's a much simpler option: use inline source blocks:

src_python{hello'}

This works without problems for characters like quotations marks, and it provides syntax highlighting for inline code.

To include the inline source code when exporting, you'll want to add this declaration to the header:

#+PROPERTY: header-args :exports code
Shon
  • 157
  • 1
  • 9
Clément
  • 3,924
  • 1
  • 22
  • 37
8

You can remove the quote characters from org-emphasis-regexp-components as described by @Malabarba in this blog post.

The relevant code from that post:

;; This HAS to come before (require 'org)
(setq org-emphasis-regexp-components
      '("     ('\"{“”"
        "-   .,!?;''“”\")}/\\“”"
        "    \r\n,"
        "."
        1))

See the doc string for org-emphasis-regexp-components: by default the quote characters are considered:

border - The chars forbidden as border characters.

glucas
  • 20,175
  • 1
  • 51
  • 83