12

Is there someway to escape brackets ([ and ]) so that they can be included in the description of an Org mode link? The following link doesn't work, for example:

[[http://mathoverflow.net/questions/195203/automorphisms-of-ideals-of-mathbbct][Automorphisms of ideals of C[t]]]

I hoped using a backslash (\[) would work or that there would be an org-entity, but that doesn't seem to be the case.

  • 1
    The problem is links in Org are parsed using regular expressions, and this task would be equivalent to recursion, which cannot be parsed with regular expressions. If you wanted to only add a one level of recursion, that would be doable by patching org-make-link-regexps, but in general that can't be done with the current setup. – wvxvw Jan 29 '15 at 21:45
  • Escaping can be done. You could try to adapt the relevant regexp org-bracket-link-regexp to handle this, though org-insert-link still wants to replace the brackets with braces and it may have other ramifications. – politza Jan 29 '15 at 21:50
  • Oh, I just remembered that \[ and \] are supposed to give displayed equations (like $$), not escaped brackets. – Omar Jan 29 '15 at 21:53
4

A working solution, not so pretty though, is to use the org mode Macros.

The below macros are replaced by the ASCII codes of the [ and ] when exporting to html or latex.

# Square Bracket Open [
#+MACRO: BO @@latex:\char91@@ @@html:[@@
# Square Bracket Close ]
#+MACRO: BC @@latex:\char93@@ @@html:]@@

[[http://emacs.stackexchange.com][{{{BO}}}Emacs SE{{{BC}}}]]

Reference

  • Thanks, but I was hoping to convince org-mode to render them as brackets inside the buffer. Your solution does of course work for export. – Omar Jan 29 '15 at 21:57
  • Doesn't work for HTML export. – Alex Mar 21 '18 at 0:48
3

Below is the modified version of org-make-link-regexp which will allow one nesting level of square brackets inside description:

(defun org-make-link-regexps ()
  "Update the link regular expressions.
This should be called after the variable `org-link-types' has changed."
  (setq org-link-types-re
    (concat
     "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
    org-link-re-with-space
    (concat
     "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
     "\\([^" org-non-link-chars " ]"
     "[^" org-non-link-chars "]*"
     "[^" org-non-link-chars " ]\\)>?")
    org-link-re-with-space2
    (concat
     "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
     "\\([^" org-non-link-chars " ]"
     "[^\t\n\r]*"
     "[^" org-non-link-chars " ]\\)>?")
    org-link-re-with-space3
    (concat
     "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
     "\\([^" org-non-link-chars " ]"
     "[^\t\n\r]*\\)")
    org-angle-link-re
    (concat
     "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
     "\\([^" org-non-link-chars " ]"
     "[^" org-non-link-chars "]*"
     "\\)>")
    org-plain-link-re
    (concat
     "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
     (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
    ;;   "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
    org-bracket-link-regexp
    ;; "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
    "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^[]*?\\[[^]]*?\\][^]]*?\\|[^][]+\\)\\]\\)?\\]"
    org-bracket-link-analytic-regexp
    (concat
     "\\[\\["
     "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
     "\\([^]]+\\)"
     "\\]"
     "\\(\\[" "\\([^[]*?\\[[^]]*?\\][^]]*?\\|[^]]+\\)" "\\]\\)?"
     ;; "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
     "\\]")
    org-bracket-link-analytic-regexp++
    (concat
     "\\[\\["
     "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
     "\\([^]]+\\)"
     "\\]"
     "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
     "\\]")
    org-any-link-re
    (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
        org-angle-link-re "\\)\\|\\("
        org-plain-link-re "\\)")))

But as noted above, this doesn't solve the problem of editing links (Org will still want to replace brackets with braces.) This also can only handle one nesting level of one bracketed group.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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