From mboxrd@z Thu Jan 1 00:00:00 1970 From: Dan Davison Subject: Re: How to get pretty printed source code in PDFLaTeX Date: Fri, 06 Aug 2010 09:39:42 -0400 Message-ID: <87vd7nn9lt.fsf@stats.ox.ac.uk> References: <308653.38337.qm@web65503.mail.ac4.yahoo.com> <871vacswh4.fsf@stats.ox.ac.uk> <87aap0ol2n.fsf@gmx.de> <877hk4rbco.fsf@stats.ox.ac.uk> <8762zoogf5.fsf@gmx.de> <87k4o4893x.fsf_-_@mundaneum.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Return-path: Received: from [140.186.70.92] (port=41251 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OhNj5-0008NB-C7 for emacs-orgmode@gnu.org; Fri, 06 Aug 2010 10:16:52 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OhN9Z-00024x-Bl for emacs-orgmode@gnu.org; Fri, 06 Aug 2010 09:40:06 -0400 Received: from plane.gmane.org ([80.91.229.3]:37666) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1OhN9Z-00024Z-05 for emacs-orgmode@gnu.org; Fri, 06 Aug 2010 09:40:05 -0400 Received: from public by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1OhN9N-0004dG-24 for emacs-orgmode@gnu.org; Fri, 06 Aug 2010 15:39:53 +0200 In-Reply-To: <87k4o4893x.fsf_-_@mundaneum.com> (=?utf-8?Q?=22S=C3=A9bastie?= =?utf-8?Q?n?= Vauban"'s message of "Fri, 06 Aug 2010 09:59:30 +0200") List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: =?utf-8?Q?S=C3=A9bastien?= Vauban Cc: public-emacs-orgmode-mXXj517/zsQ@plane.gmane.org S=C3=A9bastien Vauban writes: > Hi Dan and Sebastian, > > Sebastian Rose wrote: >> Dan Davison writes: >>> Can you point me to an example that shows how to make source code in la= tex >>> look (almost) as nice as html? >> >> That is supposed to work with the `listings' package. I havent tried that >> yet. >> >> I still use my old LaTeX headers and write verbatim LaTeX code. I'd >> like to switch though, since it's a lot to type. >> >> Here is an example: >> >> \begin{codeblock} >> \keyword{public function}~\func{\_\_construct}~(\increaseindent[15= ]\doindent >> \variable{\$instance}~=3D~\konstante{MAP\_STANDARD\_JS\_INSTANCE},= \doindent >> \variable{\$z}~=3D~\konstante{MAP\_STANDARD\_ZOOM},\doindent >> \variable{\$centerX}~=3D~-1,\doindent >> \variable{\$centerY}~=3D~-1,\doindent >> \variable{\$highlights}~=3D~\keyword{true},\doindent >> \variable{\$spots}~=3D~\keyword{false}\doindent >> )\resetindent >> \end{codeblock} >> >> Looks horrible, does it? > > If I understand you right, here's such an example you're after: > > #+TITLE: Using the listings package > #+AUTHOR: Seb Vauban > #+DATE: 2010-08-06 > #+LANGUAGE: en_US > > * Code > > This must be nice to see in LaTeX. > > #+SRCNAME: srcModifyDB1.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 =3D 'dossier' > AND COLUMN_NAME =3D 'DossierSentToSecteur') > BEGIN > ALTER TABLE dossier > ADD DossierSentToSecteur smalldatetime NULL > END > GO > #+END_SRC > > Right? > > * Much better code > > For that, you need to customize =3Dlistings=3D: > > #+begin_LaTeX > % typeset source code listings > \usepackage{listings} % must be loaded after `babel' > \lstloadlanguages{C} > \definecolor{sva@lstbackground}{HTML}{FFFFCC} % light yellow > \definecolor{sva@lstkeyword}{HTML}{0000FF} % blue > \definecolor{sva@lstidentifier}{HTML}{000000} % black > \definecolor{sva@lstcomment}{HTML}{FF0000} % red > \definecolor{sva@lststring}{HTML}{008000} % dark green > \lstset{% > basicstyle=3D\ttfamily\scriptsize, % the font that is used for the co= de > tabsize=3D4, % sets default tabsize to 4 spaces > numbers=3Dleft, % where to put the line numbers > numberstyle=3D\tiny, % line number font size > stepnumber=3D0, % step between two line numbers > breaklines=3Dfalse, %!! don't break long lines of code > showtabs=3Dfalse, % show tabs within strings adding particular unders= cores > showspaces=3Dfalse, % show spaces adding particular underscores > showstringspaces=3Dfalse, % underline spaces within strings > keywordstyle=3D\color{sva@lstkeyword}, > identifierstyle=3D\color{sva@lstidentifier}, > stringstyle=3D\color{sva@lststring}, > commentstyle=3D\color{sva@lstcomment}, > backgroundcolor=3D\color{sva@lstbackground}, % sets the background co= lor > captionpos=3Db, % sets the caption position to `bottom' > extendedchars=3Dfalse %!?? workaround for when the listed file is in = UTF-8 > } > #+end_LaTeX > > #+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 =3D 'dossier' > AND COLUMN_NAME =3D 'DossierSentToSecteur') > BEGIN > ALTER TABLE dossier > ADD DossierSentToSecteur smalldatetime NULL > END > GO > #+END_SRC > > with the following in my `.emacs' file: > > (setq org-export-latex-default-packages-alist > '(("AUTO" "inputenc" t) > ("T1" "fontenc" t) > ("" "fixltx2e" nil) > ("" "graphicx" t) > ("" "longtable" nil) > ("" "float" nil) > ("" "wrapfig" nil) > ("" "soul" t) > ("" "t1enc" t) > ("" "textcomp" t) > ("" "marvosym" t) > ("" "wasysym" t) > ("" "latexsym" t) > ("" "amssymb" t) > ("" "hyperref" nil) > "\\tolerance=3D1000")) > > ;; tell org to use listings (instead of verbatim) for source code > (setq org-export-latex-listings t) > > ;; if you want fontified source code, then you must include the > ;; `listings' package > (add-to-list 'org-export-latex-packages-alist '("" "listings")) > > ;; if you want colored source code, then you need to include the > ;; `xcolor' package > (add-to-list 'org-export-latex-packages-alist '("" "xcolor")) > > I've put the PDF (for easy access) onto my Web site: > > http://www.mygooglest.com/sva/ECM-Listings.pdf Wow, that's really nice. Thanks for sharing that. I think we should aim to get to a point where org-mode can produce such nicely formatted source code out-of-the-box. Maybe we could even make latex inherit the colours and fonts that emacs is currently using for source code mark up? I was going to suggest doing this with listings but then came across minted, and I wonder whether that's even more suitable? (See the other post I just made.) Dan > > Best regards, > Seb