Latex

Disclaimer: After so many years (and blog migrations), this article might be broken or outdated. I try to keep the information here as relevant as possible but please let me know if you find any problems while reading this article

Latex!

800px-latex_logosvg

Some time ago I wrote a review about LyX for DPOTD (now dead) and now I think it’s time for an update: what can you do when LyX is not enough?

I’ll quote myself; the original article ends (almost) like this:

What LyX isn’t for
Although LyX may be a valuable piece in anyone toolkit it’s worth noticing it isn’t exactly the Swiss army knife of the text editors. If you need to define a very customized layout or format, like slides for a presentation, this is the wrong tool for the job.

After having prepared several presentations with LyX (even my CV!) I’d like to write some more on this last part. Even if I thought that my original comment was mistaken I write once again the same: when you have to create a presentation LyX is not the tool for the job, LaTeX is.

As I explained in that article, LyX is a front-end for LaTeX but at the same time it’s only a small part of LaTeX. Doing several presentations in LyX (using Beamer, a package I’ll talk about in another post) I noticed that most of my documents where “ERT”, evil red text, which is nothing else than plain TeX code: that means, each time more, I was using LaTeX functionality not available from LyX. This is the reason I’ve decided to write a how-to LaTeX, a small survival guide for those want to start using with the tips and tricks I’ve learned in this time.

The target for these posts series will be, mainly, programmers or other advanced users, without fear of working a little bit in the console, markup languages and maybe a makefile.

Soon the first part 🙂

——————————————————————————————————-

A case for LaTeX

lion

So, after struggling with you word processor to get an acceptable format you decide to hunt the repos (you’re using Linux, right?) for a better alternative. We already know why would you choose LyX but why would you choose LaTeX?

Let’s begin by listing some of its advantages

  • Wide range of applications – LaTeX is a widely used format, with lots of years, users and fields of application. It’s not used only to create papers or just presentations, it can be used in a wide range of applications.
  • There are “packages” prepared for each type of document – related to the previous point, you can find packages to create a CV, a presentation, posters, etc.
  • No need to format every single element – everything comes with a reasonable default format. You may fight a little bit whenever you need to change the default format (though you’ll find it less necessary each day) but 90 percent of the time “it just works”. With regular word processors it’s usually the other way around (it is for me!)
  • Works from a console – OK, this may be an advantage or a disadvantage, according to whom you ask. It sure is an advantage for us, the crazy console people, who don’t like to drop the CLI just for a couple of slides.
  • Great templates – no need to work in the design of the documents, that’s already taken care of.
  • Plain text format – easy to integrate with any source code management system.
  • Easy to integrate with external media (images, for example) – did you just change that image? No problem, just “recompile” to get it on your document.
  • The best math system

