[Tex/LaTex] How to include a programming language source code file in a plain TeX document

plain-texsourcecode

I have some Java and C source codes and I want to include them in my .tex document. I can't simply copy and paste them into my document: In fact, doing this way, the code won't appear with the right spacings and indentations. I'd like that the source code does appear the same way it looks when opened in any text editor.

Is there a simple way to do it? Is there a macro (for the plain format) that takes care of that automatically?

Best Answer

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).