[Tex/LaTex] Writing source code in LaTeX as text

codesourcecode

Is there a package that allows source code in LaTeX as text? I'd rather not use the listings package because the code takes too much space. Copy/paste is tedious because the spaces are not preserved.

Best Answer

I can give you a couple of tips for packages and tools to start experimenting with, I tried several tools in the past:

  • I like the verbatim and verbatim* environments in TeX. It is an easy and a straighforward way of typesetting a small portion of code right away. However, it cannot wrap lines.

  • I was using the fancyvrb package some time ago, http://ctan.org/pkg/fancyvrb. That was the first package where I noticed that cross-referencing to the source code line numbers is possible (at all), see pages 14 and 15 of the documentation.

  • I use the listings package quite frequently, http://ctan.org/pkg/listings. It is rather complex package, but it's serving my needs, especially its option of formatting an external file (the \lstinputlisting command). It can also wrap lines and add an opening and a closing symbol to such lines. I remember that I created my own filter for the VBA language (Visual Basic for Applications), it was fun trying that. There is a new package when dealing with the UTF-8 coded characters, please see the listingsutf8 package, http://ctan.org/pkg/listingsutf8. I was using escape sequences in listings to solve that particular problem. I was typesetting CJKV (Chinese, Japanese, Korean, Vietnamese) in my source code, e.g. さようなら (that's Sayōnara! in Japanese, it is Goodbye! in English). I was presenting preparation of my PF postcards once.

  • If you are dealing with the R code, there are excellent tools these days to work with, knitr (http://yihui.name/knitr/) and rather old tool named Sweave (http://www.stat.uni-muenchen.de/~leisch/Sweave/).

  • If you are dealing with the Sage code, please give a try to the SageTeX style, http://ctan.org/pkg/sagetex. It's programmed by Robert Mařík from Brno, the Czech Republic, a person/developer I met face to face once. :-)

  • I also use the Highlight program (http://www.andre-simon.de/). There is an option to mix several languages in one source code to be highlighted (LaTeX+Lua, HTML+CSS etc.). It's called formatting and recognition of the nested languages within a file, I asked for that feature when LuaTeX was born, I am just proud I did that. Both programs (this and Pygments) provide the LaTeX format as the export option for other languages. A style can be extracted to a separate file from the rest of the document.

  • Worth mentioning is a very powerful tool named Pygments (http://pygments.org/). I am using it directly from the command line (or as a library from within Python) plus in the TeX world there are (at least) two packages to make the conversion easier: TeXments (http://ctan.org/pkg/texments) and Minted (http://ctan.org/pkg/minted). Speaking of Python, there is also a package named PythonTeX, see http://ctan.org/pkg/pythontex. Give it a try!

  • I have recently learned about the pandoc tool, http://johnmacfarlane.net/pandoc/. It is rather a format-to-format converter, but it's worth giving it a try. In addition to the self-standing executables for all major operating systems there is an online version, http://johnmacfarlane.net/pandoc/try/. The LaTeX format is an option on the input as well as on the output side of the converter. It looks that this tool is (from) the future!

Good luck with typesetting the source codes!

Related Question