emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* minted for latex source code export
@ 2010-08-06 13:34 Dan Davison
  2010-08-07  7:20 ` Carsten Dominik
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Dan Davison @ 2010-08-06 13:34 UTC (permalink / raw)
  To: emacs org-mode mailing list

[-- Attachment #1: Type: text/plain, Size: 3887 bytes --]

Seb's nice listings code led me to come across this[1] blog post,
describing the latex package "minted"[2]. I've made a first-pass
implementation of org-mode latex export using minted. This didn't take
too long because I copied the original work Eric Schulte did on
listings. I think that minted may be an improvement over using listings
for exporting code from org-mode.

This pdf shows an example of export from org-mode

http://www.princeton.edu/~ddavison/software/org-minted/minted.pdf

My patch is below and in branch "minted" at
http://github.com/dandavison/org-devel.

minted does coloured syntax highlighting in ~150 languages. Unlike with
the latex listings package, no extra configuration is required to set up
the colors and fonts for different language elements. There are other
nice features such as displaying latex code in code comments, support
for unicode in the code, and (apparently) greater sophistication of the
syntax highlighters than the listings package. It uses a python library
pygments[3] to do the syntax highlighting, so imposes a requirement on
org users.

Below is the org source for the pdf, with instructions for installing
minted and pygments, and the necessary elisp configuration.

Dan

--8<---------------cut here---------------start------------->8---
#+title: latex export with minted

This demonstrates export using the [[http://code.google.com/p/minted/][minted]] package. minted is a latex
package that formats source code with syntax highlighting. It uses the
python library [[http://pygments.org/][pygments]] to do the syntax highlighting.

The org-mode patch is in branch =minted= at

git://github.com/dandavison/org-devel.git.

You will also need to get minted.sty, and the python package (see
requirements). Then, using the =minted= branch of org-mode, make the
elisp customizations in the Org config section. The source code blocks
in this file can then be exported to pdf as usual with e.g. C-c C-e d.

* Requirements
#+begin_src sh
  # Clone minted
  hg clone https://minted.googlecode.com/hg/ minted
  # Install the python syntax highlighter
  sudo aptitude install python-pygments
#+end_src

* Org config
#+begin_src emacs-lisp :results silent
   (setq org-export-latex-minted t)
   (add-to-list 'org-export-latex-packages-alist '("" "minted"))
   (setq org-latex-to-pdf-process
      '("pdflatex --shell-escape -interaction nonstopmode %s"))
#+end_src

* Example by the minted author

#+begin_src csharp
  string title = "This is a Unicode \pi in the sky"
  /*
  Defined as $\pi=\lim_{n\to\infty}\frac{P_n}{d}$ where $P$ is the perimeter
  of an $n$-sided regular polygon circumscribing a
  circle of diameter $d$.
  */
  const double pi = 3.1415926535
#+end_src


This example originally demonstrated both unicode strings and \LaTeX \nbsp
code in comments. However the unicode \pi in the string isn't working
for me at the moment, and I've replaced it with backslash pi.

* Sébastian's example
#+SRCNAME: srcModifyDB2.sql
#+BEGIN_SRC sql :tangle srcModifyDB.sql
    -- add column `DossierSentToSecteur' (if column does not exist yet)
    IF NOT EXISTS (SELECT *
                   FROM INFORMATION_SCHEMA.COLUMNS
                   WHERE TABLE_NAME = 'dossier'
                   AND COLUMN_NAME = 'DossierSentToSecteur')
    BEGIN
        ALTER TABLE dossier
        ADD DossierSentToSecteur smalldatetime NULL
    END
    GO
#+END_SRC

* Python
#+begin_src python
def tabulate(x):
    # Return dict containing values and counts
    vals = sorted(unique(x))
    return dict(zip(vals, map(lambda(val): x.count(val), vals)))
#+end_src
    
* Notes

To list minted language identifiers:

#+begin_src sh
  pygmentize -L lexers
#+end_src

#+options: toc:nil
--8<---------------cut here---------------end--------------->8---


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: minted.diff --]
[-- Type: text/x-diff, Size: 5221 bytes --]

diff --git a/lisp/org-exp.el b/lisp/org-exp.el
index 6fc5109..ce7078d 100644
--- a/lisp/org-exp.el
+++ b/lisp/org-exp.el
@@ -2276,35 +2276,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 056f1b3..f99af25 100644
--- a/lisp/org-latex.el
+++ b/lisp/org-latex.el
@@ -406,6 +406,48 @@ 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 also 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\"))"
+  :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.

