Question

I'd like to add a code appendix to my LyX document. There are a few options I already considered, but they all have their problems.

I know a bit about listings, but one problem with those is that, if I copy & paste my code into them, I lose all enters/newlines. Since the code is too large to correct by hand, I was wondering if there is an alternative.

In LyX there is the possibility of inserting child documents, but that seems to be only for .tex files. Would have been ideal if I could just insert my .java file as a child document.

I could print the code to PDF, but it will include margins that mess up the final document, since the PDF is placed on the left margin of the final document and then there is the margin of the PDF. Also, this PDF always contains the entire code and white areas where not the entire page has been filled.

Does anyone have good alternative?

Was it helpful?

Solution

The listings package found here

http://www.ctan.org/tex-archive/macros/latex/contrib/listings/

allows the include of external source code files (look into the reference for \lstinputlisting).

EDIT: here you find some samples how to use it:

http://en.wikibooks.org/wiki/LaTeX/Packages/Listings

OTHER TIPS

If you need to copy-paste code to LyX listing box then use Edit -> Paste Special -> Seletion or Ctrl+Alt+V.

For what it's worth, at least the 2.0 versions of LyX have the ability to include listings as child documents. Insert, File, Child Document, and choose from the dropdown box "Program Listing". This uses the listings package and lets you keep your source in its own file.

If listings doesn't support your language, you can always use something like highlight or source-highlight to generate a latex snippet of syntax-highlighted code that you can add as a child document of type "Input"

Yes, if you copy&paste code into the LyX listings box, you lose all newlines, but you can preprocess your code (insert an extra newline below each line):

$ cat foo.java | sed -e 's/$/\n/' > bar.java

Then you can copy&paste the new file bar.java and everything will be ok.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top