This is a solution using latex package lstlisting
instead of minted
(lstlisting is not so powerful as minted but does not need a whole python stack).
You need to put following lisp code into your Emacs init.el (or try it in *scratch*
buffer like I did.
(setq org-latex-listings t)
(add-to-list 'org-latex-packages-alist '("" "listings"))
(add-to-list 'org-latex-packages-alist '("" "color"))
These lines load the latex packages listings
and color
and advice org mode to export code block using the listings
latex package.
After that you can write your code block that way:
#+CAPTION: Caption, my caption!
#+ATTR_LATEX: :options frame=single,backgroundcolor=\color{lightgray}
#+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
Notice the extra line of
#+ATTR_LATEX: :options frame=single,backgroundcolor=\color{lightgray}
,
which defines a frame around the source code and a background color.
Above settings should produce following lines (among others) in the resulting .tex
file:
...
\usepackage{color}
\usepackage{listings}
...
\lstset{language=C,label= ,caption={Caption, my caption!},captionpos=b,numbers=none,frame=single backgroundcolor=\color{lightgray}}
\begin{lstlisting}
int i, x = 10;
for(i = 0; i < x; i++)
printf("%d ",i);
printf(" ~ %d\n", x);
\end{lstlisting}
Remark: in older Org-Versions (v8 and older) #+ATTR_LATEX
uses another syntax. Space (
) instead of =
is used. The #+ATTR_LATEX
needs to be changed slightly. Read more at the question org ignores attr latex
Note: here is another solution, using minted
: https://stackoverflow.com/a/60396939