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'm trying to condense the size of my comments by using a condensed font however its not working correctly.

The font shape looks fine with ttfamily enabled in basicstyle, however, there is a huge amount of space between the letters for comments. How can I get rid of this spacing?

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{lmodern}

\begin{document}
    \begin{lstlisting}[language=Prolog,basicstyle=\ttfamily\footnotesize,commentstyle=\fontseries{lc}\selectfont\itshape]
    hello :- world
    % This is a comment
    \end{lstlisting}    
\end{document}

Edit: How do I get the source of this post to compile like I've seen in other threads?

share|improve this question
1  
Try columns=fullflexible. Since you're highlighting some Prolog code, you may also be interested in this answer of mine. –  Jubobs Jun 14 at 14:57
    
It is not a duplicate, because this question is about the spacing in comments only. Setting of columns for the whole listings affects the whole listings. –  Heiko Oberdiek Jun 14 at 16:12

1 Answer 1

up vote 3 down vote accepted
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{listings}

\begin{document}
    \begin{lstlisting}[language=Prolog,basicstyle=\ttfamily\footnotesize,
      commentstyle=\fontseries{lc}\selectfont\itshape,columns=fullflexible]
    hello :- world
    % This is a comment
    \end{lstlisting}    
\end{document}

Answer of Heiko Oberdiek:

Hijacking the answer, because the question is closed, when I was seconds from sending my anser. :-((((

A variation of Herbert's answer, which requires columns=fullflexible. The following example uses this setting for the comment only. Then other values of columns can be used, if vertical alignment is an issue and requires a less flexible setting for columns:

\documentclass{article}
\usepackage[a6paper]{geometry}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{listings}

\newcommand*{\mycommentstyle}[1]{%
  \begingroup
    \fontseries{lc}%
    \fontshape{it}%
    \selectfont
    \lstset{columns=fullflexible}%
    #1%
  \endgroup
}

\begin{document}
\begin{lstlisting}[
  language=Prolog,
  basicstyle=\ttfamily,
  commentstyle=\mycommentstyle,
]
a     :- b     % Dummy statement
hello :- world % This is a longer comment
% This is a comment again
\end{lstlisting}
\end{document}

Result

BTW, if the vertical alignment requirements allow it, I would prefer fullflexible or at least flexible to fixed as value for columns. The text is much nicer to read. There is even a variable width typewriter font (family lmvtt or option variablett for package lmodern).

share|improve this answer
    
Thanks for the variation Heiko. Is there a way of doing this comment only without creating a new command since this is just a one off? Also, is that a screenshot you attached or is there some markup command to compile tex code in pre blocks? –  Michael Jun 14 at 18:13
    
@Michael What dod you mean "this is just a one off"? Do you mean you only want that style for one comment? About the output in Herbert/Heiko's answer, that's a screenshot (image) inside a Markdown quote block; it looks good, but the semantics are wrong. –  Jubobs Jun 14 at 19:07

Your Answer

 
discard

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

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