如何在 LaTeX / LyX 中添加(大)代码附录?

发布于 2024-10-23 11:55:20 字数 326 浏览 2 评论 0原文

我想向我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

爱格式化 2024-10-30 11:55:20

列表包在这里找到

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

淡淡離愁欲言轉身 2024-10-30 11:55:20

如果您需要将代码复制粘贴到 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.

殤城〤 2024-10-30 11:55:20

就其价值而言,至少 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"

方觉久 2024-10-30 11:55:20

是的,如果将代码复制并粘贴到 LyX 列表框中,您将丢失所有换行符,但您可以预处理代码(在每行下方插入额外的换行符):

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

然后您可以复制并粘贴新文件 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):

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文