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?

  • take a look at listings – Sean Allred May 8 '14 at 19:00
  • 2
    @SeanAllred Isn't listings a LaTeX package? I asked for a plain TeX macro... – User May 8 '14 at 19:08
  • 3
    @Matteo Plain users tend not to be big on 'automatic'. Moreover, there might be specific requirements here you've not mentioned: verbatim is easy enough to set up, verbatim input a little more tricky, highlighting/line selection/... much more so. Could you give us a bit more detail about your requirements? – Joseph Wright May 8 '14 at 19:11
  • 1
    looks like you've found an answer using eplain. i was going to suggest that the "plain" version of the tugboat macros (tugboat.sty, should be in the tex/plain/tugboat-plain area of a full tex live distribution) contains a nice implementation of verbatim that takes into account spaces at the beginnings of lines, which (if i remember correctly) knuth's original in the texbook ignores. – barbara beeton May 8 '14 at 20:32
  • 1
    You could also perhaps read section "3. Verbatim Listing" in Appendix D of the TeXBook, which explains exactly what you need to do in plain TeX to include files verbatim; or if you are more adventurous look up "programs, for computers" in the index and see how you can pretty-print source code in plain TeX. – Thruston May 8 '14 at 20:41

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

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.