Document Classeslistings

Information and discussion about specific document classes and how to create your own document classes.
Post Reply
tomileee
Posts: 9
Joined: Tue Jul 08, 2008 11:49 am

listings

Post by tomileee »

I use the listings package to put some source code im my document.
The problem is, when i set the fontfamiliy to \ttfamily with basicstyle = \ttfamily, then the syntax isn't highlighted any more.
This are the settings for the package:

Code: Select all

\usepackage{listings} \lstset{numbers=left, numberstyle=\tiny, numbersep=5pt} \lstset{language=Python} 
\lstset{basicstyle = \ttfamily, captionpos=b}
And a source code example:

Code: Select all

\begin{lstlisting}[caption=if example]{ifexample}
a = 5
if a == 5:
  print a
elif a < 5 :
  print a + 1
else:
  print a + 2
\end{lstlisting}
Does anybody know how to fix this problem?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

l0calh05t
Posts: 19
Joined: Fri Jul 04, 2008 2:57 pm

listings

Post by l0calh05t »

Are you using the current version of the listings package? I also use that package and this never happened to me before.
Do you use the lstset command in the preamble? (and why 3 separate lstset commands?)

EDIT:
I just noticed you might not be using the lstlisting command correctly, because you apparently have one option too many. I think

Code: Select all

\begin{lstlisting}[caption=if example]{ifexample}
a = 5
if a == 5:
  print a
elif a < 5 :
  print a + 1
else:
  print a + 2
\end{lstlisting}
should actually be

Code: Select all

\begin{lstlisting}[caption=if example, label=ifexample]
a = 5
if a == 5:
  print a
elif a < 5 :
  print a + 1
else:
  print a + 2
\end{lstlisting}
(although apparently both work...)
tomileee
Posts: 9
Joined: Tue Jul 08, 2008 11:49 am

listings

Post by tomileee »

I changed it to a single command:

Code: Select all

\usepackage{listings} \lstset{numbers=left, numberstyle=\tiny, numbersep=5pt, language=Python, basicstyle = \ttfamily, captionpos=b}
and the example:

Code: Select all

\begin{lstlisting}[caption=ifExample, label=ifExample]
a = 5
if a == 5:
  print a
elif a < 5 :
  print a + 1
else:
  print a + 2
\end{lstlisting}
But I still have the problem.

My listings package is from 2007-03-13.

In our case, are the if, elif commands printed in bold letters?
User avatar
Stefan Kottwitz
Site Admin
Posts: 10322
Joined: Mon Mar 10, 2008 9:44 pm

listings

Post by Stefan Kottwitz »

Hi Tomileee,

there's no bold typeface of the cmtt font, the standard Computer Modern Typewriter font, have a look here in the UK TeX FAQ.
One possible solution is to use a monospaced font that provides a bold typeface, like courier, just try:

Code: Select all

\usepackage{courier}
Stefan
LaTeX.org admin
l0calh05t
Posts: 19
Joined: Fri Jul 04, 2008 2:57 pm

Re: listings

Post by l0calh05t »

Ah, that's why it never happened to me, I've been using the Bitstream Vera Mono font. (which is a very nice monospaced font imho, theres also a latex package called beramono)
tomileee
Posts: 9
Joined: Tue Jul 08, 2008 11:49 am

Re: listings

Post by tomileee »

Now it works. Thanx.
Best regards.
Post Reply