Is there a package which allows me to render (n)curses based output (VT100, ANSI ...) directly in LaTeX (including colors if possible)? I'm currently making screenshots and include the PNGs via graphicx. I'm just wondering whether it's possible to achieve a higher quality.

4 Answers 4


Jan's solution is the most direct, but, speaking from experience, writing parsers for teletype codes is a real pain. I wonder if it makes sense to reinvent such a tricky wheel, rather than use some external program to do the parsing instead.

Instead, I suggest using an external curses to HTML converter, and use one of the existing HTML to Latex interpreters/translaters to generate the code for the document.

I can't say which tools are right for the job, but maybe some of my thoughts are worth typing out:

  1. David Mertz has written a series on curses programming in Python, of which #6: Curses programming in Python: Tips for Beginners introduces curses_txt2html.py — note that code has dependencies on other code from the series;
  2. Finding the right converter from HTML to Latex is made more difficult by the fact that (i) there are so many, and (ii) most care about logical structure, rather than exactly representing coloured, preformatted monospaced text.
  3. The Texinfo people know about curses, since, e.g., the pinfo Texinfo reader is ncurses based, and Texinfo is a Tex format that closely resembles, and is easily converted to Latex. I don't know if they have done anything like this, but if they have, they are likely to have done a good job.

I don't know about a package. You would have to make TeX interpret the ANSI sequences. That should be possible by assigning catcode 13 to the ESC character. It may be tricky to figure out how long is the argument, though, since in the escape sequences it is the last character that determines what type of sequence it is, and therefore how long the argument is.

  • 1
    This is definitely the right approach. You could try some sort of looping definition, with ESC requiring a [ and scanning until it determines it should stop (and defining the one-byte CSI to do the same thing); the fancyvrb package might help with making everything else verbatim. As for the other characters, you might be able to find a font which has them, or you could define them as active too, and replace them with an appropriate symbol or, in the worst case, a figure drawn with e.g. TikZ. This actually sounds interesting; I might take a crack at it if I have some free time. Nov 12, 2010 at 22:42
  • Although I'm comming from a programming background (incl. Parser generation) I'm not that deep into (La)TeX at the moment. Good to know that it could be done though. Nov 14, 2010 at 8:26

You could use the listings package, and have the terminal output to a text file (on most operative systems this is accomplished by adding >> output.txt at the end of the command).

  • I was considering using something like alltt ... However this doesn't provide automagical background and text coloring and dosen't provide the pseudo (double)line character for boxes and things like that so common in (menu-driven) n(curses) applications. Nov 12, 2010 at 15:42

This would be very useful to have, I scoured the internet unsuccessfully last week for something which would do just this. I think I decided the best approach would be (eventually) to modify http://pypi.python.org/pypi/ansi2html/ so that it could support LaTeX output too, along the lines of the Pygments syntax highlighting system which creates a generic internal representation then supports multiple output formats like HTML, LaTeX, RTF etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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