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.
[Tex/LaTex] Writing source code in LaTeX as text
codesourcecode
Related Solutions
With tcolorbox
and its listings
library you can use \newtcblisting
to define a new environment for listings (using behind the scenes the listings
package); if you use the hbox
option for the tcolorbox
, its width will dynamically adapt to the width of its content but, alas!, now the box doesn't admit page breaks and also automatic line breaks are not very well supported (the code will break if breaklines=true
was passed as an option for the listing, but the box now won't behave nicely for long lines).
\documentclass{article}
\usepackage[many]{tcolorbox}
\tcbuselibrary{listings}
\newtcblisting{mylisting}{
listing only,
hbox,
colframe=cyan,
colback=cyan!10,
listing options={
basicstyle=\small\ttfamily,
breaklines=true,
columns=fullflexible
},
}
\begin{document}
\begin{mylisting}
int main(int ac, char *av[])
{
printf("Hello, World");
return 0;
}
\end{mylisting}
\begin{mylisting}
int main(int ac, char *av[], char **ep,char *av[], char **ep)
{
printf("Hello, World");
return 0;
}
\end{mylisting}
\end{document}
Perhaps then you can have breakable boxes with automatic line breaks by default, and selectively add the hbox
option if you need variable width for short listings:
\documentclass{article}
\usepackage[a6paper]{geometry}% just for the example
\usepackage[many]{tcolorbox}
\tcbuselibrary{listings}
\newtcblisting{mylisting}[1][]{
listing only,
breakable,
colframe=cyan,
colback=cyan!10,
listing options={
language=C,
basicstyle=\small\ttfamily,
breaklines=true,
columns=fullflexible
},
#1
}
\begin{document}
\begin{mylisting}[hbox]
int main(int ac, char *av[])
{
printf("Hello, World");
return 0;
}
\end{mylisting}
\begin{mylisting}
int main(int ac, char *av[],int ac, char *av[],int ac, char *av[],int ac, char *av[],int ac, char *av[],int ac, char *av[])
{
printf("Hello, World");
return 0;
}
int main(int ac, char *av[])
{
printf("Hello, World");
return 0;
}
{
printf("Hello, World");
return 0;
}
{
printf("Hello, World");
return 0;
}
{
printf("Hello, World");
return 0;
}
{
printf("Hello, World");
return 0;
}
{
printf("Hello, World");
return 0;
}
\end{mylisting}
\end{document}
The tcolorbox
package also offers interaction with minted
(another popular package for listings); minted, however, doesn't support automatic line breaking. A little example using now the minted
library:
\documentclass{article}
\usepackage[many]{tcolorbox}
\tcbuselibrary{minted}
\newtcblisting{mylisting}{
colframe=cyan,
colback=cyan!10,
listing only,
listing engine=minted,
minted language=cpp,
minted options={fontsize=\small,linenos,numbersep=3mm},
}
\begin{document}
\begin{mylisting}
int main(int ac, char *av[])
{
printf("Hello, World");
return 0;
}
\end{mylisting}
\begin{mylisting}
int main(int ac, char *av[], char **ep,char *av[], char **ep)
{
printf("Hello, World");
return 0;
}
\end{mylisting}
\end{document}
I've found the solution in the eplain
format (which I actually use as an extension of Knuth's plain
, and in addition to amstex
and epsf
). In eplain
there is a macro called \listing
that takes as argument the text file you want to include in your document.
Suppose we have a text file called Main.c
. Then if you use the command \listing{Main.c}
, TeX reads the file Main.c
and puts it in your document, making it appear as it appears when you open it in a text editor. The text file is typeset using the Computer Modern 10pt typewriter font, but you can simply change this behaviour (for more information have a look at eplain
documentation).
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
andverbatim*
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 thelistingsutf8
package, http://ctan.org/pkg/listingsutf8. I was using escape sequences inlistings
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 namedSweave
(http://www.stat.uni-muenchen.de/~leisch/Sweave/).If you are dealing with the
Sage
code, please give a try to theSageTeX
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) 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
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!