Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to encapsualte code blocks into a frame when exporting to pdf?

I have been using org-mode + emacs for a while now, and I love how easy it is to produce contents. I often use the html + pdf export combo from the same document (first, a web page, following, a pdf document). My problem is about exporting code blocks (#+BEGIN_SRC...) to pdf.

To html, the export command (C-c C-e h h) gives me a satisfactory solution: it uses a frame to encapsulate the code (showing the programming language when you rest the mouse pointer on it) and uses a different frame for the resulting messages (as I set :export both). When using #+CAPTION: my caption here before the #+BEGIN_SRC, resulting html page includes "Listing #: my caption here" before the code frame.

To pdf, the document generated by export command (C-c C-e l p) doesn't have frames around neither code or results (a real mess), and captions show up as "Figure #: my caption here" in between the code and results.

How do I get both different frames for code and results and Listings-like captions for my code blocks when exporting from org-mode to pdf?

Here is a minimal example:

#+TITLE: EXPORT TESTINGS
#+OPTIONS: toc:nil

#+CAPTION: Caption, my caption!
#+BEGIN_SRC C :results output :exports both
int i, x = 10;
for(i = 0; i < x; i++)
    printf("%d ",i);
printf(" ~ %d\n", x);
#+END_SRC

Here is the resulting html and the resulting pdf.

like image 656
iperetta Avatar asked Dec 19 '22 04:12

iperetta


1 Answers

Based on the Alex Ott answer (and a couple hours of web browsing) I finally did it.

We are going to use the minted package. For completness, this how I had to set up everything:


Python is needed

minted use a Python package to highlight syntax called Pygmets. You could install it with:

pip install Pygments

Emacs setup

  • In org file preamble you need the line: #+LaTeX_HEADER: \usepackage{minted}
  • Source code block export use the variable org-latex-listings. You have to set it with: (setq org-latex-listings 'minted).
  • Last but not least, you have to allow pdflatex to execute shell commands in order to use the Python package Pygments. The option is -shell-escape. And the emacs variable that describes the call to the LaTeX compiler is org-latex-pdf-process.

To achieve those 3 points, I have added this snipet in my init file

;; inside .emacs file
(setq org-latex-listings 'minted
      org-latex-packages-alist '(("" "minted"))
      org-latex-pdf-process
      '("pdflatex -shell-escape -interaction nonstopmode -output-directory %o %f"
        "pdflatex -shell-escape -interaction nonstopmode -output-directory %o %f"
        "pdflatex -shell-escape -interaction nonstopmode -output-directory %o %f"))

NB: see here to understand why three calls to pdflatex are needed. If you use bibtex you have to insert the appropriate line.


Back to your Org file

Now you can add the LaTeX attribute on top of your source code block:

#+ATTR_LATEX: :options frame=single
#+BEGIN_SRC emacs-lisp
  (defun Fib (n)
    (if (< n 2) n (+ (Fib (- n 1)) (Fib (- n 2)))))
#+END_SRC

Et voilà ! enter image description here


Minted manual

To use different frame styles go and check the manual

like image 172
pietrodito Avatar answered Dec 20 '22 18:12

pietrodito