6

I'm using Lyx's Insert|Program Listing to insert a code listing into my document. Unfortunately, Lyx seems to ignore the Typewriter font selection I made in DocumentOptions under Fonts ⇒ Typewriter. Interestingly enough, typewriter font does come out correctly elsewhere, but not in program linstings.

Can someone help me figure out why? I'm using Lyx 2.0.2 and am using XeTeX for output.

3
  • Have you changed the listing to actually use typewriter fonts? By default, they are not used. Apr 25, 2012 at 11:42
  • No, I haven't. I assumed they'd be used automatically. If that's not the case, how do I make this setting document-wide? Also, I think the letter spacing in program listings is wrong (wider than normal), is there any way to get it to 'default' levels? Apr 25, 2012 at 11:48
  • I added a suggestion about the spacing issue to my answer. Apr 25, 2012 at 11:57

2 Answers 2

10

Program listings does not actually use the typewriter font family by default, so you have to change this in the settings. To change it for a single listing, right click it, choose Settings and select Typewriter as Font family.

enter image description here

To make it a global change, go to Document --> Settings --> Listings, and type in the following option:

basicstyle={\ttfamily}

For the spacing, try adding

columns=fullflexible

to the options list for listings. I'm not entirely sure about this, though the different choices for columns is described in the listings manual (listings is the package used for these program listings), section 2.10 Fixed and flexible columns.

5
  • Thanks, I get it now. What about letter spacing - can that be fixed too? Or am I imagining things? Apr 25, 2012 at 11:55
  • Actually, I've just tried \basicstyle{\ttfamily} and XeTeX gave me an Undefined control sequence. Apr 25, 2012 at 13:32
  • @DmitriNesteruk There is no backslash before basicstyle and an = after. Apr 25, 2012 at 13:39
  • Whoops, my bad. Apr 25, 2012 at 13:43
  • For spacing, setting basewidth=0.45em will reduce the inter-letter spacing from a ridiculous 0.6em to a more compact width, all while preserving the fixed width (whereas columns=fullflexible wrecks the spacing). To change the font size, try, for example, \basicstyle{\ttfamily,\small}.
    – Linas
    May 20, 2021 at 18:25
2

I still use LyX 1.6, the more recent version available for Windows 2000 (which I have in a couple of PCs). In LyX 1.6.10 you don't find the "Listings" module in the "Document->Settings" menu. So you solve this issue by adding

\lstset{basicstyle={\ttfamily},columns=fullflexible}

to the "Latex Preamble" box in the "Document->Settings" menu.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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