6

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?

2
  • YConsider posting your question on tex.stackexchange.com. Mar 15, 2011 at 10:53
  • @Bernard @Remix consider having this question migrated... crossposting is discouraged. If you want to migrate, just flag for mod attention.
    – user1228
    Mar 15, 2011 at 17:05

4 Answers 4

8

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

3
  • I believe the external resource must be of type .sty or .tex. If I put in a .java file, it gives the error: *** (cannot \read from terminal in nonstop modes). However, I don't know what it actually means. Mar 15, 2011 at 15:49
  • @RemiX: have a look at the second link I posted above.
    – Doc Brown
    Mar 15, 2011 at 15:57
  • Thanks. I also decided to export the LyX document to LaTeX, so I could use the actual commands for more control. Mar 16, 2011 at 13:27
3

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

0
2

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"

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.