2

I am writing a report in org mode and would like to show some code blocks as seen in so many tutorials. Here is some official documentation, however it was written for org-mode 6.xx back in 2011.

I have tried all variations that I have found on my search so far:

#+BEGIN_SRC R
  for(i in 1:19){
      print(i)
  }
#+END_SRC

#+BEGIN_SRC R :exports code
  for(i in 1:19){
      print(i)
  }
#+END_SRC


#+BEGIN_SRC R :exports both
  for(i in 1:19){
      print(i)
  }
#+END_SRC

#+begin_src R
  for(i in 1:19){
      print(i)
  }
#+end_src

#+begin_src R :exports code
  for(i in 1:19){
      print(i)
  }
#+end_src

The output looks like this:

enter image description here

I feel this must therefore be an issue with my setup. Here are (as far as I know) the relevant parts of my .emacs:

(setq org-src-fontify-natively t)

(org-babel-do-load-languages
 'org-babel-load-languages
 '((R . t)
   (latex . t)))

I have org-version 8.2.10 and all my LaTeX packages are up to date. I also have things setup for python, which also doesn't work, further pointing to a faulty setup.

The most promising help I could find was this great tutorial, which creates an environment for using org with Latex/HTML exports and my language of choice R. It is all written in an org file, however I still can't find my problem. The code blocks there, if I generate the .tex and pdf files still do not show up correctly.

Edit 1: TeX buffer output

 % Created 2016-03-08 Tue 08:18

 \documentclass{article}
 \usepackage[utf8]{inputenc}
 \usepackage[T1]{fontenc}
 \usepackage{fixltx2e} 
 \usepackage{graphicx} 
 \usepackage{longtable}
 \usepackage{float} 
 \usepackage{wrapfig} 
 \usepackage{rotating}
 \usepackage[normalem]{ulem}
 \usepackage{amsmath} 
 \usepackage{textcomp}
 \usepackage{marvosym} 
 \usepackage{wasysym}
 \usepackage{amssymb}
 \usepackage{hyperref} 
 \tolerance=1000
 \usepackage{minted}
 \author{user name} 
 \date{\today}
 \title{Org-mode and R: An Introduction} 
 \hypersetup{   pdfkeywords={},   pdfsubject={},  pdfcreator={Emacs 24.5.1 (Org mode 8.2.10)}} 
 \begin{document}

 \maketitle 
 \setcounter{tocdepth}{2}
 \tableofcontents

 \lstset{language=R,label= ,caption= ,numbers=none} 
 \begin{lstlisting}
 for(i in 1:19){
     print(i) } 
 \end{lstlisting}

 \lstset{language=R,label= ,caption= ,numbers=none} 
 \begin{lstlisting}
 for(i in 1:19){
     print(i) } 
 \end{lstlisting}

 \lstset{language=R,label= ,caption= ,numbers=none} 
 \begin{lstlisting}
 for(i in 1:19){
     print(i) } 
 \end{lstlisting}

 \lstset{language=R,label= ,caption= ,numbers=none}
 \begin{lstlisting}
 for(i in 1:19){
     print(i) } 
 \end{lstlisting}

 \lstset{language=R,label= ,caption= ,numbers=none} 
 \begin{lstlisting}
 for(i in 1:19){
     print(i) } 
 \end{lstlisting} % Emacs 24.5.1 (Org mode 8.2.10) 
 \end{document}

Edit 2: Output from the *Org PDF LaTeX Output* buffer

This is pdfTeX, Version 3.14159265-2.6-1.40.16 (TeX Live 2015) (preloaded format=pdflatex)
 restricted \write18 enabled.
entering extended mode

