GeneralListings package == issue

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
alastairandrew
Posts: 8
Joined: Sun Aug 31, 2008 10:08 pm

Listings package == issue

Post by alastairandrew »

Hi,

I'm having a problem using the Listings package to typeset some code. Anywhere that it should display the == equality operator it renders this as a single wide equals. I'm using the following option: basicstyle=\sffamily to change the font into a sans serif one.

Any comments would be appreciated.
Thanks,
Alastair

Recommended reading 2024:

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

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

User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Listings package == issue

Post by gmedina »

Hi,

please post a minimal working example showing the undesired behaviour.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
alastairandrew
Posts: 8
Joined: Sun Aug 31, 2008 10:08 pm

Listings package == issue

Post by alastairandrew »

Here's a minimal example reproducing the problem on my machine. The setup I'm using is I'm pdfLatex from gwTex on a Mac.

Code: Select all

\documentclass{letter}
\usepackage{listings}
\begin{document}
\begin{lstlisting}[language=C,basicstyle=\sffamily]
if(a == b){
	print("Example");
}
\end{lstlisting}
\end{document}
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Listings package == issue

Post by localghost »

From my point of view another basic style fits better for source code of any kind.

Code: Select all

\documentclass{letter}
\usepackage{listings}

\begin{document}
\begin{lstlisting}[language=C,basicstyle=\ttfamily]
if(a == b){
  print("Example");
}
\end{lstlisting}
\end{document}
And it solves your problem.


Best regards
Thorsten¹
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Listings package == issue

Post by gmedina »

Hi alastairandrew,

I agree with localghost's previous suggestion. If, however, you need the sans serif font for your code, you can solve the issue by leaving a blank space between the equal signs or escaping to LaTeX (see the listings documentation) and adding the space; the following code shows both approaches:

Code: Select all

\documentclass{letter}
\usepackage{listings}

\begin{document}

\begin{lstlisting}[language=C,basicstyle=\sffamily]
if(a = = b){
  print("Example");
}
\end{lstlisting}

\begin{lstlisting}[language=C,basicstyle=\sffamily,escapechar= @]
if(a =@\,\,@= b){
  print("Example");
}
\end{lstlisting}

\end{document}
1,1,2,3,5,8,13,21,34,55,89,144,233,...
alastairandrew
Posts: 8
Joined: Sun Aug 31, 2008 10:08 pm

Re: Listings package == issue

Post by alastairandrew »

Thanks for your replies.

I'd like to stick to the sans serif font, it looks cleaner and a bit more modern than the old typewriter family. Escaping the LaTeX or putting spaces in do work, the drawback is that I'm using \lstinputlisting to load my source code straight into my document so I was hoping there would be a solution that didn't involve actually manipulating the individual code listings.

Is there anyway of redefining how Listings treats characters?
alastairandrew
Posts: 8
Joined: Sun Aug 31, 2008 10:08 pm

Listings package == issue

Post by alastairandrew »

Finally cracked it by taking gmedina's suggestion of putting a space in between the equals signs. Rather than editing all my source to do this I scoured the Listings manual and found the literate command.

Placing

Code: Select all

literate={==}{= =}{3}
into the input command seemed to sort things out exactly as required.

Thanks for all your help.
All the best,
Alastair
User avatar
frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

Listings package == issue

Post by frabjous »

alastairandrew wrote:Thanks for your replies.

I'd like to stick to the sans serif font, it looks cleaner and a bit more modern than the old typewriter family.
Completely understood. You might consider, however, the more modern looking typewriter/monospace fonts offered by the bera and inconsolata packages. Somehow code just looks more "like code" in a monospace font.
Post Reply