Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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):

\documentclass[10pt,pdftex]{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{listings}

\begin{document}

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

\begin{lstlisting}
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
    }
}
\end{lstlisting}

\end{document}

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?

share|improve this question
    
@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 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 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 at 17:56
    
@Manuel Yes, that works in this case, but an automatic solution would be really nice. –  Håkon Marthinsen Jul 12 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 at 18:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.