From org mode manual:
You can make words bold, /italic/, underlined, ‘=verbatim=’ and
‘~code~’, and, if you must, ‘+strike-through+’. Text in the code and
verbatim string is not processed for Org mode specific syntax, it is
exported verbatim.
Indeed ~> (cons 1 nil) \to (1)~
is exported as LaTeX \texttt{> (cons 1 nil) \textbackslash{}to (1)}
.
But this does suggest a workaround for LaTeX: use an export filter to undo the conversion of \
to \textbackslash{}
.
(defun my-latex-filter-allow-latex-in-code (text backend info)
"Undo backslash escaping"
(when (org-export-derived-backend-p backend 'latex)
(replace-regexp-in-string "textbackslash{}" "" text)))
(add-to-list 'org-export-filter-code-functions
#'my-latex-filter-allow-latex-in-code)
The export filter can be extended for handling html as well. Here's a first draft handling only \to
:
(defun my-filter-allow-latex-in-code (text backend info)
"Allow LaTeX symbols in code"
(cond
((org-export-derived-backend-p backend 'latex)
(replace-regexp-in-string "textbackslash{}" "" text))
((org-export-derived-backend-p backend 'html)
;; either extend this or find the html export function doing these conversions
(replace-regexp-in-string "\\\\to" "→" text))))
(add-to-list 'org-export-filter-code-functions
#'my-filter-allow-latex-in-code)
→
?