I would like to typeset code using proportional fonts and the listings package, but no matter what I do, alignment is always off. Here is an example in Java (taken from Wikipedia):




\lstset{language=Java, columns=fullflexible, keepspaces}

import javax.swing.JFrame;  // Importing class JFrame
import javax.swing.JLabel;  // Importing class JLabel
public class HelloWorld {
    public static void main(String[] args) {
        JFrame frame = new JFrame();            // Creating frame
        frame.setTitle("Hi!");                  // Setting title frame
        frame.add(new JLabel("Hello, world!")); // Adding text to frame
        frame.pack();                           // Setting size to smallest
        frame.setLocationRelativeTo(null);      // Centering frame
        frame.setVisible(true);                 // Showing frame


Note that with the monospaced font seen above, all the comments are nicely aligned. I have tried using different combinations of columns=flexible, columns=fullflexible, columns=spaceflexible, and keepspaces, but none of them give correct alignment.

Here is what an ideal result should look like:

enter image description here

Notice that the comments are aligned the same way as in the source code and no spurious spaces have been introduced.

I think this could be done automatically by having a mechanism that detects when alignment is used in the source code. Since I use tabs for indentation and spaces for alignment, this mechanism could be e.g. detection of two or more consecutive spaces.

My LaTeX programming skills are too poor to create this myself, but maybe someone here could give it a go?

  • @egreg No, I don't want columns=fixed, because this destroys spacing inside words. This option only works if I use monospaced fonts, but as I said, but I would like to use proportional fonts. – Håkon Marthinsen Jul 12 '14 at 17:49
  • @Manuel If I remove columns=fullflexible, it defaults to columns=fixed, but this is not what I want. See the reply to egreg. – Håkon Marthinsen Jul 12 '14 at 17:53
  • columns=flexible works for me (with keepspaces=false), but you need to add one more space (in this case) before every comment. EDIT I don't know how this works, that's the reason I'm just saying what I see. If you need keepspaces=true you need to add even more spaces before the comments and that way they align perfectly. How many are needed? No idea :D – Manuel Jul 12 '14 at 17:56
  • @Manuel Yes, that works in this case, but an automatic solution would be really nice. – Håkon Marthinsen Jul 12 '14 at 18:02
  • 1
    Well, I don't know how it works. But it seems that needs a minimum space. May be as a partial solution (until someone explain how it works and how to tune it) you just need to add a little more space by default before the comments (in order to leave enough room to accommodate the alignment). – Manuel Jul 12 '14 at 18:03

Your Answer

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

Browse other questions tagged or ask your own question.