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.
- 95,762
- 181
4 Answers
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
verbatimandverbatim*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
fancyvrbpackage 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
listingspackage 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\lstinputlistingcommand). 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 thelistingsutf8package, http://ctan.org/pkg/listingsutf8. I was using escape sequences inlistingsto 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
Rcode, there are excellent tools these days to work with,knitr(http://yihui.name/knitr/) and rather old tool namedSweave(http://www.stat.uni-muenchen.de/~leisch/Sweave/).If you are dealing with the
Sagecode, please give a try to theSageTeXstyle, 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
Highlightprogram (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) andMinted(http://ctan.org/pkg/minted). Speaking of Python, there is also a package namedPythonTeX, see http://ctan.org/pkg/pythontex. Give it a try!I have recently learned about the
pandoctool, 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!
- 13,287
-
+1
pythontexis also a notable package that uses Pygments. It would be worth mentioning thatverbatimcan't wrap lines. – jub0bs Mar 26 '14 at 11:25 -
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.
\documentclass{article}
\usepackage{verbatim}
\begin{document}
Homework exercises here \ldots
\newpage
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.
\verbatiminput{\jobname}
\end{document}
- 9,333
- 3
- 42
- 69
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.
\documentclass{article}
\usepackage{verbatimbox}
\usepackage{graphicx}
\parskip 1ex
\parindent 0in
\begin{document}
With verbatimbox, you can tell the macro to make the code ``footnotesize.''\par
\begin{verbbox}[\footnotesize]
Here is my input code text
Line two of my $%& code text
Done now.
\end{verbbox}
\fbox{\theverbbox}\par
If it is from a file, I can do that, too, this time \verb|\scriptsize|:\par
\verbfilebox[\scriptsize]{junk.tex}
\fbox{\theverbbox}\par
Alternately, you could shrink it further with a \verb|\scalebox|:\par
\fbox{\scalebox{.5}{\theverbbox}}\par
If you have need to break across pages, the package offers ``nobox'' versions
of some of its macros.
\end{document}

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

- 1,765
- 237,551
Problem with verbatim is that it does not break long lines of code, so I use the spverbatim package instead
\documentclass{article}
\usepackage{spverbatim}
\begin{document}
\begin{spverbatim}
code
code with a very long line that does not fit on the page and so needs to be broken or wrapped
code
\end{spverbatim}
\end{document}
- 95,762
-
1Welcome to [tex.se]! I have completed your code snippet to a complete compilable file and added an image of the output. – Andrew Swann Aug 30 '17 at 17:57

verbatimenvironment? – cslstr Mar 26 '14 at 02:25\verbatiminput{}may help - it reads in a file and has it included as verbatim. – bombcar Mar 26 '14 at 02:44\verbatiminput{}... – cslstr Mar 26 '14 at 03:24\lstset{basicstyle=\footnotesize\ttfamily\fontseries{lc}\selectfont}will save a lot of space. – Chris H Mar 26 '14 at 08:59