summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Davison <davison@stats.ox.ac.uk>2010-10-12 23:20:28 +0100
committerDan Davison <davison@stats.ox.ac.uk>2010-10-12 23:20:28 +0100
commit60fa8cf31a91836eccbc8b3ef6c2a9f533d2b57e (patch)
tree0e077f3bb90fed77d9302d0fba546a975f7f2317
parent5014288b680b00a639386518ee258846f0d10255 (diff)
downloadorg-mode-60fa8cf31a91836eccbc8b3ef6c2a9f533d2b57e.tar.gz
LaTeX minted package for fontified source code export
* org-exp.el (org-export-format-source-code-or-example): latex formatting of source code blocks using the minted package (org-export-plist-vars): Add :latex-minted property (org-export-latex-minted): Ensure variable is defined (org-export-latex-minted-langs): Ensure variable is defined * org-latex.el (org-export-latex-minted): new variable (org-export-latex-minted-langs): new variable (org-export-latex-minted-with-line-numbers): new variable (org-export-latex-minted):
-rw-r--r--doc/org.texi25
-rw-r--r--lisp/org-exp.el81
-rw-r--r--lisp/org-latex.el46
3 files changed, 116 insertions, 36 deletions
diff --git a/doc/org.texi b/doc/org.texi
index 801bf12..088d43a 100644
--- a/doc/org.texi
+++ b/doc/org.texi
@@ -8653,13 +8653,24 @@ Here is an example
@cindex formatting source code, markup rules
If the example is source code from a programming language, or any other text
that can be marked up by font-lock in Emacs, you can ask for the example to
-look like the fontified Emacs buffer@footnote{Currently this works for the
-HTML backend, and requires the @file{htmlize.el} package version 1.34 or
-later. It also works for LaTeX with the listings package, if you turn on the
-option @code{org-export-latex-listings} and make sure that the listings
-package is included by the LaTeX header.}. This is done with the @samp{src}
-block, where you also need to specify the name of the major mode that should
-be used to fontify the example:
+look like the fontified Emacs buffer@footnote{This works automatically for
+the HTML backend (it requires version 1.34 of the @file{htmlize.el} package,
+which is distributed with Org.) Fontified code chunks in LaTeX can be
+achieved using either the listings or the
+@url{http://code.google.com/p/minted, minted,} package. To use listings, turn
+on the variable @code{org-export-latex-listings} and ensure that the listings
+package is included by the LaTeX header (e.g. by configuring
+@code{org-export-latex-packages-alist}). See the listings documentation for
+configuration options, including obtaining colored output. For minted it is
+necessary to install the program @url{http://pygments.org, pygments}, in
+addition to setting @code{org-export-latex-minted}, ensuring that the minted
+package is included by the LaTeX header, and ensuring that the
+@code{-shell-escape} option is passed to @file{pdflatex} (see
+@code{org-latex-to-pdf-process}). See the documentation of the variables
+@code{org-export-latex-listings} and @code{org-export-latex-minted} for
+further details.}. This is done with the @samp{src} block, where you also
+need to specify the name of the major mode that should be used to fontify the
+example:
@cindex #+BEGIN_SRC
@example
diff --git a/lisp/org-exp.el b/lisp/org-exp.el
index f75cfb4..6486e46 100644
--- a/lisp/org-exp.el
+++ b/lisp/org-exp.el
@@ -607,6 +607,7 @@ table.el tables."
(:TeX-macros "TeX" org-export-with-TeX-macros)
(:LaTeX-fragments "LaTeX" org-export-with-LaTeX-fragments)
(:latex-listings nil org-export-latex-listings)
+ (:latex-minted nil org-export-latex-minted)
(:skip-before-1st-heading "skip" org-export-skip-text-before-1st-heading)
(:fixed-width ":" org-export-with-fixed-width)
(:timestamps "<" org-export-with-timestamps)
@@ -2208,6 +2209,8 @@ in the list) and remove property and value from the list in LISTVAR."
(defvar org-export-latex-listings) ;; defined in org-latex.el
(defvar org-export-latex-listings-langs) ;; defined in org-latex.el
(defvar org-export-latex-listings-w-names) ;; defined in org-latex.el
+(defvar org-export-latex-minted) ;; defined in org-latex.el
+(defvar org-export-latex-minted-langs) ;; defined in org-latex.el
(defun org-export-format-source-code-or-example
(backend lang code &optional opts indent caption)
@@ -2337,35 +2340,55 @@ INDENT was the original indentation of the block."
(setq rtn (org-export-number-lines rtn 'latex 0 0 num cont rpllbl fmt))
(concat "#+BEGIN_LaTeX\n"
(org-add-props
- (if org-export-latex-listings
- (concat
- (if lang
- (let*
- ((lang-sym (intern lang))
- (lstlang
- (or (cadr
- (assq
- lang-sym
- org-export-latex-listings-langs))
- lang)))
- (format "\\lstset{language=%s}\n" lstlang))
- "\n")
- (when (and caption
- org-export-latex-listings-w-names)
- (format "\n%s $\\equiv$ \n"
- (replace-regexp-in-string
- "_" "\\\\_" caption)))
- "\\begin{lstlisting}\n"
- rtn "\\end{lstlisting}\n")
- (concat (car org-export-latex-verbatim-wrap)
- rtn (cdr org-export-latex-verbatim-wrap)))
- '(org-protected t org-example t))
- "#+END_LaTeX\n"))
- ((eq backend 'ascii)
- ;; This is not HTML or LaTeX, so just make it an example.
- (setq rtn (org-export-number-lines rtn 'ascii 0 0 num cont rpllbl fmt))
- (concat caption "\n"
- "#+BEGIN_ASCII\n"
+ (cond
+ (org-export-latex-listings
+ (concat
+ (if lang
+ (let*
+ ((lang-sym (intern lang))
+ (lstlang
+ (or (cadr
+ (assq
+ lang-sym
+ org-export-latex-listings-langs))
+ lang)))
+ (format "\\lstset{language=%s}\n" lstlang))
+ "\n")
+ (when (and caption
+ org-export-latex-listings-w-names)
+ (format "\n%s $\\equiv$ \n"
+ (replace-regexp-in-string
+ "_" "\\\\_" caption)))
+ "\\begin{lstlisting}\n"
+ rtn "\\end{lstlisting}\n"))
+ (org-export-latex-minted
+ (if lang
+ (let*
+ ((lang-sym (intern lang))
+ (minted-lang
+ (or (cadr
+ (assq
+ lang-sym
+ org-export-latex-minted-langs))
+ (downcase lang))))
+ (concat
+ (when (and caption
+ org-export-latex-listings-w-names)
+ (format "\n%s $\\equiv$ \n"
+ (replace-regexp-in-string
+ "_" "\\\\_" caption)))
+ (format
+ "\\begin{minted}[mathescape,%s\nnumbersep=5pt,\nframe=lines,\nframesep=2mm]{%s}\n" (if org-export-latex-minted-with-line-numbers "\nlinenos," "") minted-lang)
+ rtn "\\end{minted}\n"))))
+ (t (concat (car org-export-latex-verbatim-wrap)
+ rtn (cdr org-export-latex-verbatim-wrap))))
+ '(org-protected t org-example t))
+ "#+END_LaTeX\n"))
+ ((eq backend 'ascii)
+ ;; This is not HTML or LaTeX, so just make it an example.
+ (setq rtn (org-export-number-lines rtn 'ascii 0 0 num cont rpllbl fmt))
+ (concat caption "\n"
+ "#+BEGIN_ASCII\n"
(org-add-props
(concat
(mapconcat
diff --git a/lisp/org-latex.el b/lisp/org-latex.el
index bae4c92..1abec60 100644
--- a/lisp/org-latex.el
+++ b/lisp/org-latex.el
@@ -411,6 +411,52 @@ of noweb."
:group 'org-export-latex
:type 'boolean)
+(defcustom org-export-latex-minted nil
+ "Non-nil means export source code using the minted package.
+This package will fontify source code with color.
+If you want to use this, you need to make LaTeX use the
+minted package. Add this to `org-export-latex-packages-alist',
+for example using customize, or with something like
+
+ (require 'org-latex)
+ (add-to-list 'org-export-latex-packages-alist '(\"\" \"minted\"))
+
+In addition, it is neccessary to configure
+`org-latex-to-pdf-process' so that the -shell-escape option is
+passed to pdflatex."
+ :group 'org-export-latex
+ :type 'boolean)
+
+(defcustom org-export-latex-minted-langs
+ '((emacs-lisp "common-lisp")
+ (cc "c++")
+ (cperl "perl")
+ (shell-script "bash")
+ (caml "ocaml"))
+ "Alist mapping languages to their minted language counterpart.
+The key is a symbol, the major mode symbol without the \"-mode\".
+The value is the string that should be inserted as the language parameter
+for the minted package. If the mode name and the listings name are
+the same, the language does not need an entry in this list - but it does not
+hurt if it is present.
+
+Note that minted uses all lower case for language identifiers,
+and that the full list of language identifiers can be obtained
+with:
+pygmentize -L lexers
+"
+ :group 'org-export-latex
+ :type '(repeat
+ (list
+ (symbol :tag "Major mode ")
+ (string :tag "Listings language"))))
+
+(defcustom org-export-latex-minted-with-line-numbers nil
+ "Should source code line numbers be included when exporting
+with the latex minted package?"
+ :group 'org-export-latex
+ :type 'boolean)
+
(defcustom org-export-latex-remove-from-headlines
'(:todo nil :priority nil :tags nil)
"A plist of keywords to remove from headlines. OBSOLETE.