# ansi2tex(22 November 2011)

A while back, I was doing a presentation about Clang (a C/C++ compiler) at my university. (The presentation can be found here.) Clang has very nice coloured diagnostics, and I wanted to show some examples of that in the presentation. I was using LaTeX and Beamer to do the presentation as usual. If I just copy-pasted the diagnostics, I would lose the colours. I could of course add them manually, but that seemed like a lot of work, and writing a small program to do it for me seemed like more fun.

The coloured output is achieved using ANSI escape codes. To capture the diagnostics, I ran Clang like this:

$clang -Xclang -fcolor-diagnostics -c my_program.c 2> foo.diag  This command tries to compile the file my_program.c, and outputs any warnings or errors to foo.diag. The -Xclang -fcolor-diagnostics flags are used to force the compiler to output coloured diagnostics even though the output is going to a file rather than the terminal. I was learning about Flex, a lexical analyser generator, at the time, and wrote the following program: ansi2tex.l. To compile it, first use flex to generate the C program, and then compile that with your favourite compiler: $ flex -o ansi2tex.c ansi2tex.l
$gcc -o ansi2tex ansi2tex.c  To use the program to convert a diagnostic to TeX, run it like this: $ ./ansi2tex < foo.diag > foo.tex


That will write the resulting code to foo.tex. To include that in your document, you might do something like:

\documentclass{article}
\usepackage{color}
\begin{document}
\input{foo.tex}
\end{document}


The results look like this: (PDF available here.)

I am not a TeX expert, so I am sure there are more elegant ways of converting text with ansi escape characters to LaTeX, but this has served my purposes so far. Feel free to use, modify and distribute this program as you wish if you find it useful.