# The % symbol overlaps inside a listing with MatLab code

I'm using the listings package to insert MatLab routines that I've created for some papers I need to write. Everything works awesome, except the symbol % (comment in MatLab) which overlaps with the next character. Below there's an image that shows this phenomenon.

And the MWE showing the problem is attached below:

\documentclass[letterpaper,12pt,openany,final]{memoir}
\usepackage[utf8x]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{graphicx} %Gráficos
\usepackage[top=4cm,right=2.5cm,left=3cm, bottom=3cm]{geometry}
\usepackage{ucs}
\usepackage{xcolor}
\usepackage{color,calc,graphicx,soul}
\usepackage{float}
\usepackage{rotating}
\usepackage{parskip}
\usepackage{mathptmx}%Fuente
\usepackage{epstopdf}

\setlength{\evensidemargin}{\oddsidemargin}

\title{Lorem Ipsum}
\author{DOlor Sit amet}

\usepackage{textcomp}
\usepackage{listingsutf8}
\renewcommand\lstlistingname{Código}
\renewcommand\lstlistlistingname{Índice de Códigos Fuente}

\lstset{
backgroundcolor=\color{white},
tabsize=4,
rulecolor=,
literate={ó}{{\'o}}1
{á}{{\'a}}1
{é}{{\'e}}1
{º}{{\textdegree}}1
{ú}{{\'u}}1,
escapeinside={\%*}{*)},
language=octave,
basicstyle=\scriptsize,
numberstyle=\tiny,
upquote=true,
aboveskip={1.5\baselineskip},
columns=fixed,
showstringspaces=false,
extendedchars=true,
breaklines=true,
prebreak = \raisebox{0ex}[0ex][0ex]{\ensuremath{\hookleftarrow}},
frame=single,
showtabs=false,
name=Feedservoplot
showspaces=false,
texcl=false,
inputencoding=latin1,
numbers=left,
firstnumber=auto,
showstringspaces=false,
identifierstyle=\ttfamily,
keywordstyle=\color[rgb]{0,0,1},
stringstyle=\color[rgb]{0.627,0.126,0.941},
escapeinside={(*@}{@*)}
}

\usepackage{amssymb}
\usepackage{amsmath}

\usepackage[activeacute,spanish] {babel}
\renewcommand\shorthandsspanish{}
\renewcommand{\arraystretch}{1.2}
\clubpenalty=10000
\widowpenalty=10000
\setcounter{tocdepth}{2}
\setcounter{secnumdepth}{2}
\setcounter{lofdepth}{2}
\usepackage[]{hyperref}
\usepackage{memhfixc}
\begin{document}
\setcounter{chapter}{1}

\setlength{\evensidemargin}{\oddsidemargin}

\chapter{Lorem ipsum}

Lorem ipsum dolor sit amet:

\begin{lstlisting}
a%
\end{lstlisting}

$$\frac{d}{dt} \frac{\partial \mathcal{L}}{\partial \dot{q_i}}$$
$$a<0$$

\end{document}


Any suggestions? Thanks in advance.

Best regards,

Charlie

PS: I must say that I don't know if this happens with other prog. languages.

• could you strip your code down a bit? presumably not all of the packages are necessary :) – cmhughes Jul 7 '13 at 23:11
• First of all try reducing the example to the bare minimum necessary for producing the issue; there are many packages and definition that are unlikely to be involved with the problem. On the contrary, the Test.m file is necessary. – egreg Jul 7 '13 at 23:11
• You might be interested in the matlab-prettifier package; see this answer. – jub0bs Apr 28 '14 at 15:36

The problem comes from the a bad interaction with the spanish module for babel, as the following MWE shows:

\documentclass{memoir}
\usepackage[spanish]{babel}
\usepackage{listingsutf8}
\spanishplainpercent

\begin{document}

\begin{lstlisting}
%A
%a
%b
\end{lstlisting}

\end{document}


The spanish module makes osme special treatment for the percentage symbol and this produces the undesired result. Using \spanishplainpercent solves the problem:

\documentclass{memoir}
\usepackage[spanish]{babel}
\usepackage{listingsutf8}
\spanishplainpercent

\begin{document}

\begin{lstlisting}
%A
%a
%b
\end{lstlisting}

\end{document}


If for other parts of your document you want to keep the fine space that the module introduces for the percentage symbol, you can do the following:

\documentclass{memoir}
\usepackage[spanish]{babel}
\usepackage{listingsutf8}

\makeatletter
\def\spanishplainpercent{\let\es@sppercent\@empty}
\def\spanishpercent{\def\es@sppercent{\unskip\textormath{$\m@th\,$}{\,}}}
\makeatother

\begin{document}

8\%

\spanishplainpercent 8\%
\begin{lstlisting}
%A
%a
%b
\end{lstlisting}
\spanishpercent

8\%

\end{document}


\spanishplainpercent deactivates the fine space (and solves the problem with listings) and \spanishpercent activates the space.

This can be done automatically using the etoolbox package:

\usepackage{etoolbox}

\makeatletter
\def\spanishplainpercent{\let\es@sppercent\@empty}
\def\spanishpercent{\def\es@sppercent{\unskip\textormath{$\m@th\,$}{\,}}}
\makeatother

\AtBeginEnvironment{lstlisting}{\spanishplainpercent}
\AtEndEnvironment{lstlistings}{\spanishpercent}

• It says "undefined control sequence \spanishplainpercent". Maybe I have to install more texlive packages. Any suggestions? – Charlie Jul 7 '13 at 23:36
• @Charlie Most probably your LaTeX installation is outdated. Update your LaTeX system. – Gonzalo Medina Jul 7 '13 at 23:38
• @Charlie before attempting the updating, please try the new code in my updated answer and see if this new code works with your system; if not, you'll need the updating. – Gonzalo Medina Jul 7 '13 at 23:40
• But it's the latex that came with Ubuntu 13.04. :O – Charlie Jul 7 '13 at 23:40
• @Charlie You're welcome! I wrote a little message to Javier Bezos (the author of the spanish module) about this issue. Let's wait for his answer/comments. – Gonzalo Medina Jul 8 '13 at 2:06