5

I was trying to imitate the code listings in the book Mathematical Recipes in C, I understand that it uses the CM TT font. What I am interested is in the proportion between the text and the code, and the treatment of the comments which are formatted as sans serif roman characters of the same size as the code. As I intend to include small text files with the original code I would like that no additional formatting is needed inside the code.

I have tried the listings package but I have some problem with the width of the characters in the listings environment this is what I have tried

\documentclass[fontsize=10pt]{article}
\usepackage{verbatim}
\usepackage{lipsum}
\usepackage{listings}
\lstset{
  morecomment=[s]{//},
  basicstyle=\ttfamily\footnotesize, 
  commentstyle=\rmfamily
}

\begin{document}
\lipsum[4]
\begin{lstlisting}
void square(float d){return d*d;} // formated with listings
\end{lstlisting}
{\footnotesize
\begin{verbatim}
void square(float d){return d*d;} // formatted with verbatim
\end{verbatim}
}
\end{document}

and I get this (compared with the output of verbatim):

enter image description here

The individual letters are identical but the listings ouput uses a lot more space. Is there a way to obtain in the listings package a result similar than in verbatim?

jub0bs
  • 58,916
  • 1
    Try to add columns=fullflexible to the argument of \lstset. – jub0bs Apr 11 '14 at 16:37
  • Somewhat related: http://tex.stackexchange.com/questions/164993/indent-content-of-verbatim-lstlisting-environment-relative-to-containing-envir – jub0bs Apr 11 '14 at 16:41
  • 1
    Thank you and sorry for the duplicate; I hadn't seen that question. The columns=fullflexible option solves my problem, I had not understood it's purpose in the doc. – Esteban Crespi Apr 11 '14 at 17:16
  • No worries. Finding a duplicate is not always easy. – jub0bs Apr 11 '14 at 17:17

0 Answers0