Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
Could you use the verbatim environment? –  cslstr Mar 26 '14 at 2:25
What do you mean by "the code takes too much space"? You can set an arbitrary font size, you know... –  Jubobs Mar 26 '14 at 2:32
\verbatiminput{} may help - it reads in a file and has it included as verbatim. –  bombcar Mar 26 '14 at 2:44
Three different methods are explored at tex.stackexchange.com/questions/4095/…, including \verbatiminput{}... –  cslstr Mar 26 '14 at 3:24
You really might want to expand on "the code takes too much space" because a suitable choice of font/size/line spacing is needed for all solutions if you want them compact. The listings manual and questions tagged with listings here will help with this. The only difficulty I had was finding a narrow-ish monospaced font until I realised that lmodern has a condensed shape: \lstset{basicstyle=\footnotesize\ttfamily\fontseries{lc}\selectfont} will save a lot of space. –  Chris H Mar 26 '14 at 8:59

3 Answers 3

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!

share|improve this answer
+1 pythontex is also a notable package that uses Pygments. It would be worth mentioning that verbatim can't wrap lines. –  Jubobs Mar 26 '14 at 11:25
@Jubobs Thank you for your tips! I have updated the post. –  Malipivo Mar 26 '14 at 12:34

This uses verbatimbox to place it in a box at a reduced size. Further downsizing of the box is possible with a \scalebox. Whereas a box will not break across a page boundary, the package also offers "nobox" versions of some of its macros, that will break across a page. However, the boxed feature is nice for including verbatim in figures, tables, and moving it right on the line or centering it.

In this MWE, I have manually added \fbox enclosures around all the verbatim boxes, for clarity, but they are not part of the verbatim-box.

\parskip 1ex
\parindent 0in
With verbatimbox, you can tell the macro to make the code ``footnotesize.''\par
Here is my input code text
Line two of my $%& code text
Done now.
If it is from a file, I can do that, too, this time \verb|\scriptsize|:\par
Alternately, you could shrink it further with a \verb|\scalebox|:\par
If you have need to break across pages, the package offers ``nobox'' versions
of some of its macros.

enter image description here

There is also the numberedblock package (http://ctan.org/pkg/numberedblock)that allows formatting small code blocks in a manner such as (note: image taken from package docs):

enter image description here

share|improve this answer

Here's the idiom I use to give my students (learning TeX) the source code for the homework assignment, so they can study and edit it when they write up their mathematics.



Homework exercises here \ldots 


Here is the \LaTeX{} source for this document. You can cut it from the
pdf and use it to start your answers. I used the \verb!\jobname! macro
for the source file name, so you can call your file by any name you like.


share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.