浏览代码

New UI for configuring latex src code export.

Three new user-customizable variables:

org-export-latex-listings-options (default nil)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Association list of options for the latex listings package.

These options are supplied as a comma-separated list to the
\\lstset command. Each element of the association list should be
a list containing two strings: the name of the option, and the
value. For example,

  (setq org-export-latex-listings-options
    '((\"basicstyle\" \"\\small\")
      (\"keywordstyle\" \"\\color{black}\\bfseries\\underbar\")))

will typeset the code in a small size font with underlined, bold
black keywords.

Note that the same options will be applied to blocks of all
languages.

See ftp://ftp.tex.ac.uk/tex-archive/macros/latex/contrib/listings/listings.pdf

customization group: org-export-latex

org-export-latex-minted-options (default nil)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Association list of options for the latex minted package.

These options are supplied within square brackets in
\\begin{minted} environments. Each element of the alist should be
a list containing two strings: the name of the option, and the
value. For example,

  (setq org-export-latex-minted-options
    '((\"bgcolor\" \"bg\") (\"frame\" \"lines\")))

will result in src blocks being exported with

\\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}

as the start of the minted environment. Note that the same
options will be applied to blocks of all languages."

customization group: org-export-latex

See minted.googlecode.com/files/minted.pdf

org-export-latex-custom-lang-environments (default nil)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Association list mapping languages to language-specific latex
environments used during export of src blocks by the listings
and minted latex packages. For example,

  (setq org-export-latex-custom-lang-environments
     '((python \"pythoncode\")))

* lisp/org-exp.el (org-export-format-source-code-or-example):
  Support new user-customizable options
(org-export-latex-custom-lang-environments): Ensure new variable is defined
(org-export-latex-listings-options): Ensure new variable is defined
(org-export-latex-minted-options): Ensure new variable is defined

* lisp/org-latex.el (org-export-latex-listings-options): New variable
(org-export-latex-minted-options): New variable
(org-export-latex-custom-lang-environments): New variable
Dan Davison 14 年之前
父节点
当前提交
65d0b3d353
共有 2 个文件被更改,包括 120 次插入50 次删除
  1. 59 50
      lisp/org-exp.el
  2. 61 0
      lisp/org-latex.el

+ 59 - 50
lisp/org-exp.el

@@ -2293,6 +2293,10 @@ in the list) and remove property and value from the list in LISTVAR."
 (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-langs) ;; defined in org-latex.el
+(defvar org-export-latex-custom-lang-environments) ;; defined in org-latex.el
+(defvar org-export-latex-listings-options) ;; defined in org-latex.el
+(defvar org-export-latex-minted-options) ;; defined in org-latex.el
+
 (defun org-export-format-source-code-or-example
   (backend lang code &optional opts indent caption)
   "Format CODE from language LANG and return it formatted for export.
@@ -2419,56 +2423,61 @@ INDENT was the original indentation of the block."
 	      (concat "\n#+BEGIN_HTML\n" (org-add-props rtn '(org-protected t org-example t)) "\n#+END_HTML\n\n"))
 	     ((eq backend 'latex)
 	      (setq rtn (org-export-number-lines rtn 'latex 0 0 num cont rpllbl fmt))
-	      (concat "#+BEGIN_LaTeX\n"
-		      (org-add-props
-                          (cond
-			   ((and org-export-latex-listings
-				 (not (eq org-export-latex-listings 'minted)))
-			    (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"))
-			   ((eq org-export-latex-listings '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}{%s}\n" 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"
+	      (concat
+	       "#+BEGIN_LaTeX\n"
+	       (org-add-props
+		   (cond
+		    ((and lang org-export-latex-listings)
+		     (let* ((lang-sym (intern lang))
+			    (minted-p (eq org-export-latex-listings 'minted))
+			    (listings-p (not minted-p))
+			    (backend-lang
+			     (or (cadr
+				  (assq
+				   lang-sym
+				   (cond
+				    (minted-p org-export-latex-minted-langs)
+				    (listings-p org-export-latex-listings-langs))))
+				 lang))
+			    (custom-environment
+			     (cadr
+			      (assq
+			       lang-sym
+			       org-export-latex-custom-lang-environments))))
+		       (concat
+			(when (and listings-p (not custom-environment))
+			  (format
+			   "\\lstset{%s}\n"
+			   (mapconcat
+			    (lambda (pair) (mapconcat #'identity pair "="))
+			    (append org-export-latex-listings-options
+				    `(("language" ,backend-lang))) ",")))
+			(when (and caption org-export-latex-listings-w-names)
+			  (format
+			   "\n%s $\\equiv$ \n"
+			   (replace-regexp-in-string "_" "\\\\_" caption)))
+			(cond
+			 (custom-environment
+			  (format "\\begin{%s}\n%s\\end{%s}\n"
+				  custom-environment rtn custom-environment))
+			 (listings-p
+			   (format "\\begin{%s}\n%s\\end{%s}\n"
+				   "lstlisting" rtn "lstlisting"))
+			 (minted-p
+			  (format
+			   "\\begin{minted}[%s]{%s}\n%s\\end{minted}\n"
+			   (mapconcat
+			    (lambda (pair) (mapconcat #'identity pair "="))
+			    org-export-latex-minted-options ",")
+			   backend-lang rtn))))))
+		    (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

+ 61 - 0
lisp/org-latex.el

@@ -461,6 +461,67 @@ pygmentize -L lexers
 	   (symbol :tag "Major mode       ")
 	   (string :tag "Listings language"))))
 
+(defcustom org-export-latex-listings-options nil
+  "Association list of options for the latex listings package.
+
+These options are supplied as a comma-separated list to the
+\\lstset command. Each element of the association list should be
+a list containing two strings: the name of the option, and the
+value. For example,
+
+  (setq org-export-latex-listings-options
+    '((\"basicstyle\" \"\\small\")
+      (\"keywordstyle\" \"\\color{black}\\bfseries\\underbar\")))
+
+will typeset the code in a small size font with underlined, bold
+black keywords.
+
+Note that the same options will be applied to blocks of all
+languages."
+  :group 'org-export-latex
+  :type '(repeat
+	  (list
+	   (string :tag "Listings option name ")
+	   (string :tag "Listings option value"))))
+
+(defcustom org-export-latex-minted-options nil
+  "Association list of options for the latex minted package.
+
+These options are supplied within square brackets in
+\\begin{minted} environments. Each element of the alist should be
+a list containing two strings: the name of the option, and the
+value. For example,
+
+  (setq org-export-latex-minted-options
+    '((\"bgcolor\" \"bg\") (\"frame\" \"lines\")))
+
+will result in src blocks being exported with
+
+\\begin{minted}[bgcolor=bg,frame=lines]{<LANG>}
+
+as the start of the minted environment. Note that the same
+options will be applied to blocks of all languages."
+  :group 'org-export-latex
+  :type '(repeat
+	  (list
+	   (string :tag "Minted option name ")
+	   (string :tag "Minted option value"))))
+
+(defvar org-export-latex-custom-lang-environments nil
+  "Association list mapping languages to language-specific latex
+  environments used during export of src blocks by the listings
+  and minted latex packages. For example,
+
+  (setq org-export-latex-custom-lang-environments
+     '((python \"pythoncode\")))
+
+  would have the effect that if org encounters begin_src python
+  during latex export it will output
+
+  \\begin{pythoncode}
+  <src block body>
+  \\end{pythoncode}")
+
 (defcustom org-export-latex-remove-from-headlines
   '(:todo nil :priority nil :tags nil)
   "A plist of keywords to remove from headlines.  OBSOLETE.