Fonts & Character SetsListing package displays curly 'quotes' and tiny tildes ~

Information and discussion about fonts and character sets (e.g. how to use language specific characters)
stinkinrich88
Posts: 52
Joined: Sat Jun 07, 2008 11:56 am

Listing package displays curly 'quotes' and tiny tildes ~

Post by stinkinrich88 »

Hello,

I would like to include some Matlab code in my latex document. I use the listing package to do this, however, the quotation marks in my code are made curly (both curve to the right). I want straight quotes. Also, the tilde symbol (meaning not() in matlab) is displayed too small and too high.

I would like to avoid using monospaced font as I would like syntax highlighting.

Thank you,

p.s. here is some code:

preamble:

Code: Select all

\lstset{frame=lines,tabsize=4,numbers=left, numberstyle=\tiny}
document:

Code: Select all

\lstset{language=Matlab}
\begin{lstlisting}[float, caption=test, label=test]
### code here ###
\end{lstlisting}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
stinkinrich88
Posts: 52
Joined: Sat Jun 07, 2008 11:56 am

Re: Listing package displays curly 'quotes' and tiny tildes ~

Post by stinkinrich88 »

Hey Rich, use this in you lstset command in the preamble:

upquote=true

and use the textcomp package. This will sort out your curly quotes.

As for the tilde, you'll have to do this manually, I'm afraid. So first we need the listings package to recognise an escape sequence. Use the following code in your lstset command to do this:

escapeinside={\%*}{*)}

now you can escape to latex between characters %* and *)

So go through you code, delete all ~ and replace them with %*$\sim$*)

Tidy!
stinkinrich88
Posts: 52
Joined: Sat Jun 07, 2008 11:56 am

Listing package displays curly 'quotes' and tiny tildes ~

Post by stinkinrich88 »

Better still, for the tilde problem:

Use the following code to include the listings package:

Code: Select all

\usepackage[formats]{listings}
Create a format to replace weird tildes with proper ~'s:

Code: Select all

\lstdefineformat{C}{\~=$\sim$}
And include the following line in you lstset command (in the preamble):

Code: Select all

format=C
Now all weird tildes will be replaced automatically in all of your code. Forget escape characters etc, this is all you need!
dontpanic
Posts: 5
Joined: Mon Jul 26, 2010 1:13 pm

Re: Listing package displays curly 'quotes' and tiny tildes

Post by dontpanic »

Thanks stinkinrich88!

It solves my tilde problem too, but now tabsize=4 doesn't work anymore:

\lstset{ breaklines=true, % new line
basicstyle=\ttfamily,
tabsize=4}

\begin{lstlisting}[language=R, format=tilde]
...
\end{lstlisting}

output looks like tabsize=0
stinkinrich88
Posts: 52
Joined: Sat Jun 07, 2008 11:56 am

Listing package displays curly 'quotes' and tiny tildes ~

Post by stinkinrich88 »

Hi dontpanic, glad it helped you!

I haven't worked on this for ages, so I might not be very helpful now, I'm afraid... Here is what I used in my preamble to set-up the listings package:

Code: Select all

\lstset{ 
	upquote=true,
	language=Matlab,
	basicstyle=\footnotesize,
	frame=lines,
	tabsize=4,
	numbers=left,
	numberstyle=\tiny,
	tabsize=2,
	captionpos=b,
	%breaklines=true,  
	%breakatwhitespace=false,
	escapeinside={\%*}{*)}
}
And here is the code I used to insert a snippet of matlab:

Code: Select all

\begin{lstlisting}[float, caption=Snippet of code used to check whether the current simulation data is valid, label=lst:SettingsBackup]
Have a fiddle with that and see if you can get it looking nicer. Mine looks like this:

Image

Beautiful!
dontpanic
Posts: 5
Joined: Mon Jul 26, 2010 1:13 pm

Re: Listing package displays curly 'quotes' and tiny tildes

Post by dontpanic »

unfortunately not better...

i don't understand, why you use tabsize= twice in lstset{}

And why do you need escapeinside={\%*}{*)}?
("Now all weird tildes will be replaced automatically in all of your code. Forget escape characters etc, this is all you need!")

Thanks!
dontpanic
stinkinrich88
Posts: 52
Joined: Sat Jun 07, 2008 11:56 am

Listing package displays curly 'quotes' and tiny tildes ~

Post by stinkinrich88 »

Hmmm... I think the two tabsize settings was a mistake. Remove the first one and it should stay the same.

For some reason, I did not actually use my advice in the second post about the tilde problem (i.e. use formatting). Instead, I did what the first post says (replaced all tildes with

Code: Select all

%*$\sim$*)
) and used the escape sequence. This explains why I still needed the escape sequence code.

So, the last line of code in the picture I posted looks like this in my latex source:

Code: Select all

if %*$\sim$*)isempty(stepTooFar), break; end
It depends how much trouble it is for you to replace all tildes with this. I just tested my second suggestion (formatting) and it does seem to work just as well. It sounds like a better solution, to me. Again, I'm not sure why I chose not to use it. (probably couldn't be bothered to change my code once I worked it out).

Hope this helps
dontpanic
Posts: 5
Joined: Mon Jul 26, 2010 1:13 pm

Listing package displays curly 'quotes' and tiny tildes ~

Post by dontpanic »

I don't know why, but none of these solutions work... I also tried escapechar:
I am using two languages (SAS and R) and the escapechar only worked for SAS, in R it says "undefined control sequence".

So I've defined a new environment for R:

Code: Select all

\lstnewenvironment{R}
{
\lstset{
	basicstyle=\ttfamily,
	frame=lines,
	tabsize=4,
	captionpos=b,
	breaklines=true,
	showstringspaces=false,
	keywordstyle=\color{darkblue},
	stringstyle=\color{darkred},
	identifierstyle=\bfseries, 
	commentstyle=\color{darkgreen},
	escapechar={\%}
	}}
{}
Now the escapechar is working fine (%$\sim$%), but the colors for comments, keywords,... doesn't work anymore...

Thanks,
dontpanic
stinkinrich88
Posts: 52
Joined: Sat Jun 07, 2008 11:56 am

Re: Listing package displays curly 'quotes' and tiny tildes

Post by stinkinrich88 »

Could you post a minimal working example for me to play with?
dontpanic
Posts: 5
Joined: Mon Jul 26, 2010 1:13 pm

Listing package displays curly 'quotes' and tiny tildes ~

Post by dontpanic »

A short example:

Code: Select all

\documentclass[pdftex,a4paper]{scrreprt}
\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage{color}
\usepackage[formats]{listings}
\definecolor{darkred}{rgb}{.5,0,0}
\definecolor{darkblue}{rgb}{0,0,.5}
\lstnewenvironment{R}
{
\lstset{
	basicstyle=\ttfamily,
	frame=lines,
	tabsize=4,
	showstringspaces=false,
	keywordstyle=\color{darkblue},
	stringstyle=\color{darkred},
	identifierstyle=\bfseries, 
	escapechar={\%}
	}}
{}
\lstset{
	basicstyle=\ttfamily,
	frame=lines,
	tabsize=4,
	showstringspaces=false,
	keywordstyle=\color{darkblue},
	stringstyle=\color{darkred},
	identifierstyle=\bfseries, 
	escapechar={\%}
	}
\begin{document}
lorem ipsum
\begin{lstlisting}[language=SAS]
	%$\sim$%
	set &dat1;
	title1 'test';
\end{lstlisting}
\begin{R}
	#comment
	%$\sim$%
        importXPORT <- function(filename) {
}
\end{R}
\end{document}
Thank you!
Post Reply