0

the following MWE shows how I have created a new type of environment called a codebox to store output in, so that I can format it inside a box with LaTeX. However, I don't know how to make Org Mode treat each #+begin_codebox ... #+end_codebox environment so that its contents are preserved verbatim.

This works:

enter image description here

Source:

#+TITLE: MWE
#+LATEX_HEADER: \RequirePackage{fancyvrb}
#+LATEX_HEADER: \DefineVerbatimEnvironment{verbatim}{Verbatim}{fontsize=\scriptsize}
#+LATEX_HEADER: \usepackage{mdframed}
#+LATEX_HEADER: \newenvironment{example}{\VerbatimEnvironment\begin{BVerbatim}[fontsize=\scriptsize]}{\end{BVerbatim}}
#+LATEX_HEADER: \newenvironment{codebox}{\VerbatimEnvironment\begin{mdframed}\begin{example}}{\end{example}\end{mdframed}}

#+begin_src R :session :results output org :wrap codebox
# Sample from the vector 'a' 1 element.
a<-c("a","b","c","d","e","f")
sample(a, 3)
#+end_src

#+RESULTS:
#+begin_codebox
[1] "b" "e" "d"
#+end_codebox

However, if I make a codebox that includes things like ~ or _, etc., those symbols are given a special interpretation by the org mode exporter.

This isn't what I want:

enter image description here

Source (to add to file above):

* Not so great

#+begin_codebox
This ~ is a test_of_the{codebox}
#+end_codebox
0

Well, I found a solution, but it does seem excessively clever!

Based on this: https://emacs.stackexchange.com/a/19941/8406

Key idea: wrap with example rather than codebox, then use built-in ability to alter the formatting of example blocks.

Installing some adapted elisp code from the linked answer:

(defun my-latex-export-example-blocks (text backend info)
  "Export example blocks as custom results env."
  (when (org-export-derived-backend-p backend 'latex)
    (with-temp-buffer
      (insert text)
      ;; replace verbatim env by 'results'
      (goto-char (point-min))
      (while (re-search-forward "\\\\\\(begin\\|end\\){verbatim}" nil t)
        (replace-match "\\\\\\1{results}"))
      (buffer-substring-no-properties (point-min) (point-max)))))

(add-to-list 'org-export-filter-example-block-functions
             'my-latex-export-example-blocks)

Updated MWE:

#+TITLE: MWE
#+LATEX_HEADER: \RequirePackage{fancyvrb}
#+LATEX_HEADER: \DefineVerbatimEnvironment{verbatim}{Verbatim}{fontsize=\scriptsize}
#+LATEX_HEADER: \usepackage{mdframed}
#+LATEX_HEADER: \newenvironment{contents}{\VerbatimEnvironment\begin{BVerbatim}[fontsize=\scriptsize]}{\end{BVerbatim}}
#+LATEX_HEADER: \newenvironment{results}{\VerbatimEnvironment\begin{mdframed}\begin{contents}}{\end{contents}\end{mdframed}}

* Example

#+begin_src R :session :results output org :wrap example
# Sample from the vector 'a' 1 element.
a<-c("a","b","c","d","e","f")
sample(a, 3)
#+end_src

#+RESULTS:
#+begin_example
[1] "c" "f" "e"
#+end_example

* This works now

#+begin_example
This ~ is a test_of_the{codebox}
#+end_example

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.