Browse Source

LaTeX export: Allow export using the listings package

Patch by Eric Schulte, see

http://thread.gmane.org/gmane.emacs.orgmode/16269
Carsten Dominik 15 years ago
parent
commit
c5783bd8bd
5 changed files with 67 additions and 8 deletions
  1. 5 0
      doc/ChangeLog
  2. 8 5
      doc/org.texi
  3. 8 0
      lisp/ChangeLog
  4. 17 2
      lisp/org-exp.el
  5. 29 1
      lisp/org-latex.el

+ 5 - 0
doc/ChangeLog

@@ -1,3 +1,8 @@
+2009-08-09  Carsten Dominik  <carsten.dominik@gmail.com>
+
+	* org.texi (Literal examples): Document that source code can be
+	exported to LaTeX using the listings package.
+
 2009-08-04  Carsten Dominik  <carsten.dominik@gmail.com>
 2009-08-04  Carsten Dominik  <carsten.dominik@gmail.com>
 
 
 	* org.texi (Publishing action): Improve documentation of file
 	* org.texi (Publishing action): Improve documentation of file

+ 8 - 5
doc/org.texi

@@ -8178,11 +8178,13 @@ Here is an example
 @cindex formatting source code, markup rules
 @cindex formatting source code, markup rules
 If the example is source code from a programming language, or any other text
 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
 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 only for
-the HTML backend, and requires the @file{htmlize.el} package version 1.34 or
-later.}.  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{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 inclluded 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:
 @cindex #+BEGIN_SRC
 @cindex #+BEGIN_SRC
 
 
 @example
 @example
@@ -9664,6 +9666,7 @@ respective variable for details.
 @item @code{:priority}              @tab @code{org-export-with-priority}
 @item @code{:priority}              @tab @code{org-export-with-priority}
 @item @code{:TeX-macros}            @tab @code{org-export-with-TeX-macros}
 @item @code{:TeX-macros}            @tab @code{org-export-with-TeX-macros}
 @item @code{:LaTeX-fragments}       @tab @code{org-export-with-LaTeX-fragments}
 @item @code{:LaTeX-fragments}       @tab @code{org-export-with-LaTeX-fragments}
+@item @code{:latex-listings}        @tab @code{org-export-latex-listings}
 @item @code{:skip-before-1st-heading} @tab @code{org-export-skip-text-before-1st-heading}
 @item @code{:skip-before-1st-heading} @tab @code{org-export-skip-text-before-1st-heading}
 @item @code{:fixed-width}           @tab @code{org-export-with-fixed-width}
 @item @code{:fixed-width}           @tab @code{org-export-with-fixed-width}
 @item @code{:timestamps}            @tab @code{org-export-with-timestamps}
 @item @code{:timestamps}            @tab @code{org-export-with-timestamps}

+ 8 - 0
lisp/ChangeLog

@@ -1,3 +1,11 @@
+2009-08-09  Carsten Dominik  <carsten.dominik@gmail.com>
+
+	* org-latex.el (org-export-latex-listings)
+	(org-export-latex-listings-langs): New options.
+
+	* org-exp.el (org-export-format-source-code-or-example): Use
+	listing package if requested by the user.
+
 2009-08-07  Carsten Dominik  <carsten.dominik@gmail.com>
 2009-08-07  Carsten Dominik  <carsten.dominik@gmail.com>
 
 
 	* org-exp.el (org-get-file-contents): Only quote org lines when
 	* org-exp.el (org-get-file-contents): Only quote org lines when

+ 17 - 2
lisp/org-exp.el

@@ -574,6 +574,7 @@ much faster."
     (:priority		      "pri"	  org-export-with-priority)
     (:priority		      "pri"	  org-export-with-priority)
     (:TeX-macros	      "TeX"	  org-export-with-TeX-macros)
     (:TeX-macros	      "TeX"	  org-export-with-TeX-macros)
     (:LaTeX-fragments	      "LaTeX"	  org-export-with-LaTeX-fragments)
     (:LaTeX-fragments	      "LaTeX"	  org-export-with-LaTeX-fragments)
+    (:latex-listings	      nil         org-export-latex-listings)
     (:skip-before-1st-heading "skip"	  org-export-skip-text-before-1st-heading)
     (:skip-before-1st-heading "skip"	  org-export-skip-text-before-1st-heading)
     (:fixed-width	      ":"	  org-export-with-fixed-width)
     (:fixed-width	      ":"	  org-export-with-fixed-width)
     (:timestamps	      "<"	  org-export-with-timestamps)
     (:timestamps	      "<"	  org-export-with-timestamps)
@@ -2204,6 +2205,8 @@ in the list) and remove property and value from the list in LISTVAR."
 (defvar htmlp)  ;; dynamically scoped
 (defvar htmlp)  ;; dynamically scoped
 (defvar latexp)  ;; dynamically scoped
 (defvar latexp)  ;; dynamically scoped
 (defvar org-export-latex-verbatim-wrap) ;; defined in org-latex.el
 (defvar org-export-latex-verbatim-wrap) ;; defined in org-latex.el
