Installing Listings Package for LaTeX

Ever wondered how you could insert code in a latex document/presentation. Read on to know how to do it without any hassle.

Listings is a awesome package for LaTeX which makes including code a breeze. No need to do formating, no need for copy paste too !! It has syntax highlighting too. Check out the links given below. I had some difficulty installing listings package and couldn’t find a nice way and hence this blog post.

Here is the code: ( Try it and check if you already have listings)


\documentclass{article}
\usepackage{listings}
\begin{document}
\lstinputlisting[language=C++,firstline=2,lastline=20]{../Programs/4.cpp}
\end{document}

It has support for many languages and if you find one thats not there, you could perhaps write a patch for one, thereby contributing to FOSS.

“../Programs/4.cpp” is the exact file name with its path.

Installation:

  1. Download the zip here : http://mirror.ctan.org/macros/latex/contrib/listings.zip and listing.sty from http://ctan.um.ac.ir/macros/latex/contrib/listing/listing.sty
  2. Unzip it using your favourite archive manager.
  3. Then go to the location where you installed TeX. For linux users it is /usr/share/texmf/ . Windows users, figure it yourself
  4. Copy the following files as mentioned :
    1. listings.pdf              to      texmf/doc/latex/listings
    2. listings.dtx, listings.ins,listings.ind, lstpatch.sty,lstdrvrs.dtx          to            texmf/source/latex/listings
  5. Create the directory texmf/tex/latex/listings or, if it exists already,remove all files except lst<whatever>0.sty and lstlocal.cfg from it.
  6. Run listings.ins through LaTeX  i.e  type  sudo latex listings.ins in your terminal.
  7. Move the generated files to texmf/tex/latex/listings.

If all goes well, then you should have listings working properly. Try it out with the code given above. If you still have any problems, just ping me.

There are plenty of examples available over the net which illustrate the use of Listings package.

Check this pdf here : http://ctan.um.ac.ir/macros/latex/contrib/listings/listings.pdf

Check out these too :

http://dante.ctan.org/indexes/macros/latex/contrib/listings/

http://en.wikibooks.org/wiki/LaTeX/Packages/Listings

About these ads

5 Responses to “Installing Listings Package for LaTeX”

  1. Varun Says:

    works perfectly….great way to make a presentation related to coding stuff…..

  2. Fer Says:

    i’m using miktex, command prompt says “cant find file” (listings) i think im not using the right directory calling listings with latex.exe

  3. David Says:

    Thank you!!!
    It worked fine.

  4. Yves Says:

    Thanks for your one and only description how and where to put the listing files.
    Everybody explains the use of listings and not to forget to include \usepackage{listings} etc. but nobody explains where to put the files.

    But even with your explanation I can’t get it run.
    I did everything as described. At step 4 however there is no “source” folder under “texmf/”. The only “source” folder I could find was under “texmf-dist/”.
    So I copied these files (listings.dtx, listings.ins,listings.ind, lstpatch.sty,lstdrvrs.dtx) into “texmf-dist/source/latex/listings”.

    I can’t figure out the problem but I still get the error message “! LaTeX Error: Environment lstlistings undefined.”.

    If you need some more information please let me know.

    I hope you can give me some advice.

  5. Yves Says:

    Ok, got it.

    On Mac you have to use the folder “texmf-dist/” for every step instead of “texmf/”.

    Thanks anyway for the nice description.


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

Follow

Get every new post delivered to your Inbox.

Join 519 other followers

%d bloggers like this: