如何在 LaTeX / LyX 中添加(大)代码附录?
我想向我的 LyX 文档添加代码附录。我已经考虑过一些选择,但它们都有各自的问题。
我对列表了解一点,但其中的一个问题是,如果我复制并复制列表,将我的代码粘贴到其中,我丢失了所有输入/换行符。由于代码太大而无法手动纠正,我想知道是否有替代方案。
在 LyX 中可以插入子文档,但这似乎仅适用于 .tex 文件。如果我可以将 .java 文件作为子文档插入,那就太理想了。
我可以将代码打印为 PDF,但它会包含弄乱最终文档的边距,因为 PDF 放置在最终文档的左边距,然后是 PDF 的边距。此外,此 PDF 始终包含整个代码和未填充整个页面的白色区域。
有人有好的选择吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
列表包在这里找到
http://www.ctan.org/tex -archive/macros/latex/contrib/listings/
允许包含外部源代码文件(查看
\lstinputlisting
的参考)。编辑:在这里您可以找到一些如何使用它的示例:
http://en.wikibooks.org /wiki/LaTeX/Packages/Listings
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
如果您需要将代码复制粘贴到 LyX 列表框,请使用编辑 ->选择性粘贴 ->选择或 Ctrl+Alt+V。
If you need to copy-paste code to LyX listing box then use Edit -> Paste Special -> Seletion or Ctrl+Alt+V.
就其价值而言,至少 LyX 2.0 版本能够将列表作为子文档包含在内。插入、文件、子文档,然后从下拉框中选择“程序列表”。这使用
listings
包,并允许您将源代码保存在自己的文件中。如果列表不支持您的语言,您可以随时使用 highlight< /a> 或 source-highlight 生成语法高亮代码的乳胶片段您可以添加为“输入”类型的子文档
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"
是的,如果将代码复制并粘贴到 LyX 列表框中,您将丢失所有换行符,但您可以预处理代码(在每行下方插入额外的换行符):
然后您可以复制并粘贴新文件 bar.java一切都会好起来的。
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):
Then you can copy&paste the new file bar.java and everything will be ok.