2

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?

Michael
  • 123
  • 5

1 Answers1

4
\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).

  • 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 '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. – jub0bs Jun 14 '14 at 19:07