It has some downsides too

  • Steep learning curve
  • Requires some familiarity with the console
  • There’s a lot of markup to learn – this can lead to a low signal to noise ratio for some packages like Beamer and the need to RTFM more often than usual.
  • Creating a LaTeX document requires certain syntax – it’s not quite like programming, but programmers will be much more comfortable around it
  • Weird error messages – the LaTeX compiler will create an enormous amount of output and error messages may not always be easy to comprehend.
  • It’s a one way ticket – you may be Word – impaired for the rest of your life
  • Who should use LaTeX?

    As previously stated, programmers will be more comfortable around plain LaTeX but that’s not the only prerequisite – you should be somewhat familiar with the console in Linux (no idea about LaTeX on Windows but I do know it’s possible) and have some patience to read a couple of manuals. Once you have mastered the basics it’s much easier, so don’t get discouraged.

    If you feel you don’t fit the profile perhaps LyX is a better alternative than plain LaTeX; it’s very easy to use and quite user friendly. It won’t be that great for presentations and other, more “advanced”, documents though.

    What kind of work can you do with LaTeX?

    That’s an easy one, check My Articles section for some examples. You can create any kind of document but it’s better suited for those in which you care most about the content and don’t want to get bogged down with perky design details.

    Editors

    Last (for this entry) but not least, what editor can you use to create LaTeX documents? Well, LaTeX is basically plain text so any editor will do. There are some LaTeX-specific editors, I like ViM myself. There’ll be a complete post dedicated to LaTeX + Vim, coming soon.

    This article will be the first in a series of LaTeX survival guide so stay tuned.

    ——————————————————————————————————-

    LaTeX basics

    Remember last post? I mentioned its possible to create several type of documents. Even if the structure will, most likely, be different for different applications, there’s a common ground which can be used for most LaTeX documents. I’d go as far as saying ALL of them, but in LaTeX it’s possible to override everything and anything. Any reasonable package won’t do it, so let’s see some LaTeX basics you can use for any project.

    typesetting

    LaTeX documents

    The first thing to understand about LaTeX, it’s not WYSIWYG. If you use a plain text editor you won’t see any kind of format at all (LyX provides some sort of preview though). This confuses some people who, used to Word-like applications, tend to think in the structure and desing before the contents; LaTeX is different in the way to structure documents, a way which is called What You See Is What You Mean, WYSIWYM, for short.

    What does being WYSIWYM means? You just think about the content and structure and it gets formatted for you. Instead of saying you want “Centered, Arial 12px orange font with pink background” just say “this is a title”. The format will be there, but not attached to the content – it’ll be in a “stylesheet”, the document’s class (*). Of course, these styles can be changed too

    WYSIWYM has a downside too: you’ll have to “compile” the mark-up text into a pdf (or any other format) using pdflatex, for example. It may be a shock to some but it’s not difficult, just write “pdf2latex file.tex” and let it work its magic. You can use the makefile I’m attaching to this post too, if you feel comfortable with makefiles: just check at the bottom of this post. The makefile has a few cool tricks you can use too. I’ll explain all of it in another entry).

    Seeing some examples

    In GNU/Linux you can find lots of examples and templates in /usr/share/doc/texlive-doc/latex/ (no idea where may they be in Windows). Writing this article I found out lots of templates I didn’t know about so go and take a look. Go on, I’ll wait here. Done? Ok, let’s move on.

    Preamble

    In LaTeX there’s a basic structure every document follows:

    % Preamble
    begin{document}
    end{document}
    

    What comes before the begin‘ning is the preamble. There you can tell LaTeX:

    • Which packages are you going to use – there are lots of packages, some examples include source code highlighting, including images, absolute positioning of figures and much more. Include them with the usepackage command
    • The style – there are stylesheets you can use, just as you do with HTML, and the preamble is the place to define them. How to do it is package-dependent, Beamer, for example, uses a usetheme tag.
    • Global declarations – You may declare an image which you’ll use throughout the entire document, you’re company’s logo for example
    • The author(s) – OK, this is a global declaration too, but there is certain meta-data which is declared in the preamble that’s quite handy and author is one of them.
    • The title – same as before, the title is very important in your document!
    • Lots more – there are package dependent declarations so there’s no way to create a comprehensive list, just remember it’s a place to put global settings

    This post is getting a little bit too large so I’ll leave some basic commands for the next one; you should be able to start writing some basic documents anyway.

    (*) <Rant mode> Yeah, something like content and style separation but only a couple of decades before it was “invented” for the WWW.</Rant mode>

    ——————————————————————————————————-

    LaTeX: format basics

    Basic structure

    tex

    So, what’s the most basic structural elements for any document you write? Paragraphs, of course. I usually use LyX for writing paragraph-based documents (i.e. most of them) and presentations don’t have many, so perhaps this is the structural element I use the least. Oh, wait, there’s no element, just a “nn”. Point for LaTeX (take that, HTML).

    You’ll also need to put titles, sections, subtitles and all that stuff to give your document a nice structure. Well, that’s easy too, just use

    • section{name}
    • subsection{name}
    • subsubsection{name}

    Easy, right? Check the example at the end of the post. There are some other commands you’ll use in any LaTeX document:

    • Footnotes
      footnote{ Footnote text }

      .
      Just write your text, it’ll be there when you compile the document

    • Vertical skip
      bigskip

      Because, some times, LaTeX default formatting won’t be enough.

    • Align text Center:
      begin{center}TEXTend{center}

      Right:

      begin{flushright}TEXTend{flushright}

      Again, some times you may need it. Not too often, though.

    You should now be able to write your first LaTeX document, starting from a template (always start with a template… it’s easier). I’m attaching to this post an example document in LyX, LaTeX and its compiled pdf, in case you’re wondering what does it look like in the end. For the next entry: some “advanced” tips and tricks, now let’s see an example LaTeX document:

    \documentstyle[11pt]{article}
    \setlength{\topmargin}{-.5in}
    \setlength{\oddsidemargin}{.125in}
    \setlength{\textwidth}{6.25in}
    
    
    \title{LaTeX Example}
    \author{Nicol\'as Brailovsky}
    
    \begin{document}
    
    \maketitle
    This is just a LaTeX kick off, with some basic commands.
    
    \section {Section one}
    For example, this is a paragraph.
    \begin{quotation}
        { em Lorem ipsum dolor sit amet, consectetur adipiscing elit.}
        Donec porta, enim eget tempus tempor, eros sem dapibus diam, vitae lacinia mauris metus id nulla.
    \end{quotation}
    
    \subsection{Subsection}
    You can have subsections too \ldots
    
    \subsection{Subsection}
        \ldots as many as you want
    
    \subsection{Random stuff}
    You can even create a shopping list in LaTeX, if you wish
    \begin{center}
    \begin{itemize}
        \item Beer
        \item Beer
    	\begin{itemize}
    		\item (Another brand of beer)
    	\end{itemize}
        \item Pizza
        \item Beer
    \end{itemize}
    \end{center}
    
    Or even better, use it to do the homework. \
    Math is a breeze in LaTeX: $x_n = sqrt{a + b}$ can be typeset inline.
    \end{document}
    
    

    May be it doesn’t look too useful (I’d use LyX instead) but soon we’ll start doing some cool stuff LyX can’t handle. Keep tuned for the next LaTeX article.

    ——————————————————————————————————-

    LaTeX: LyX Revisited

    Seeing I’m publishing a new LaTeX series it seems appropriate to review my old LyX article. This is just a copy & paste from http://debaday.debian.net/2008/01/20/lyx-a-text-editor-that-stays-out-of-the-way/ (The link is now dead, unfortunately).

    lyx_logo_hi

    Did you ever get to struggle against your text editor’s random format feature while trying to write a document? Open Office may be a great project, but when you want to focus on the content, it can be annoying to have your editor format or unformat your text, seemingly at random.

    Well there are good news for those of us using Vim to create content and then Abiword to format it: LyX is a text editor that produces beautiful documents, without the need of being a designer, and yet manages to stay out of the way. From the tutorial and the homepage (www.lyx.org):

    LyX is the first WYSIWYM (What You See Is What You Mean) document processor. The basic idea of LyX is that you do not need to handle style, or actually, you use a set of predefined styles and concentrate on your document content, This makes sure that your resulting document will be typographically correct and good looking visually. […] LyX uses Latex as its back end typesetting mechanism.

    Sounds great already, doesn’t it?

    A first look into LyX

    Upon start LyX looks more or less like any other graphical text editor in the universe:

    Well, it’s logo may look nicer, but that’s about it. Anyway, the magic starts just as you start writing: you’ll notice most of the common format options seems missing, but you can define what you’re writing instead:

    Note that we don’t tell it to center it or to make the font larger and bold. LyX takes care of all that automatically. Simply click on the format menu (below File, and it has the default value of “Standard”).

    So instead of defining Times New Roman 12px bold centered, you say «Title». WYSIWYM, remember? In the homepage there is a «Graphical Tour» with all the basic functions, it’s quick and it’s great: http://www.lyx.org/LGT

    Some useful features

    LyX also provides a great support for math formulas (and all the weird symbols you can think off). Just click the button «Insert Equation» and a box to enter math symbols will appear. No more struggle to align the dividend and the divisor!

    Of course, LyX provides the usual features such as tables, spell checking, footnotes and many more. The tutorial of the application is more than complete, and easy to follow.
    LyX documents formats

    LyX documents can be exported to a wide variety of formats, mainly because being based on Latex it takes advantage of the already existing conversion programs. Some of the possible export plugins installed by default are PS, PDF, DVI, Latex, HTML and Plain text, but custom ones may be defined.
    What LyX isn’t for

    Although LyX may be a valuable piece in anyone toolkit it’s worth noticing it isn’t exactly the Swiss army knife of the text editors. If you need to define a very customized layout or format, like slides for a presentation, this is the wrong tool for the job.

    Availability

    According to it’s homepage, LyX 1.5.3 was released the 16 th of December, 2007. It’s available in Debian since Sarge (packages.debian.org/LyX). Lyx Version 1.5.1, released 4 th of August, 2007, is available in the repository of Ubuntu 7.10. Development is still active. There’s also a Windows version for those of us stuck with primitive a OS at work.

    ——————————————————————————————————-

    LaTeX: Makefile

    latexmk

    Remember I said that being a programmer would make you a lot more comfortable around LaTeX? The reason is quite simple, tex is just source code for a document. As with any source code in Linux (Windows too, but that is besides the point) you can use a Makefile to compile it and make your life easier.

    I have already posted this Makefile in another entry but it’s time to explain how it works.

    all: main.pdf
    
    main.pdf: *.tex
    	# We need to run it twice: the first creates the TOC
    	pdflatex main.tex &amp;&amp; pdflatex main.tex 
    
    .PHONY: run clean
    
    run: main.pdf
    	evince main.pdf &amp;
    
    clean:
    	rm -f *.out *.nav *.aux *.toc *.log *.snm *.pdf *.vrb
    

    It’s rather easy, let’s check it target by target:

    • all: create the main document – used as default target
    • main.pdf: document’s target – no need to call it main.pdf, I just do it because the entry point in C programs is called “main” so I’m used to it. Also, it looks better than foo.pdf. As a side note, it runs twice “pdflatex” because it first creates the document and the second time updates the document’s index.
    • run: target to open up the document in a pdf viewer. I call it run so I can use it with my default mapping in Vim
    • clean: clean up the files created while compiling a document. Some times it’s needed if there’s an error you can track – it may be a corrupted .aux file

    Short entry this time – next: using source code from within LaTeX.

    ——————————————————————————————————-

    LaTeX: including documents

    This is a post from my LaTeX series – check the others too!

    So far we’ve seen some of LaTeX advantages, and a few basic commands to get you started. Let’s see a trick to be more proficient with it:

    Including other tex files

    tex

    You should be able to write some simple documents now, some in LyX, some in LaTeX, but you’ll soon start to notice that using a single text file to create a large document becomes cumbersome. Even more so if you need to split the work between several people in a team.

    There’s an easy way to keep a main file and then several, smaller, files in which you can work more comfortably:

    \include{file.tex}

    Easy, right? Beware, you can’t use an include inside an include. Why? No idea, but there’s a way around this:

    \input{file.tex}

    Quick preview

    Using includes has another advantage: you can have a quick preview while working with a chapter at a time. I usually keep the following structure within my projects:

    % Header declarations
    % Include packages
    % Document preamble
    % ...
    
    % \input{chapter1.tex}
    % \input{chapter2.tex}
    \input{chapter3.tex}
    % \input{chapter4.tex}
    

    Just uncomment the chapter you’re working with. In big documents this has a very noticeable effect, as compiling a large LaTeX file into an enormous pdf document (several MBs) may be quite slow.

    Of course, I use “input” in my main file so I can use include in the chapters themselves. I won’t usually need to include other documents inside the chapters, it would get quite messy, but it’s necessary to work with embedded documents as we’ll see in another post.

    ——————————————————————————————————-

    LaTeX: Including Source Code

    source_code

    I’m sure every programmer has had to include source code in a document at least once, an architecture documentation for example, or perhaps as part of a presentation (beamer FTW, coming soon!).

    With most document formats including source code usually means writing the code and use some kind of manual syntax highlighting. Of course, any time you change the source you’ll need to manually update the document.

    In LaTeX there’s an easy way to handle source code. Though it was difficult to figure out, once in place this requires no maintenance at all – no need to manually syntax highlight your code nor update any files other than the source code itself. Even more, you can even change the source code language and it won’t even matter in the presentation.

    Preliminaries

    You’ll need “pygmentyze” which is provided by the package python-pygments:

    apt-cache search pygment
    python-pygments - syntax highlighting package written in Python
    sudo apt-get install python-pygments
    

    Using it is really easy, check the manpage. Anyway, I’m too lazy to generate the syntax highlighted document by hand, let’s add it to the makefile.

    Automating the source code generation

    Let’s begin by adding a “code” folder inside our project. We’ll store there all the source code files, and having them in a separated folder will enable us to add a target in the makefile to automatically update the source code in the LaTeX file.

    Remember the makefile from a couple of posts back? Let’s change it a bit:

    # Hack to make it work when foo.code =&gt; foo.code.tex
    code/%.tex: code/%.* code/%.aux code/%.tex
    	@rm code/*.aux
    	$(MAKE) -C .
    
    code/%.tex: code/%.*
    	pygmentize -f latex -O style='border=#000000,colorful,linenos=1' $&lt; &gt; $@
    
    # Search each code file to format and include
    CODE_FILES:=$(shell ls code/|egrep -v '.tex$ |.aux$ ' )
    CODE_FILES_DEP:=$(addprefix code/, $(CODE_FILES))
    CODE_FILES_TGT:=$(addsuffix .tex, $(basename $(CODE_FILES_DEP)))
    
    main.pdf: $(shell ls *.tex) $(CODE_FILES_TGT)
    	pdflatex main.tex &gt; /dev/null
    
    all: main.pdf
    
    .PHONY: run clean
    
    run: main.pdf
    	evince main.pdf &amp;
    
    clean:
    	rm -f *.out *.nav *.aux *.toc *.log *.snm *.pdf *.vrb
    

    OK, maybe more than just a bit. It may seem like a lot of makefile code but all it does is define a code directory and a target to run pygmentize on each source file found there. We’ll have to add a dependency in the document’s target so it’ll be automatically generated with each build. If you use this makefile it will now pygmentize any source code file which may sit on the code directory.

    Use the source

    There is one more step to go: you should now be able to compile your source files into pygmentized versions, to be included in latex. To do this just use the \include directive. The problem is, the file you just included will have a lot of macros and unmet dependencies. To fix this you will have to run pygmentize again, on any file, adding a -O full. This will print all the dependencies you need; just copy and paste that into your main tex file, and you are good to go. This is what I had to add for my example file, but it may vary in your case:

    \usepackage{fancyvrb}
    \usepackage{color}
    
    \makeatletter
    \def\PY@reset{\let\PY@it=\relax \let\PY@bf=\relax%
        \let\PY@ul=\relax \let\PY@tc=\relax%
        \let\PY@bc=\relax \let\PY@ff=\relax}
    \def\PY@tok#1{\csname PY@tok@#1\endcsname}
    \def\PY@toks#1+{\ifx\relax#1\empty\else%
        \PY@tok{#1}\expandafter\PY@toks\fi}
    \def\PY@do#1{\PY@bc{\PY@tc{\PY@ul{%
        \PY@it{\PY@bf{\PY@ff{#1}}}}}}}
    \def\PY#1#2{\PY@reset\PY@toks#1+\relax+\PY@do{#2}}
    
    \def\PY@tok@gd{\def\PY@tc##1{\textcolor[rgb]{0.63,0.00,0.00}{##1}}}
    \def\PY@tok@gu{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.50,0.00,0.50}{##1}}}
    \def\PY@tok@gt{\def\PY@tc##1{\textcolor[rgb]{0.00,0.25,0.82}{##1}}}
    \def\PY@tok@gs{\let\PY@bf=\textbf}
    \def\PY@tok@gr{\def\PY@tc##1{\textcolor[rgb]{1.00,0.00,0.00}{##1}}}
    \def\PY@tok@cm{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}}
    \def\PY@tok@vg{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
    \def\PY@tok@m{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
    \def\PY@tok@mh{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
    \def\PY@tok@go{\def\PY@tc##1{\textcolor[rgb]{0.50,0.50,0.50}{##1}}}
    \def\PY@tok@ge{\let\PY@it=\textit}
    \def\PY@tok@vc{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
    \def\PY@tok@il{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
    \def\PY@tok@cs{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}}
    \def\PY@tok@cp{\def\PY@tc##1{\textcolor[rgb]{0.74,0.48,0.00}{##1}}}
    \def\PY@tok@gi{\def\PY@tc##1{\textcolor[rgb]{0.00,0.63,0.00}{##1}}}
    \def\PY@tok@gh{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,0.50}{##1}}}
    \def\PY@tok@ni{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.60,0.60,0.60}{##1}}}
    \def\PY@tok@nl{\def\PY@tc##1{\textcolor[rgb]{0.63,0.63,0.00}{##1}}}
    \def\PY@tok@nn{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}}
    \def\PY@tok@no{\def\PY@tc##1{\textcolor[rgb]{0.53,0.00,0.00}{##1}}}
    \def\PY@tok@na{\def\PY@tc##1{\textcolor[rgb]{0.49,0.56,0.16}{##1}}}
    \def\PY@tok@nb{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
    \def\PY@tok@nc{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}}
    \def\PY@tok@nd{\def\PY@tc##1{\textcolor[rgb]{0.67,0.13,1.00}{##1}}}
    \def\PY@tok@ne{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.82,0.25,0.23}{##1}}}
    \def\PY@tok@nf{\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}}
    \def\PY@tok@si{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.53}{##1}}}
    \def\PY@tok@s2{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
    \def\PY@tok@vi{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
    \def\PY@tok@nt{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
    \def\PY@tok@nv{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
    \def\PY@tok@s1{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
    \def\PY@tok@sh{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
    \def\PY@tok@sc{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
    \def\PY@tok@sx{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
    \def\PY@tok@bp{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
    \def\PY@tok@c1{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}}
    \def\PY@tok@kc{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
    \def\PY@tok@c{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}}
    \def\PY@tok@mf{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
    \def\PY@tok@err{\def\PY@bc##1{\fcolorbox[rgb]{1.00,0.00,0.00}{1,1,1}{##1}}}
    \def\PY@tok@kd{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
    \def\PY@tok@ss{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
    \def\PY@tok@sr{\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.53}{##1}}}
    \def\PY@tok@mo{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
    \def\PY@tok@kn{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
    \def\PY@tok@mi{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
    \def\PY@tok@gp{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,0.50}{##1}}}
    \def\PY@tok@o{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
    \def\PY@tok@kr{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
    \def\PY@tok@s{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
    \def\PY@tok@kp{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
    \def\PY@tok@w{\def\PY@tc##1{\textcolor[rgb]{0.73,0.73,0.73}{##1}}}
    \def\PY@tok@kt{\def\PY@tc##1{\textcolor[rgb]{0.69,0.00,0.25}{##1}}}
    \def\PY@tok@ow{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.67,0.13,1.00}{##1}}}
    \def\PY@tok@sb{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
    \def\PY@tok@k{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
    \def\PY@tok@se{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.13}{##1}}}
    \def\PY@tok@sd{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
    
    \def\PYZbs{\char`\\}
    \def\PYZus{\char`\_}
    \def\PYZob{\char`\{}
    \def\PYZcb{\char`\}}
    \def\PYZca{\char`\^}
    \def\PYZsh{\char`\#}
    \def\PYZpc{\char`\%}
    \def\PYZdl{\char`\$}
    \def\PYZti{\char`\~}
    % for compatibility with earlier versions
    \def\PYZat{@}
    \def\PYZlb{[}
    \def\PYZrb{]}
    \makeatother
    

    At last!

    It shouldn’t have been too much work and we’re done anyway. To include a source code file in your document now use the include command (like include{code/foobar.cpp) and re build. I’m attaching a complete example in a zip file, with my latest implementation of a bogosort algorithm (now 50% faster).

    Full main.tex file:

    \documentclass{article}
    \setlength{\topmargin}{-.5in}
    \setlength{\oddsidemargin}{.125in}
    \setlength{\textwidth}{6.25in}
    
    \date{May 28, 2009}
    
    \usepackage{fancyvrb}
    \usepackage{color}
    
    \makeatletter
    \def\PY@reset{\let\PY@it=\relax \let\PY@bf=\relax%
        \let\PY@ul=\relax \let\PY@tc=\relax%
        \let\PY@bc=\relax \let\PY@ff=\relax}
    \def\PY@tok#1{\csname PY@tok@#1\endcsname}
    \def\PY@toks#1+{\ifx\relax#1\empty\else%
        \PY@tok{#1}\expandafter\PY@toks\fi}
    \def\PY@do#1{\PY@bc{\PY@tc{\PY@ul{%
        \PY@it{\PY@bf{\PY@ff{#1}}}}}}}
    \def\PY#1#2{\PY@reset\PY@toks#1+\relax+\PY@do{#2}}
    
    \def\PY@tok@gd{\def\PY@tc##1{\textcolor[rgb]{0.63,0.00,0.00}{##1}}}
    \def\PY@tok@gu{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.50,0.00,0.50}{##1}}}
    \def\PY@tok@gt{\def\PY@tc##1{\textcolor[rgb]{0.00,0.25,0.82}{##1}}}
    \def\PY@tok@gs{\let\PY@bf=\textbf}
    \def\PY@tok@gr{\def\PY@tc##1{\textcolor[rgb]{1.00,0.00,0.00}{##1}}}
    \def\PY@tok@cm{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}}
    \def\PY@tok@vg{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
    \def\PY@tok@m{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
    \def\PY@tok@mh{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
    \def\PY@tok@go{\def\PY@tc##1{\textcolor[rgb]{0.50,0.50,0.50}{##1}}}
    \def\PY@tok@ge{\let\PY@it=\textit}
    \def\PY@tok@vc{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
    \def\PY@tok@il{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
    \def\PY@tok@cs{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}}
    \def\PY@tok@cp{\def\PY@tc##1{\textcolor[rgb]{0.74,0.48,0.00}{##1}}}
    \def\PY@tok@gi{\def\PY@tc##1{\textcolor[rgb]{0.00,0.63,0.00}{##1}}}
    \def\PY@tok@gh{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,0.50}{##1}}}
    \def\PY@tok@ni{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.60,0.60,0.60}{##1}}}
    \def\PY@tok@nl{\def\PY@tc##1{\textcolor[rgb]{0.63,0.63,0.00}{##1}}}
    \def\PY@tok@nn{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}}
    \def\PY@tok@no{\def\PY@tc##1{\textcolor[rgb]{0.53,0.00,0.00}{##1}}}
    \def\PY@tok@na{\def\PY@tc##1{\textcolor[rgb]{0.49,0.56,0.16}{##1}}}
    \def\PY@tok@nb{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
    \def\PY@tok@nc{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}}
    \def\PY@tok@nd{\def\PY@tc##1{\textcolor[rgb]{0.67,0.13,1.00}{##1}}}
    \def\PY@tok@ne{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.82,0.25,0.23}{##1}}}
    \def\PY@tok@nf{\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}}
    \def\PY@tok@si{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.53}{##1}}}
    \def\PY@tok@s2{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
    \def\PY@tok@vi{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
    \def\PY@tok@nt{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
    \def\PY@tok@nv{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
    \def\PY@tok@s1{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
    \def\PY@tok@sh{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
    \def\PY@tok@sc{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
    \def\PY@tok@sx{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
    \def\PY@tok@bp{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
    \def\PY@tok@c1{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}}
    \def\PY@tok@kc{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
    \def\PY@tok@c{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}}
    \def\PY@tok@mf{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
    \def\PY@tok@err{\def\PY@bc##1{\fcolorbox[rgb]{1.00,0.00,0.00}{1,1,1}{##1}}}
    \def\PY@tok@kd{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
    \def\PY@tok@ss{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
    \def\PY@tok@sr{\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.53}{##1}}}
    \def\PY@tok@mo{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
    \def\PY@tok@kn{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
    \def\PY@tok@mi{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
    \def\PY@tok@gp{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,0.50}{##1}}}
    \def\PY@tok@o{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
    \def\PY@tok@kr{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
    \def\PY@tok@s{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
    \def\PY@tok@kp{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
    \def\PY@tok@w{\def\PY@tc##1{\textcolor[rgb]{0.73,0.73,0.73}{##1}}}
    \def\PY@tok@kt{\def\PY@tc##1{\textcolor[rgb]{0.69,0.00,0.25}{##1}}}
    \def\PY@tok@ow{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.67,0.13,1.00}{##1}}}
    \def\PY@tok@sb{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
    \def\PY@tok@k{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
    \def\PY@tok@se{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.13}{##1}}}
    \def\PY@tok@sd{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
    
    \def\PYZbs{\char`\\}
    \def\PYZus{\char`\_}
    \def\PYZob{\char`\{}
    \def\PYZcb{\char`\}}
    \def\PYZca{\char`\^}
    \def\PYZsh{\char`\#}
    \def\PYZpc{\char`\%}
    \def\PYZdl{\char`\$}
    \def\PYZti{\char`\~}
    % for compatibility with earlier versions
    \def\PYZat{@}
    \def\PYZlb{[}
    \def\PYZrb{]}
    \makeatother
    
    
    \title{LaTeX Example}
    \author{Nicol\'as Brailovsky}
    
    \begin{document}
    
    \maketitle
    This is just a LaTeX kick off, with some basic commands.
    
    \section {Section one}
    For example, this is a paragraph.
    \begin{quotation}
        { em Lorem ipsum dolor sit amet, consectetur adipiscing elit.}
        Donec porta, enim eget tempus tempor, eros sem dapibus diam, vitae lacinia mauris metus id nulla.
    \end{quotation}
    
    \subsection{Subsection}
    You can have subsections too \ldots
    
    \subsection{Subsection}
        \ldots as many as you want
    
    \subsection{Random stuff}
    You can even create a shopping list in LaTeX, if you wish
    \begin{center}
    \begin{itemize}
        \item Beer
        \item Beer
    	\begin{itemize}
    		\item (Another brand of beer)
    	\end{itemize}
        \item Pizza
        \item Beer
    \end{itemize}
    \end{center}
    
    Or even better, use it to do the homework. \
    Math is a breeze in LaTeX: $x_n = sqrt{a + b}$ can be typeset inline.
    
    \section {Some source code}
    \begin{Verbatim}[commandchars=\\\{\},numbers=left,firstnumber=1,stepnumber=1]
    \PY{k+kt}{void} \PY{n+nf}{bogosort}\PY{p}{(}\PY{k+kt}{int} \PY{o}{*}\PY{n}{v}\PY{p}{)}
    \PY{p}{\PYZob{}}
        \PY{k}{while}\PY{p}{(} \PY{o}{!}\PY{n}{in\PYZus{}order}\PY{p}{(}\PY{n}{v}\PY{p}{)} \PY{p}{)} \PY{p}{\PYZob{}}
            \PY{n}{shuffle}\PY{p}{(}\PY{n}{v}\PY{p}{)}\PY{p}{;}
        \PY{p}{\PYZcb{}}
    \PY{p}{\PYZcb{}}
    \end{Verbatim}
    
    \end{document}
    

    Full Makefile:

    # Hack to make it work when foo.code => foo.code.tex
    code/%.tex: code/%.* code/%.aux code/%.tex
    	@rm code/*.aux
    	$(MAKE) -C .
    
    code/%.tex: code/%.*
    	pygmentize -f latex -O style='border=#000000,colorful,linenos=1' $< > $@
    
    # Search each code file to format and include
    CODE_FILES:=$(shell ls code/|egrep -v '.tex$ |.aux$ ' )
    CODE_FILES_DEP:=$(addprefix code/, $(CODE_FILES))
    CODE_FILES_TGT:=$(addsuffix .tex, $(basename $(CODE_FILES_DEP)))
    
    main.pdf: $(shell ls *.tex) $(CODE_FILES_TGT)
    	pdflatex main.tex > /dev/null
    
    all: main.pdf
    
    .PHONY: run clean
    
    run: main.pdf
    	evince main.pdf &
    
    clean:
    	rm -f *.out *.nav *.aux *.toc *.log *.snm *.pdf *.vrb
    

    Full bogosort.c file:

    void bogosort(int *v)
    {
        while( !in_order(v) ) {
            shuffle(v);
        }
    }
    


    Leave a Reply

    Fill in your details below or click an icon to log in:

    WordPress.com Logo

    You are commenting using your WordPress.com account. Log Out / Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out / Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out / Change )

    Google+ photo

    You are commenting using your Google+ account. Log Out / Change )

    Connecting to %s