[-- Attachment #3: Type: text/plain, Size: 162 bytes --]



Footnotes:

[1] http://stackoverflow.com/questions/1966425/source-code-highlighting-in-latex

[2] http://code.google.com/p/minted/

[3] http://pygments.org/




[-- Attachment #4: Type: text/plain, Size: 201 bytes --]

_______________________________________________
Emacs-orgmode mailing list
Please use `Reply All' to send replies to the list.
Emacs-orgmode@gnu.org
http://lists.gnu.org/mailman/listinfo/emacs-orgmode

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: minted for latex source code export
  2010-08-06 13:34 minted for latex source code export Dan Davison
@ 2010-08-07  7:20 ` Carsten Dominik
  2010-08-07 12:26 ` Sebastian Rose
  2010-10-04  7:51 ` Carsten Dominik
  2 siblings, 0 replies; 4+ messages in thread
From: Carsten Dominik @ 2010-08-07  7:20 UTC (permalink / raw)
  To: Dan Davison; +Cc: emacs org-mode mailing list

Hi Dan,

Looks good!

- Carsten

On Aug 6, 2010, at 3:34 PM, Dan Davison wrote:

> Seb's nice listings code led me to come across this[1] blog post,
> describing the latex package "minted"[2]. I've made a first-pass
> implementation of org-mode latex export using minted. This didn't take
> too long because I copied the original work Eric Schulte did on
> listings. I think that minted may be an improvement over using  
> listings
> for exporting code from org-mode.
>
> This pdf shows an example of export from org-mode
>
> http://www.princeton.edu/~ddavison/software/org-minted/minted.pdf
>
> My patch is below and in branch "minted" at
> http://github.com/dandavison/org-devel.
>
> minted does coloured syntax highlighting in ~150 languages. Unlike  
> with
> the latex listings package, no extra configuration is required to  
> set up
> the colors and fonts for different language elements. There are other
> nice features such as displaying latex code in code comments, support
> for unicode in the code, and (apparently) greater sophistication of  
> the
> syntax highlighters than the listings package. It uses a python  
> library
> pygments[3] to do the syntax highlighting, so imposes a requirement on
> org users.
>
> Below is the org source for the pdf, with instructions for installing
> minted and pygments, and the necessary elisp configuration.
>
> Dan
>
> --8<---------------cut here---------------start------------->8---
> #+title: latex export with minted
>
> This demonstrates export using the [[http://code.google.com/p/ 
> minted/][minted]] package. minted is a latex
> package that formats source code with syntax highlighting. It uses the
> python library [[http://pygments.org/][pygments]] to do the syntax  
> highlighting.
>
> The org-mode patch is in branch =minted= at
>
> git://github.com/dandavison/org-devel.git.
>
> You will also need to get minted.sty, and the python package (see
> requirements). Then, using the =minted= branch of org-mode, make the
> elisp customizations in the Org config section. The source code blocks
> in this file can then be exported to pdf as usual with e.g. C-c C-e d.
>
> * Requirements
> #+begin_src sh
>  # Clone minted
>  hg clone https://minted.googlecode.com/hg/ minted
>  # Install the python syntax highlighter
>  sudo aptitude install python-pygments
> #+end_src
>
> * Org config
> #+begin_src emacs-lisp :results silent
>   (setq org-export-latex-minted t)
>   (add-to-list 'org-export-latex-packages-alist '("" "minted"))
>   (setq org-latex-to-pdf-process
>      '("pdflatex --shell-escape -interaction nonstopmode %s"))
> #+end_src
>
> * Example by the minted author
>
> #+begin_src csharp
>  string title = "This is a Unicode \pi in the sky"
>  /*
>  Defined as $\pi=\lim_{n\to\infty}\frac{P_n}{d}$ where $P$ is the  
> perimeter
>  of an $n$-sided regular polygon circumscribing a
>  circle of diameter $d$.
>  */
>  const double pi = 3.1415926535
> #+end_src
>
>
> This example originally demonstrated both unicode strings and \LaTeX  
> \nbsp
> code in comments. However the unicode \pi in the string isn't working
> for me at the moment, and I've replaced it with backslash pi.
>
> * Sébastian's example
> #+SRCNAME: srcModifyDB2.sql
> #+BEGIN_SRC sql :tangle srcModifyDB.sql
>    -- add column `DossierSentToSecteur' (if column does not exist yet)
>    IF NOT EXISTS (SELECT *
>                   FROM INFORMATION_SCHEMA.COLUMNS
>                   WHERE TABLE_NAME = 'dossier'
>                   AND COLUMN_NAME = 'DossierSentToSecteur')
>    BEGIN
>        ALTER TABLE dossier
>        ADD DossierSentToSecteur smalldatetime NULL
>    END
>    GO
> #+END_SRC
>
> * Python
> #+begin_src python
> def tabulate(x):
>    # Return dict containing values and counts
>    vals = sorted(unique(x))
>    return dict(zip(vals, map(lambda(val): x.count(val), vals)))
> #+end_src
>
> * Notes
>
> To list minted language identifiers:
>
> #+begin_src sh
>  pygmentize -L lexers
> #+end_src
>
> #+options: toc:nil
> --8<---------------cut here---------------end--------------->8---
>
> <minted.diff>
>
> Footnotes:
>
> [1] http://stackoverflow.com/questions/1966425/source-code-highlighting-in-latex
>
> [2] http://code.google.com/p/minted/
>
> [3] http://pygments.org/
>
>
>
> _______________________________________________
> Emacs-orgmode mailing list
> Please use `Reply All' to send replies to the list.
> Emacs-orgmode@gnu.org
> http://lists.gnu.org/mailman/listinfo/emacs-orgmode

- Carsten

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: minted for latex source code export
  2010-08-06 13:34 minted for latex source code export Dan Davison
  2010-08-07  7:20 ` Carsten Dominik
@ 2010-08-07 12:26 ` Sebastian Rose
  2010-10-04  7:51 ` Carsten Dominik
  2 siblings, 0 replies; 4+ messages in thread
From: Sebastian Rose @ 2010-08-07 12:26 UTC (permalink / raw)
  To: Dan Davison; +Cc: emacs org-mode mailing list

Dan Davison <davison@stats.ox.ac.uk> writes:
> Seb's nice listings code led me to come across this[1] blog post,
> describing the latex package "minted"[2]. I've made a first-pass
> implementation of org-mode latex export using minted. This didn't take
> too long because I copied the original work Eric Schulte did on
> listings. I think that minted may be an improvement over using listings
> for exporting code from org-mode.
>
> This pdf shows an example of export from org-mode
>
> http://www.princeton.edu/~ddavison/software/org-minted/minted.pdf
>
> My patch is below and in branch "minted" at
> http://github.com/dandavison/org-devel.
>
> minted does coloured syntax highlighting in ~150 languages. Unlike with
> the latex listings package, no extra configuration is required to set up
> the colors and fonts for different language elements. There are other
> nice features such as displaying latex code in code comments, support
> for unicode in the code, and (apparently) greater sophistication of the
> syntax highlighters than the listings package. It uses a python library
> pygments[3] to do the syntax highlighting, so imposes a requirement on
> org users.



Hm --- the listings package comes with TexLive so no extra installation
is neccessary on many systems.  And you can define your own languages.


But no configuration of the documents is nice, too :)


    Sebastian

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: minted for latex source code export
  2010-08-06 13:34 minted for latex source code export Dan Davison
  2010-08-07  7:20 ` Carsten Dominik
  2010-08-07 12:26 ` Sebastian Rose
@ 2010-10-04  7:51 ` Carsten Dominik
  2 siblings, 0 replies; 4+ messages in thread
From: Carsten Dominik @ 2010-10-04  7:51 UTC (permalink / raw)
  To: Dan Davison, Sebastian Rose; +Cc: emacs org-mode mailing list

Hi Dan,

I think there are no objections to this patch.  Some documentation  
will be needed, maybe on Work, with a link from the manual?

Sebastian, are we mentioning the listing package in the documentation?

Thanks

- Carsten

On Aug 6, 2010, at 3:34 PM, Dan Davison wrote:

> Seb's nice listings code led me to come across this[1] blog post,
> describing the latex package "minted"[2]. I've made a first-pass
> implementation of org-mode latex export using minted. This didn't take
> too long because I copied the original work Eric Schulte did on
> listings. I think that minted may be an improvement over using  
> listings
> for exporting code from org-mode.
>
> This pdf shows an example of export from org-mode
>
> http://www.princeton.edu/~ddavison/software/org-minted/minted.pdf
>
> My patch is below and in branch "minted" at
> http://github.com/dandavison/org-devel.
>
> minted does coloured syntax highlighting in ~150 languages. Unlike  
> with
> the latex listings package, no extra configuration is required to  
> set up
> the colors and fonts for different language elements. There are other
> nice features such as displaying latex code in code comments, support
> for unicode in the code, and (apparently) greater sophistication of  
> the
> syntax highlighters than the listings package. It uses a python  
> library
> pygments[3] to do the syntax highlighting, so imposes a requirement on
> org users.
>
> Below is the org source for the pdf, with instructions for installing
> minted and pygments, and the necessary elisp configuration.
>
> Dan
>
> --8<---------------cut here---------------start------------->8---
> #+title: latex export with minted
>
> This demonstrates export using the [[http://code.google.com/p/ 
> minted/][minted]] package. minted is a latex
> package that formats source code with syntax highlighting. It uses the
> python library [[http://pygments.org/][pygments]] to do the syntax  
> highlighting.
>
> The org-mode patch is in branch =minted= at
>
> git://github.com/dandavison/org-devel.git.
>
> You will also need to get minted.sty, and the python package (see
> requirements). Then, using the =minted= branch of org-mode, make the
> elisp customizations in the Org config section. The source code blocks
> in this file can then be exported to pdf as usual with e.g. C-c C-e d.
>
> * Requirements
> #+begin_src sh
>  # Clone minted
>  hg clone https://minted.googlecode.com/hg/ minted
>  # Install the python syntax highlighter
>  sudo aptitude install python-pygments
> #+end_src
>
> * Org config
> #+begin_src emacs-lisp :results silent
>   (setq org-export-latex-minted t)
>   (add-to-list 'org-export-latex-packages-alist '("" "minted"))
>   (setq org-latex-to-pdf-process
>      '("pdflatex --shell-escape -interaction nonstopmode %s"))
> #+end_src
>
> * Example by the minted author
>
> #+begin_src csharp
>  string title = "This is a Unicode \pi in the sky"
>  /*
>  Defined as $\pi=\lim_{n\to\infty}\frac{P_n}{d}$ where $P$ is the  
> perimeter
>  of an $n$-sided regular polygon circumscribing a
>  circle of diameter $d$.
>  */
>  const double pi = 3.1415926535
> #+end_src
>
>
> This example originally demonstrated both unicode strings and \LaTeX  
> \nbsp
> code in comments. However the unicode \pi in the string isn't working
> for me at the moment, and I've replaced it with backslash pi.
>
> * Sébastian's example
> #+SRCNAME: srcModifyDB2.sql
> #+BEGIN_SRC sql :tangle srcModifyDB.sql
>    -- add column `DossierSentToSecteur' (if column does not exist yet)
>    IF NOT EXISTS (SELECT *
>                   FROM INFORMATION_SCHEMA.COLUMNS
>                   WHERE TABLE_NAME = 'dossier'
>                   AND COLUMN_NAME = 'DossierSentToSecteur')
>    BEGIN
>        ALTER TABLE dossier
>        ADD DossierSentToSecteur smalldatetime NULL
>    END
>    GO
> #+END_SRC
>
> * Python
> #+begin_src python
> def tabulate(x):
>    # Return dict containing values and counts
>    vals = sorted(unique(x))
>    return dict(zip(vals, map(lambda(val): x.count(val), vals)))
> #+end_src
>
> * Notes
>
> To list minted language identifiers:
>
> #+begin_src sh
>  pygmentize -L lexers
> #+end_src
>
> #+options: toc:nil
> --8<---------------cut here---------------end--------------->8---
>
> <minted.diff>
>
> Footnotes:
>
> [1] http://stackoverflow.com/questions/1966425/source-code-highlighting-in-latex
>
> [2] http://code.google.com/p/minted/
>
> [3] http://pygments.org/
>
>
>
> _______________________________________________
> Emacs-orgmode mailing list
> Please use `Reply All' to send replies to the list.
> Emacs-orgmode@gnu.org
> http://lists.gnu.org/mailman/listinfo/emacs-orgmode

- Carsten

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2010-10-04  7:52 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-08-06 13:34 minted for latex source code export Dan Davison
2010-08-07  7:20 ` Carsten Dominik
2010-08-07 12:26 ` Sebastian Rose
2010-10-04  7:51 ` Carsten Dominik

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).