在 Lyx 文档中插入编程代码

发布于 2024-08-18 09:49:54 字数 100 浏览 2 评论 0原文

在 Lyx 文档中插入 python/C++ 代码的最佳方法是什么?该代码是不到 20 行的小示例。

我的 Lyx 文档使用 Book 文档类。

奥尔扬普

What is the best way of inserting python/C++ code in a Lyx document? The code is small examples less than 20 lines.

My Lyx document is using the Book document class.

Orjanp

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

世态炎凉 2024-08-25 09:49:54

我更喜欢将列表作为子文档插入,因此可以直接从文件中获取代码,您可以进一步编辑并保持更新(您可以避免重复信息,维护也容易得多)。
要在 Lyx 中执行此操作:
插入->文件->子文档
然后在出现的窗口中将类型更改为程序列表并根据需要进行配置,例如您可能需要输入参数语言=Python(您可以键入? 查看所有参数)。
我通常使用的一组参数是:

breaklines=true //--> breaks lines to margin
captionpos=b  //--> caption at the bottom of the listing (default is "t")
frame=tb  //--> frame at the top and at the bottom of the listing
language=Python  //--> syntax highlighting for python

I prefer to insert the listings as a child document, so the code is grabbed directly from a file that you can further edit and keep updated (you avoid to duplicate an information and maintenance is a lot easier).
To do this in Lyx:
Insert->File->Child document
Then in the window that will appear change the type to program listing and configure it as you need, for example you could want to enter the parameter language=Python (you can type a ? to view all the parameters).
A set of parameters I usually use is:

breaklines=true //--> breaks lines to margin
captionpos=b  //--> caption at the bottom of the listing (default is "t")
frame=tb  //--> frame at the top and at the bottom of the listing
language=Python  //--> syntax highlighting for python
姜生凉生 2024-08-25 09:49:54

应该有一个插入 ->程序列表选项。它使用 listings Latex 包,所以你应该安装它。该支持似乎已在 Lyx 1.5 中添加,从他们的屏幕截图来看,似乎为您提供了很多定制选项。

There should be an Insert -> Program Listing option. That uses listings Latex package, so you should have that installed. The support seems to have been added in Lyx 1.5, and from their screenshot, it seems it gives you a lot of customization options.

走过海棠暮 2024-08-25 09:49:54

插入->节目列表功能效果很好。我只是想补充一点,如果你在Mac上,并尝试直接使用命令+V或右键单击+粘贴将一段代码粘贴到程序列表中,空格将不会被保留,你必须手动通过键入制表符、回车符等插入空格。

解决此问题的一个非常简单的方法是使用 shift+command+V 进行粘贴。这会保留原始源代码的所有空白。

The insert->Program Listing feature works great. I just want to add that if you are on Mac, and try to directly paste, using command+V or right-click+paste, a block of code into the program listing, the whitespace will not be preserved, and you have to manually insert the whitespace by typing tabs, returns, etc.

A very easy way to get around this is is to paste using shift+command+V. This preserves all the whitespace of your original source code.

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