Export org-mode code block and result with different styles
Asked Answered
B

1

15

I am preparing a presentation using org-mode and babel, and want to export to beamer pdf.

In the output, the source code and the results are with the same style (verbatim in latex). Thus it is difficult to distinguish them.

Would it be possible to export source code and results with different styles (preferably different color)?

Thanks a lot!

Barksdale answered 8/1, 2014 at 20:32 Comment(1)
I found using listings can solve the problem. I will tweak the styles a bit and answer myself. More answers are of course welcome, especially if there are other ways.Barksdale
B
23

You could use the minted LaTeX package to syntax highlight the source code:

C-h v org-latex-listings

...

  (setq org-latex-listings 'minted)

causes source code to be exported using the minted package as
opposed to listings.  If you want to use minted, you need to add
the minted package to `org-latex-packages-alist', for example
using customize, or with

  (require 'ox-latex)
  (add-to-list 'org-latex-packages-alist '("" "minted"))

In addition, it is necessary to install pygments
(http://pygments.org), and to configure the variable
`org-latex-pdf-process' so that the -shell-escape option is
passed to pdflatex.

The minted choice has possible repercussions on the preview of
latex fragments (see `org-preview-latex-fragment').  If you run
into previewing problems, please consult

  http://orgmode.org/worg/org-tutorials/org-latex-preview.html

I have this in my init file:

(require 'ox-latex)
(add-to-list 'org-latex-packages-alist '("" "minted"))
(setq org-latex-listings 'minted)

(setq 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"))

There are different color-themes you can use with minted, for example you could put this option into your org file to use "monokai":

#+LaTeX_HEADER: \usemintedstyle{monokai}

to get a list of the supported styles from pygmentize:

pygmentize -L styles
Breathless answered 8/1, 2014 at 21:39 Comment(1)
Thanks a lot! The language I currently need (Mathematica) is supported by listings package instead of minted. Nevertheless, your answer perfectly answers my above question. In case of Mathematica, I wrote a blog about how to do it cosmosimple.blogspot.co.uk/2014/01/…Barksdale

© 2022 - 2024 — McMap. All rights reserved.