Insert programming code in a Lyx document
Asked Answered
V

3

75

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

Vestryman answered 22/1, 2010 at 11:45 Comment(1)
Here's a related answer for javascript syntax highlighting: #10421257Uncommunicative
A
68

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.

Alexipharmic answered 22/1, 2010 at 11:49 Comment(7)
Thanks. I'll have a look at it.Vestryman
What I did was this. First I inserted a <box>, then a <program listing>, and in the end a <caption>. By having a box around it I can have a visible frame around the code.Vestryman
Is there any way to have syntax highlighting and line numbers?Quinquereme
To add a caption, place your cursor inside the listing and click on "Insert -> Caption". By default the caption will be shown above the listing. To move the caption to the bottom, open the listing settings dialog ("Edit -> Listing Settings") and switch to the "Advanced" tab. Then add the parameter "captionpos=b".Heartwood
One further tip: If you want to change the numbering from "Listing 1.1/1.2/..." to "Listing 1/2/...", add "\lstset{numberbychapter=false}" to your document preamble. See also Continuous v. per-chapter/section numbering of figures, tables, and other document elements.Heartwood
@Quinquereme Under "Edit -> Listing Settings" you can choose a programming language (Python, C, ...)Athallia
An alternative to @Orjanp's suggested <box> is the built-in option "frame=lines", which you can enter under "Advanced Settings" from within the "Edit -> List Settings" menu item.Athallia
T
74

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
Transpicuous answered 9/2, 2011 at 17:33 Comment(3)
is there anyway to get color highlighting? and reduce the indent and font size?Fanning
@Fanning don't know if this helps: tex.stackexchange.com/questions/28554/…Transpicuous
How can i center it into the middle of the page using lyx ?Doronicum
A
68

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.

Alexipharmic answered 22/1, 2010 at 11:49 Comment(7)
Thanks. I'll have a look at it.Vestryman
What I did was this. First I inserted a <box>, then a <program listing>, and in the end a <caption>. By having a box around it I can have a visible frame around the code.Vestryman
Is there any way to have syntax highlighting and line numbers?Quinquereme
To add a caption, place your cursor inside the listing and click on "Insert -> Caption". By default the caption will be shown above the listing. To move the caption to the bottom, open the listing settings dialog ("Edit -> Listing Settings") and switch to the "Advanced" tab. Then add the parameter "captionpos=b".Heartwood
One further tip: If you want to change the numbering from "Listing 1.1/1.2/..." to "Listing 1/2/...", add "\lstset{numberbychapter=false}" to your document preamble. See also Continuous v. per-chapter/section numbering of figures, tables, and other document elements.Heartwood
@Quinquereme Under "Edit -> Listing Settings" you can choose a programming language (Python, C, ...)Athallia
An alternative to @Orjanp's suggested <box> is the built-in option "frame=lines", which you can enter under "Advanced Settings" from within the "Edit -> List Settings" menu item.Athallia
M
6

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.

Mukden answered 12/12, 2017 at 22:55 Comment(1)
How can i center it into the middle of the page? And can change the background color? like grey.Valerivaleria

© 2022 - 2024 — McMap. All rights reserved.