(/path/to/this/file.tex
LaTeX2e <2016/02/01>
Babel <3.9q> and hyphenation patterns for 81 language(s) loaded.
(/usr/local/texlive/2015/texmf-dist/tex/latex/base/article.cls
Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
(/usr/local/texlive/2015/texmf-dist/tex/latex/base/size10.clo))
(/usr/local/texlive/2015/texmf-dist/tex/latex/base/inputenc.sty
(/usr/local/texlive/2015/texmf-dist/tex/latex/base/utf8.def
(/usr/local/texlive/2015/texmf-dist/tex/latex/base/t1enc.dfu)
(/usr/local/texlive/2015/texmf-dist/tex/latex/base/ot1enc.dfu)
(/usr/local/texlive/2015/texmf-dist/tex/latex/base/omsenc.dfu)))
(/usr/local/texlive/2015/texmf-dist/tex/latex/base/fontenc.sty
(/usr/local/texlive/2015/texmf-dist/tex/latex/base/t1enc.def))
(/usr/local/texlive/2015/texmf-dist/tex/latex/base/fixltx2e.sty

Package fixltx2e Warning: fixltx2e is not required with releases after 2015
(fixltx2e)                All fixes are now in the LaTeX kernel.
(fixltx2e)                See the latexrelease package for details.

) (/usr/local/texlive/2015/texmf-dist/tex/latex/graphics/graphicx.sty
(/usr/local/texlive/2015/texmf-dist/tex/latex/graphics/keyval.sty)
(/usr/local/texlive/2015/texmf-dist/tex/latex/graphics/graphics.sty
(/usr/local/texlive/2015/texmf-dist/tex/latex/graphics/trig.sty)
(/usr/local/texlive/2015/texmf-dist/tex/latex/latexconfig/graphics.cfg)
(/usr/local/texlive/2015/texmf-dist/tex/latex/pdftex-def/pdftex.def
(/usr/local/texlive/2015/texmf-dist/tex/generic/oberdiek/infwarerr.sty)
(/usr/local/texlive/2015/texmf-dist/tex/generic/oberdiek/ltxcmds.sty))))
(/usr/local/texlive/2015/texmf-dist/tex/latex/tools/longtable.sty)
(/usr/local/texlive/2015/texmf-dist/tex/latex/float/float.sty)
(/usr/local/texlive/2015/texmf-dist/tex/latex/wrapfig/wrapfig.sty)
(/usr/local/texlive/2015/texmf-dist/tex/latex/rotating/rotating.sty
(/usr/local/texlive/2015/texmf-dist/tex/latex/base/ifthen.sty))
(/usr/local/texlive/2015/texmf-dist/tex/generic/ulem/ulem.sty)
(/usr/local/texlive/2015/texmf-dist/tex/latex/amsmath/amsmath.sty
For additional information on amsmath, use the `?' option.
(/usr/local/texlive/2015/texmf-dist/tex/latex/amsmath/amstext.sty
(/usr/local/texlive/2015/texmf-dist/tex/latex/amsmath/amsgen.sty))
(/usr/local/texlive/2015/texmf-dist/tex/latex/amsmath/amsbsy.sty)
(/usr/local/texlive/2015/texmf-dist/tex/latex/amsmath/amsopn.sty))
(/usr/local/texlive/2015/texmf-dist/tex/latex/base/textcomp.sty
(/usr/local/texlive/2015/texmf-dist/tex/latex/base/ts1enc.def
(/usr/local/texlive/2015/texmf-dist/tex/latex/base/ts1enc.dfu)))
(/usr/local/texlive/2015/texmf-dist/tex/latex/marvosym/marvosym.sty)
(/usr/local/texlive/2015/texmf-dist/tex/latex/wasysym/wasysym.sty)
(/usr/local/texlive/2015/texmf-dist/tex/latex/amsfonts/amssymb.sty
(/usr/local/texlive/2015/texmf-dist/tex/latex/amsfonts/amsfonts.sty))
(/usr/local/texlive/2015/texmf-dist/tex/latex/hyperref/hyperref.sty
(/usr/local/texlive/2015/texmf-dist/tex/generic/oberdiek/hobsub-hyperref.sty
(/usr/local/texlive/2015/texmf-dist/tex/generic/oberdiek/hobsub-generic.sty))
(/usr/local/texlive/2015/texmf-dist/tex/generic/ifxetex/ifxetex.sty)
(/usr/local/texlive/2015/texmf-dist/tex/latex/oberdiek/auxhook.sty)
(/usr/local/texlive/2015/texmf-dist/tex/latex/oberdiek/kvoptions.sty)
(/usr/local/texlive/2015/texmf-dist/tex/latex/hyperref/pd1enc.def)
(/usr/local/texlive/2015/texmf-dist/tex/latex/latexconfig/hyperref.cfg)
(/usr/local/texlive/2015/texmf-dist/tex/latex/url/url.sty))

Package hyperref Message: Driver (autodetected): hpdftex.

(/usr/local/texlive/2015/texmf-dist/tex/latex/hyperref/hpdftex.def
(/usr/local/texlive/2015/texmf-dist/tex/latex/oberdiek/rerunfilecheck.sty))
(/usr/local/texlive/2015/texmf-dist/tex/latex/minted/minted.sty
(/usr/local/texlive/2015/texmf-dist/tex/latex/fancyvrb/fancyvrb.sty
Style option: `fancyvrb' v2.7a, with DG/SPQR fixes, and firstline=lastline fix 
<2008/02/07> (tvz))
(/usr/local/texlive/2015/texmf-dist/tex/latex/tools/calc.sty)
(/usr/local/texlive/2015/texmf-dist/tex/latex/ifplatform/ifplatform.sty
(/usr/local/texlive/2015/texmf-dist/tex/generic/oberdiek/catchfile.sty)

Package ifplatform Warning: 
    shell escape is disabled, so I can only detect \ifwindows.

) (/usr/local/texlive/2015/texmf-dist/tex/latex/etoolbox/etoolbox.sty)
(/usr/local/texlive/2015/texmf-dist/tex/generic/xstring/xstring.sty
(/usr/local/texlive/2015/texmf-dist/tex/generic/xstring/xstring.tex))
(/usr/local/texlive/2015/texmf-dist/tex/latex/lineno/lineno.sty)
(/usr/local/texlive/2015/texmf-dist/tex/latex/base/t1cmtt.fd))

