[Tex/LaTex] The % symbol overlaps inside a listing with MatLab code

listingsMATLABsymbols

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.

enter image description here

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},
        commentstyle=\color[rgb]{0.133,0.545,0.133},
        stringstyle=\color[rgb]{0.627,0.126,0.941},
        escapeinside={(*@}{@*)}
}
    
\usepackage[protrusion=true,expansion=true]{microtype}  %Alineado óptico
\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}

\begin{equation}
\frac{d}{dt} \frac{\partial \mathcal{L}}{\partial \dot{q_i}}
\end{equation} 
\begin{equation}
 a<0
\end{equation} 

\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.

Best Answer

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}

enter image description here

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}

enter image description here

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}

enter image description here

\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}
Related Question