1

In src blocks, I can use LaTeX packages to make them look nice. For example, LaTeX's minted package lets me put a nice frame around my code blocks. The syntax is

#+ATTR_LATEX: :options frame=single
#+begin_src python
<Your Code Here>
#+end_stc python

But the same does not work with an example block. How can the same – that is, a box around my example – be achieved in an example block? I don't mind if this uses minted or not.

2
  • You should look at the TeX file that is produced by the exporter and then try to figure out how to change your Org mode file to produce what you want. Keep in mind that Org mode does not know anything about LaTeX: the exporter does a good job for the simplest documents, but if you want anything more complicated, you mayl have to go beyond it (by using the methods described in [ Advanced Export Configuration](orgmode.org/manual/Advanced-Export-Configuration.html) or by writing a derived exporter or by manually tweaking the LaTeX file that the exporter produced.)
    – NickD
    Commented May 17 at 22:25
  • ... or by advising a function as the answer below shows.
    – NickD
    Commented May 19 at 4:17

2 Answers 2

2

The other answer describes how to change a verbatim environment to a combined framed and verbatim environment, by changing the code of the Org mode exporter (or a couple of other methods that I have not finished yet).

This answer describes a much shorter method that solves the problem on the LaTeX side, without changing any code on the Org mode side. However, it requires some non-trivial LaTeX trickery. I initially tried to create a framedexample environment (a combination of a verbatim environment inside a framed environment) in LaTeX, but verbatim is a peculiar environment - Lamport calls it anomalous in the LaTeX book - because once LaTeX sees \begin{verbatim} it takes everything to be in that verbatim environment until it sees the 11-character sequence \end{verbatim} explicitly in the TeX file, so you cannot hide it inside a framedexample environment. At least, that's what I thought at the time: I gave up that investigation and concentrated on modifying Org mode to do it explicitly.

But I read the verbatim doc and I finally figured out how to define a framedexample environment that works. You create a framedexample.sty file in the same directory as the TeX file with these contents:

\usepackage{verbatim}

\newenvironment{framedexample}{\framed\verbatim}{\endverbatim\endframed}

using the individual commands to start/end the environments, not the environments themselves. That was one line of LaTeX code that took me a week to figure out :-(

But given this, the Org mode side is a breeze:

#+LATEX_HEADER: \usepackage{framedexample}

* Example

#+ATTR_LATEX: :environment framedexample
#+BEGIN_EXAMPLE
This is
an example
block in
Org mode.
#+END_EXAMPLE

We add a LaTeX header to load the framedexample "package" (the 2-line framedexample.sty file shown above) and then we make sure that the example block creates a framedexample environment in the exported TeX file - and that's all: it works fine.


You can of course relocate the framedexample.sty file to another place that your TeX installation knows about, which you should certainly do if you want to use it widely. You can find out what those places are with this command:

kpsewhich -show-path=tex

at least on TeXLive (I don't know anything about othe TeX distros - not even if they provide kpsewhich). You will need to remake the indices with texhash.

1

Here's an Org mode file that implements a "global" change, by adding an advice to the function that processes example blocks in the LaTeX exporter. Some explanations are provided in the file itself, but if you have problems with it, ask. This is not the only way to solve the problem and I'm planning to add a couple more in the coming weeks, so if you are tempted to accept an answer, you might want to wait until you see some other possibilities.

* Example

#+BEGIN_EXAMPLE
This is
an example
block in
Org mode.
#+END_EXAMPLE

* Code                                                            :noexport:

Run the following code block to add an advice to
=org-latex-example-block= that encloses the "normal" =verbatim=
environment output in a =framed= environment. To test, export this file
normally with =C-c C-e l o=.

If you want to revert to the original form, put your cursor after the
closing parenthesis on the commented-out =advice-remove= line and
press =C-x C-e=.

#+name: advice
#+begin_src elisp :results drawer
    (defun ndk/org-latex-example-block (orig_fun &rest args)
      (concat "\\begin{framed}\n" (apply orig_fun args) "\n\\end{framed}\n"))

    (advice-add 'org-latex-example-block :around #'ndk/org-latex-example-block)
    ;;; (advice-remove 'org-latex-example-block #'ndk/org-latex-example-block)
#+end_src

#+RESULTS: advice
:results:
nil
:end:

To test, run the following code block. It parses the example block
above and passes it to =org-latex-example-blcok=.  Before you add the
advice, it will produce the results shown. After you add the advice,
run it again and compare.

#+name: test
#+begin_src elisp :results drawer
  (save-excursion
    (goto-char 12)
    (org-latex-example-block  (org-element-at-point) nil nil))
#+end_src

#+RESULTS: test
:results:
\begin{verbatim}
This is
an example
block in
Org mode.
\end{verbatim}
:end:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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