! Package minted Error: You must invoke LaTeX with the -shell-escape flag.

See the minted package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.20 \author
            {user name}

! Package minted Error: Missing Pygments output; \inputminted was
probably given a file that does not exist--otherwise, you may need 
the outputdir package option, or may be using an incompatible build tool.

See the minted package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.20 \author
            {user name}^^M

! Package minted Error: You must have `pygmentize' installed to use this packag
e.

See the minted package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.27 \begin{document}

(.//delete_this.aux)
(/usr/local/texlive/2015/texmf-dist/tex/latex/base/ts1cmr.fd)
(/usr/local/texlive/2015/texmf-dist/tex/context/base/supp-pdf.mkii
[Loading MPS to PDF converter (version 2006.09.02).]
) (/usr/local/texlive/2015/texmf-dist/tex/latex/oberdiek/epstopdf-base.sty
(/usr/local/texlive/2015/texmf-dist/tex/latex/oberdiek/grfext.sty)
(/usr/local/texlive/2015/texmf-dist/tex/latex/latexconfig/epstopdf-sys.cfg))
(/usr/local/texlive/2015/texmf-dist/tex/latex/hyperref/nameref.sty
(/usr/local/texlive/2015/texmf-dist/tex/generic/oberdiek/gettitlestring.sty))
(.//delete_this.out) (.//delete_this.out)
(/usr/local/texlive/2015/texmf-dist/tex/latex/xcolor/xcolor.sty
(/usr/local/texlive/2015/texmf-dist/tex/latex/latexconfig/color.cfg))

! Package minted Error: Missing Pygments output; \inputminted was
probably given a file that does not exist--otherwise, you may need 
the outputdir package option, or may be using an incompatible build tool.

See the minted package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.27 \begin{document}
                     ^^M
(/usr/local/texlive/2015/texmf-dist/tex/latex/wasysym/uwasy.fd)
(/usr/local/texlive/2015/texmf-dist/tex/latex/amsfonts/umsa.fd)
(/usr/local/texlive/2015/texmf-dist/tex/latex/amsfonts/umsb.fd)
(.//delete_this.toc)
! Undefined control sequence.
l.33 \lstset
            {language=R,label= ,caption= ,numbers=none}

! LaTeX Error: Environment lstlisting undefined.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.34 \begin{lstlisting}


! LaTeX Error: \begin{document} ended by \end{lstlisting}.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.38 \end{lstlisting}

! Undefined control sequence.
l.40 \lstset
            {language=R,label= ,caption= ,numbers=none}

! LaTeX Error: Environment lstlisting undefined.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.41 \begin{lstlisting}


! LaTeX Error: \begin{document} ended by \end{lstlisting}.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.45 \end{lstlisting}

! Undefined control sequence.
l.48 \lstset
            {language=R,label= ,caption= ,numbers=none}

! LaTeX Error: Environment lstlisting undefined.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.49 \begin{lstlisting}


! LaTeX Error: \begin{document} ended by \end{lstlisting}.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.53 \end{lstlisting}

! Undefined control sequence.
l.55 \lstset
            {language=R,label= ,caption= ,numbers=none}

! LaTeX Error: Environment lstlisting undefined.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.56 \begin{lstlisting}


! LaTeX Error: \begin{document} ended by \end{lstlisting}.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.60 \end{lstlisting}

! Undefined control sequence.
l.62 \lstset
            {language=R,label= ,caption= ,numbers=none}

! LaTeX Error: Environment lstlisting undefined.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.63 \begin{lstlisting}


! LaTeX Error: \begin{document} ended by \end{lstlisting}.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.67 \end{lstlisting}

[1{/usr/local/texlive/2015/texmf-var/fonts/map/pdftex/updmap/pdftex.map}]
(.//delete_this.aux) )
(see the transcript file for additional information){/usr/local/texlive/2015/te
xmf-dist/fonts/enc/dvips/cm-super/cm-super-t1.enc}</usr/local/texlive/2015/texm
f-dist/fonts/type1/public/cm-super/sfbx1440.pfb></usr/local/texlive/2015/texmf-
dist/fonts/type1/public/cm-super/sfrm1000.pfb></usr/local/texlive/2015/texmf-di
st/fonts/type1/public/cm-super/sfrm1200.pfb></usr/local/texlive/2015/texmf-dist
/fonts/type1/public/cm-super/sfrm1728.pfb>
Output written on .//delete_this.pdf (1 page, 50965 bytes).
Transcript written on .//delete_this.log.
  • This looks really wrong. Can you post the LaTeX code generated for it? What happens if you C-c C-c on a code block? – wvxvw Mar 8 '16 at 6:17
  • @wvxvw - Good idea. Apologies for not having included it originally. Running C-c C-c returns the messages: "wrote var/block/p2/random/path/to/somewhere..." and "code block priduced no output". – n1k31t4 Mar 8 '16 at 7:30
  • Well, LaTeX code seems to want to use the listings package, do you have it installed? Are there any errors in the *Org PDF LaTeX Output*? Also, if you want the output of printing something, rather than a result of a function being pretty-printed, you need to specify :results output in the code block header. – wvxvw Mar 8 '16 at 9:52
  • @wvxvw - The package listings is installed. As well as listing (no 's'). There are however errors in *Org PDF LaTeX Output* - towards the end of the block that I have added to my post. It talks about not finding an environment that looks like it is associated with listings. There are also some errors regarding pygmentize from the pygments library I assume, but I thought that was only for python...? – n1k31t4 Mar 8 '16 at 10:35
  • @wvxvw - hitting C-c C-c on a code block with :results output did work in the .org buffer, but doesn'T change the pdf output i.e. the results are not converted to pdf. – n1k31t4 Mar 8 '16 at 12:05
4

You have to tell org-mode to actually use the listings or listingsutf8 package in your .emacs:

(add-to-list 'org-latex-packages-alist '("" "listingsutf8"))

Then you'll have a line

\usepackage{listings}

in the exported TeX file and the pdfLaTeX run will give you the result you want to have.

Addition: For minted, I get a satisfying result when using the following .emacs (using information from https://stackoverflow.com/a/21007117/6024971):

(require 'org)
(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"))

(setq org-src-fontify-natively t)

(org-babel-do-load-languages
 'org-babel-load-languages
 '((R . t)
   (latex . t)))

Note, the triple call to pdflatex is to get all the references (including TOC) in the document right.

  • Unfortunately that didn't work. I added (setq org-latex-listing t), however the result is the same and there is no appearance of \usepackage{listings}. – n1k31t4 Mar 8 '16 at 12:06
  • Sorry, the old answer was valid a couple of years ago. I updated my answer to reflect current org usage. – Robert Klein Mar 8 '16 at 13:10
  • Hooray, that gets the code into a pdf! :) It is however without any syntax highlighting. As per my OP, I have (setq org-src-fontify-natively t) in my setup... does this not carry over into exported versions? Or do I require yet another LaTeX package for this? ` – n1k31t4 Mar 8 '16 at 14:16
  • I should mention that I also have minted installed, but with or without it running using (setq org-export-latex-listings 'minted), I am not getting any syntax highlighting. I have also tried (add-to-list 'org-latex-packages-alist '("" "minted")) without any luck. – n1k31t4 Mar 8 '16 at 14:31
  • You might need to install pygmentize for minted to work. Also run pdflatex with -shell-escape. – John Kitchin Mar 8 '16 at 14:34

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.