+(defvar org-export-latex-listings) ;; defined in org-latex.el
+(defvar org-export-latex-listings-langs) ;; defined in org-latex.el
 
 
 (defun org-export-format-source-code-or-example
 (defun org-export-format-source-code-or-example
   (backend lang code &optional opts indent)
   (backend lang code &optional opts indent)
@@ -2310,8 +2313,20 @@ INDENT was the original indentation of the block."
 	     ((eq backend 'latex)
 	     ((eq backend 'latex)
 	      (setq rtn (org-export-number-lines rtn 'latex 0 0 num cont rpllbl fmt))
 	      (setq rtn (org-export-number-lines rtn 'latex 0 0 num cont rpllbl fmt))
 	      (concat "\n#+BEGIN_LaTeX\n"
 	      (concat "\n#+BEGIN_LaTeX\n"
-		      (org-add-props (concat (car org-export-latex-verbatim-wrap)
-					     rtn (cdr org-export-latex-verbatim-wrap))
+		      (org-add-props
+                          (if org-export-latex-listings
+                              (concat
+                               (if lang
+                                   (let* ((lang-sym (intern (concat ":" lang)))
+                                          (lstlang (or (plist-get org-export-latex-listings-langs
+                                                                  lang-sym)
+                                                       lang)))
+                                     (format "\\lstset{language=%s}\n" lstlang))
+                                 "")
+                               "\\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-protected t))
 		      "#+END_LaTeX\n\n"))
 		      "#+END_LaTeX\n\n"))
 	     ((eq backend 'ascii)
 	     ((eq backend 'ascii)

+ 29 - 1
lisp/org-latex.el

@@ -245,7 +245,7 @@ When nil, grouping causes only separation lines between groups."
 
 
 (defcustom org-export-latex-packages-alist nil
 (defcustom org-export-latex-packages-alist nil
   "Alist of packages to be inserted in the header.
   "Alist of packages to be inserted in the header.
-Each cell is of the forma \( \"option\" . \"package\" \)."
+Each cell is of the format \( \"option\" . \"package\" \)."
   :group 'org-export-latex
   :group 'org-export-latex
   :type 'alist)
   :type 'alist)
 
 
@@ -297,6 +297,34 @@ Defaults to \\begin{verbatim} and \\end{verbatim}."
   :type '(cons (string :tag "Open")
   :type '(cons (string :tag "Open")
 	       (string :tag "Close")))
 	       (string :tag "Close")))
 
 
+(defcustom org-export-latex-listings nil
+  "Non-nil means, export source code using the listings package.
+This package will fontify source code, possibly even with color.
+If you want to use this, you also need to make LaTeX use the
+listings package, and if you want to have color, the color
+package.  Just add these 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 '(\"\" \"listings\"))
+  (add-to-list 'org-export-latex-packages-alist '(\"\" \"color\"))"
+  :group 'org-export-latex
+  :type 'boolean)
+
+(defcustom org-export-latex-listings-langs
+  '(:emacs-lisp "Lisp" :lisp "Lisp"
+		:c "C" :cc "C++"
+		:fortran "fortran"
+		:perl "Perl" :cperl "Perl" :python "Python" :ruby "Ruby"
+		:html "HTML" :xml "XML"
+		:tex "TeX" :latex "TeX"
+		:shell-script "bash")
+  "Property list mapping languages to their listing language counterpart.
+Yhe keys ar ethe major mode symbol, the calues are the string that should be
+inserted as the language parameter for the listings package."
+  :group 'org-export-latex
+  :type 'plist)
+
 (defcustom org-export-latex-remove-from-headlines
 (defcustom org-export-latex-remove-from-headlines
   '(:todo nil :priority nil :tags nil)
   '(:todo nil :priority nil :tags nil)
   "A plist of keywords to remove from headlines. OBSOLETE.
   "A plist of keywords to remove from headlines. OBSOLETE.