How to Include Source Code in Documents


A program called tgrind is available in the sipb locker to help you convert program listings to a form easily includable in a LaTeX document. To use this program, type at the prompt:
add sipb
tgrind -latex -llanguage filename > filename.tex
This will generate a file (filename.tex) of code (-llanguage) formatted for inclusion in a LaTeX file (-latex). For example, to format a C program called mycode.c:
tgrind -latex -lc mycode.c > mycode.c.tex
If you get errors latexd'ing, it may be necessary to remove the line which contains:
\input{tgrindmac}
at the top of the created .tex file, and the line:
\vfill\eject\end
at the bottom of it. You can also format languages other than C:
   -lp      	       Pascal programs
   -lcsh               Csh (C shell scripts)
   -lsh                sh (Bourne shell scripts)
   -ltex               TeX code
   -lprolog            prolog
   -lratfor            Fortran
   -ly                 Yacc
   -lc++               C++
To include this file in your document: o Make sure you have the tgrind documentstyle option specified. (Include tgrind in the square brackets in the \documentstyle command at the top of your file.)

Place the line

\tgrindfile{mycode.c.tex}
in your .tex file where you want to insert the formatted code.

That's all there is to it. For more information, see the man page on tgrind, and the file /usr/sipb/lib/vgrindefs (after typing add sipb).