General ⇒ Listings package == issue
-
- Posts: 8
- Joined: Sun Aug 31, 2008 10:08 pm
Listings package == issue
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
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
NEW: TikZ book now 40% off at Amazon.com for a short time.
Listings package == issue
1,1,2,3,5,8,13,21,34,55,89,144,233,...
-
- Posts: 8
- Joined: Sun Aug 31, 2008 10:08 pm
Listings package == issue
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}
- localghost
- Site Moderator
- Posts: 9202
- Joined: Fri Feb 02, 2007 12:06 pm
Listings package == issue
From my point of view another basic style fits better for source code of any kind.
And it solves your problem.
Best regards
Thorsten¹
Code: Select all
\documentclass{letter}
\usepackage{listings}
\begin{document}
\begin{lstlisting}[language=C,basicstyle=\ttfamily]
if(a == b){
print("Example");
}
\end{lstlisting}
\end{document}
Best regards
Thorsten¹
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10
Listings package == issue
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:
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,...
-
- Posts: 8
- Joined: Sun Aug 31, 2008 10:08 pm
Re: Listings package == issue
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?
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?
-
- Posts: 8
- Joined: Sun Aug 31, 2008 10:08 pm
Listings package == issue
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 into the input command seemed to sort things out exactly as required.
Thanks for all your help.
All the best,
Alastair
Placing
Code: Select all
literate={==}{= =}{3}
Thanks for all your help.
All the best,
Alastair
Listings package == issue
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.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.