emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Orgmode] How to get pretty printed source code in PDFLaTeX


From: Sébastien Vauban
Subject: [Orgmode] How to get pretty printed source code in PDFLaTeX
Date: Fri, 06 Aug 2010 09:59:30 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux)

Hi Dan and Sebastian,

Sebastian Rose wrote:
> Dan Davison <address@hidden> writes:
>> Can you point me to an example that shows how to make source code in latex
>> 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}~=~\konstante{MAP\_STANDARD\_JS\_INSTANCE},\doindent
>       \variable{\$z}~=~\konstante{MAP\_STANDARD\_ZOOM},\doindent
>       \variable{\$centerX}~=~-1,\doindent
>       \variable{\$centerY}~=~-1,\doindent
>       \variable{\$highlights}~=~\keyword{true},\doindent
>       \variable{\$spots}~=~\keyword{false}\doindent
>       )\resetindent
>       \end{codeblock}
>
> Looks horrible, does it?

If I understand you right, here's such an example you're after:

--8<---------------cut here---------------start------------->8---
#+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 = 'dossier'
                   AND COLUMN_NAME = 'DossierSentToSecteur')
    BEGIN
        ALTER TABLE dossier
        ADD DossierSentToSecteur smalldatetime NULL
    END
    GO
#+END_SRC

Right?

* Much better code

For that, you need to customize =listings=:

#+begin_LaTeX
% typeset source code listings
\usepackage{listings} % must be loaded after `babel'
\lstloadlanguages{C}
address@hidden % light yellow
address@hidden % blue
address@hidden % black
address@hidden % red
address@hidden % dark green
\lstset{%
    basicstyle=\ttfamily\scriptsize, % the font that is used for the code
    tabsize=4, % sets default tabsize to 4 spaces
    numbers=left, % where to put the line numbers
    numberstyle=\tiny, % line number font size
    stepnumber=0, % step between two line numbers
    breaklines=false, %!! don't break long lines of code
    showtabs=false, % show tabs within strings adding particular underscores
    showspaces=false, % show spaces adding particular underscores
    showstringspaces=false, % underline spaces within strings
    address@hidden,
    address@hidden,
    address@hidden,
    address@hidden,
    address@hidden, % sets the background color
    captionpos=b, % sets the caption position to `bottom'
    extendedchars=false %!?? 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 = 'dossier'
                   AND COLUMN_NAME = 'DossierSentToSecteur')
    BEGIN
        ALTER TABLE dossier
        ADD DossierSentToSecteur smalldatetime NULL
    END
    GO
#+END_SRC
--8<---------------cut here---------------end--------------->8---

with the following in my `.emacs' file:

--8<---------------cut here---------------start------------->8---
      (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=1000"))

      ;; 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"))
--8<---------------cut here---------------end--------------->8---

I've put the PDF (for easy access) onto my Web site:

http://www.mygooglest.com/sva/ECM-Listings.pdf

Best regards,
  Seb

-- 
Sébastien Vauban




reply via email to

[Prev in Thread] Current Thread [Next in Thread]