3

I just want to highlight digits for variable initialization,

to avoid colored number in equation und variable name,I have used the approach of Jubobs How to highlight all words of the form [0-9][A-Za-z0-9]* immediately following an equal sign?, then i have the result

enter image description here

to avoid colored number in equation, I tried to use escapeinside={!!}, but it causes the unexpected line break like this

enter image description here

Can someone help me to fix it?Thanks in advance

MWE

\documentclass{article}

\usepackage[usenames,dvipsnames,table]{xcolor}
\usepackage{listings}
\usepackage{lstautogobble}
\usepackage{fontspec}

\definecolor{code_gray}{RGB}{240,240,240}
\definecolor{code_green}{RGB}{0,127,0} % for comment
\setmonofont{DejaVu Sans Mono}

% Jubobs's approach
\makeatletter

\newif\iffirstchar\firstchartrue
\newif\ifstartedbyadigit
\newif\ifprecededbyequalsign

\newcommand\processletter
{%
    \ifnum\lst@mode=\lst@Pmode%
    \iffirstchar%
    \global\startedbyadigitfalse%
    \fi
    \global\firstcharfalse%
    \fi
}

\newcommand\processdigit
{%
    \ifnum\lst@mode=\lst@Pmode%
    \iffirstchar%
    \global\startedbyadigittrue%
    \fi
    \global\firstcharfalse%
    \fi
}

\lst@AddToHook{OutputOther}%
{%
    \lst@IfLastOtherOneOf{=}
    {\global\precededbyequalsigntrue}
    {}%
}

\lst@AddToHook{Output}%
{%
    \ifprecededbyequalsign%
    \ifstartedbyadigit%
    \def\lst@thestyle{\color{RedOrange}} %
    \fi
    \fi
    \global\firstchartrue%
    \global\startedbyadigitfalse%
    \global\precededbyequalsignfalse%
}
\lstdefinestyle{mymatlab}
{   language=Matlab,
    breaklines=true,
    basicstyle=\footnotesize\ttfamily,
    backgroundcolor = \color{code_gray},
    frame=shadowbox,
    commentstyle=\footnotesize\color{ForestGreen}\ttfamily,
    keywordstyle=\footnotesize\color{Cerulean}\ttfamily\bfseries,
    keepspaces, 
    alsoletter=0123456789.,  
    SelectCharTable=
    \lst@DefSaveDef{`0}\lsts@myzero{\lsts@myzero\processdigit}
    \lst@DefSaveDef{`1}\lsts@myone{\lsts@myone\processdigit}
    \lst@DefSaveDef{`2}\lsts@mytwo{\lsts@mytwo\processdigit}
    \lst@DefSaveDef{`3}\lsts@mythree{\lsts@mythree\processdigit}
    \lst@DefSaveDef{`4}\lsts@myfour{\lsts@myfour\processdigit}
    \lst@DefSaveDef{`5}\lsts@myfive{\lsts@myfive\processdigit}
    \lst@DefSaveDef{`6}\lsts@mysix{\lsts@mysix\processdigit}
    \lst@DefSaveDef{`7}\lsts@myseven{\lsts@myseven\processdigit}
    \lst@DefSaveDef{`8}\lsts@myeight{\lsts@myeight\processdigit}
    \lst@DefSaveDef{`9}\lsts@mynine{\lsts@mynine\processdigit}
    \lst@DefSaveDef{`a}\lsts@mya{\lsts@mya\processletter}
    \lst@DefSaveDef{`b}\lsts@myb{\lsts@myb\processletter}
    \lst@DefSaveDef{`c}\lsts@myc{\lsts@myc\processletter}
    \lst@DefSaveDef{`d}\lsts@myd{\lsts@myd\processletter}
    \lst@DefSaveDef{`e}\lsts@mye{\lsts@mye\processletter}
    \lst@DefSaveDef{`f}\lsts@myf{\lsts@myf\processletter}
    \lst@DefSaveDef{`g}\lsts@myg{\lsts@myg\processletter}
    \lst@DefSaveDef{`h}\lsts@myh{\lsts@myh\processletter}
    \lst@DefSaveDef{`i}\lsts@myi{\lsts@myi\processletter}
    \lst@DefSaveDef{`j}\lsts@myj{\lsts@myj\processletter}
    \lst@DefSaveDef{`k}\lsts@myk{\lsts@myk\processletter}
    \lst@DefSaveDef{`l}\lsts@myl{\lsts@myl\processletter}
    \lst@DefSaveDef{`m}\lsts@mym{\lsts@mym\processletter}
    \lst@DefSaveDef{`n}\lsts@myn{\lsts@myn\processletter}
    \lst@DefSaveDef{`o}\lsts@myo{\lsts@myo\processletter}
    \lst@DefSaveDef{`p}\lsts@myp{\lsts@myp\processletter}
    \lst@DefSaveDef{`q}\lsts@myq{\lsts@myq\processletter}
    \lst@DefSaveDef{`r}\lsts@myr{\lsts@myr\processletter}
    \lst@DefSaveDef{`s}\lsts@mys{\lsts@mys\processletter}
    \lst@DefSaveDef{`t}\lsts@myt{\lsts@myt\processletter}
    \lst@DefSaveDef{`u}\lsts@myu{\lsts@myu\processletter}
    \lst@DefSaveDef{`v}\lsts@myv{\lsts@myv\processletter}
    \lst@DefSaveDef{`w}\lsts@myw{\lsts@myw\processletter}
    \lst@DefSaveDef{`x}\lsts@myx{\lsts@myx\processletter}
    \lst@DefSaveDef{`y}\lsts@myy{\lsts@myy\processletter}
    \lst@DefSaveDef{`z}\lsts@myz{\lsts@myz\processletter}
    \lst@DefSaveDef{`A}\lsts@myA{\lsts@myA\processletter}
    \lst@DefSaveDef{`B}\lsts@myB{\lsts@myB\processletter}
    \lst@DefSaveDef{`C}\lsts@myC{\lsts@myC\processletter}
    \lst@DefSaveDef{`D}\lsts@myD{\lsts@myD\processletter}
    \lst@DefSaveDef{`E}\lsts@myE{\lsts@myE\processletter}
    \lst@DefSaveDef{`F}\lsts@myF{\lsts@myF\processletter}
    \lst@DefSaveDef{`G}\lsts@myG{\lsts@myG\processletter}
    \lst@DefSaveDef{`H}\lsts@myH{\lsts@myH\processletter}
    \lst@DefSaveDef{`I}\lsts@myI{\lsts@myI\processletter}
    \lst@DefSaveDef{`J}\lsts@myJ{\lsts@myJ\processletter}
    \lst@DefSaveDef{`K}\lsts@myK{\lsts@myK\processletter}
    \lst@DefSaveDef{`L}\lsts@myL{\lsts@myL\processletter}
    \lst@DefSaveDef{`M}\lsts@myM{\lsts@myM\processletter}
    \lst@DefSaveDef{`N}\lsts@myN{\lsts@myN\processletter}
    \lst@DefSaveDef{`O}\lsts@myO{\lsts@myO\processletter}
    \lst@DefSaveDef{`P}\lsts@myP{\lsts@myP\processletter}
    \lst@DefSaveDef{`Q}\lsts@myQ{\lsts@myQ\processletter}
    \lst@DefSaveDef{`R}\lsts@myR{\lsts@myR\processletter}
    \lst@DefSaveDef{`S}\lsts@myS{\lsts@myS\processletter}
    \lst@DefSaveDef{`T}\lsts@myT{\lsts@myT\processletter}
    \lst@DefSaveDef{`U}\lsts@myU{\lsts@myU\processletter}
    \lst@DefSaveDef{`V}\lsts@myV{\lsts@myV\processletter}
    \lst@DefSaveDef{`W}\lsts@myW{\lsts@myW\processletter}
    \lst@DefSaveDef{`X}\lsts@myX{\lsts@myX\processletter}
    \lst@DefSaveDef{`Y}\lsts@myY{\lsts@myY\processletter}
    \lst@DefSaveDef{`Z}\lsts@myZ{\lsts@myZ\processletter},
    emph=[1]%
    {% more keywords
        exp
    },
    emphstyle=[1]{\bfseries\color{RawSienna}},
    %
    escapeinside={!!},  
}

\makeatother

\begin{document}
 \begin{lstlisting}[style=mymatlab]

a = 1.1 
b = 2.2
c = 3e3

SP_Leitung = !1!/(1-r^2*exp(-1i*2*b*l))*...
                                   [-r+r*exp(-1i*2*b*l)...   % S_11
                                     (1-r^2)*exp(-1i*b*l);   % S_12
                                     (1-r^2)*exp(-1i*b*l)... % S_21
                                     -r+r*exp(-1i*2*b*l)];   % S_22
\end{lstlisting}

\end{document}
  • found a trick, use moredelim=[is][\color{Black}]{!}{!} instead of escapeinside={!!} – sun0727 Feb 28 '18 at 17:15
5

You have a lot of spurious spaces in the code which leds to spaces in the listings and so it breaks to a new line. Adding a lot of % helps:

\documentclass{article}

\usepackage[usenames,dvipsnames,table]{xcolor}
\usepackage{listings}
\usepackage{lstautogobble}
\usepackage{fontspec}

\definecolor{code_gray}{RGB}{240,240,240}
\definecolor{code_green}{RGB}{0,127,0} % for comment
\setmonofont{DejaVu Sans Mono}

% Jubobs's approach
\makeatletter

\newif\iffirstchar\firstchartrue
\newif\ifstartedbyadigit
\newif\ifprecededbyequalsign

\newcommand\processletter
{%
    \ifnum\lst@mode=\lst@Pmode%
    \iffirstchar%
    \global\startedbyadigitfalse%
    \fi
    \global\firstcharfalse%
    \fi
}

\newcommand\processdigit
{%
    \ifnum\lst@mode=\lst@Pmode%
    \iffirstchar%
    \global\startedbyadigittrue%
    \fi
    \global\firstcharfalse%
    \fi
}

\lst@AddToHook{OutputOther}%
{%
    \lst@IfLastOtherOneOf{=}%
    {\global\precededbyequalsigntrue}%
    {}%
}

\lst@AddToHook{Output}%
{%
    \ifprecededbyequalsign%
    \ifstartedbyadigit%
    \def\lst@thestyle{\color{RedOrange}}%
    \fi
    \fi
    \global\firstchartrue%
    \global\startedbyadigitfalse%
    \global\precededbyequalsignfalse%
}
\lstdefinestyle{mymatlab}
{   language=Matlab,
    breaklines=true,
    basicstyle=\footnotesize\ttfamily,
    backgroundcolor = \color{code_gray},
    frame=shadowbox,
    commentstyle=\footnotesize\color{ForestGreen}\ttfamily,
    keywordstyle=\footnotesize\color{Cerulean}\ttfamily\bfseries,
    keepspaces,
    alsoletter=0123456789.,
    SelectCharTable=
    \lst@DefSaveDef{`0}\lsts@myzero{\lsts@myzero\processdigit}%
    \lst@DefSaveDef{`1}\lsts@myone{\lsts@myone\processdigit}%
    \lst@DefSaveDef{`2}\lsts@mytwo{\lsts@mytwo\processdigit}%
    \lst@DefSaveDef{`3}\lsts@mythree{\lsts@mythree\processdigit}%
    \lst@DefSaveDef{`4}\lsts@myfour{\lsts@myfour\processdigit}%
    \lst@DefSaveDef{`5}\lsts@myfive{\lsts@myfive\processdigit}%
    \lst@DefSaveDef{`6}\lsts@mysix{\lsts@mysix\processdigit}%
    \lst@DefSaveDef{`7}\lsts@myseven{\lsts@myseven\processdigit}%
    \lst@DefSaveDef{`8}\lsts@myeight{\lsts@myeight\processdigit}%
    \lst@DefSaveDef{`9}\lsts@mynine{\lsts@mynine\processdigit}%
    \lst@DefSaveDef{`a}\lsts@mya{\lsts@mya\processletter}%
    \lst@DefSaveDef{`b}\lsts@myb{\lsts@myb\processletter}%
    \lst@DefSaveDef{`c}\lsts@myc{\lsts@myc\processletter}%
    \lst@DefSaveDef{`d}\lsts@myd{\lsts@myd\processletter}%
    \lst@DefSaveDef{`e}\lsts@mye{\lsts@mye\processletter}%
    \lst@DefSaveDef{`f}\lsts@myf{\lsts@myf\processletter}%
    \lst@DefSaveDef{`g}\lsts@myg{\lsts@myg\processletter}%
    \lst@DefSaveDef{`h}\lsts@myh{\lsts@myh\processletter}%%
    \lst@DefSaveDef{`i}\lsts@myi{\lsts@myi\processletter}%
    \lst@DefSaveDef{`j}\lsts@myj{\lsts@myj\processletter}%
    \lst@DefSaveDef{`k}\lsts@myk{\lsts@myk\processletter}%
    \lst@DefSaveDef{`l}\lsts@myl{\lsts@myl\processletter}%
    \lst@DefSaveDef{`m}\lsts@mym{\lsts@mym\processletter}%
    \lst@DefSaveDef{`n}\lsts@myn{\lsts@myn\processletter}%
    \lst@DefSaveDef{`o}\lsts@myo{\lsts@myo\processletter}%
    \lst@DefSaveDef{`p}\lsts@myp{\lsts@myp\processletter}%
    \lst@DefSaveDef{`q}\lsts@myq{\lsts@myq\processletter}%
    \lst@DefSaveDef{`r}\lsts@myr{\lsts@myr\processletter}%
    \lst@DefSaveDef{`s}\lsts@mys{\lsts@mys\processletter}%
    \lst@DefSaveDef{`t}\lsts@myt{\lsts@myt\processletter}%
    \lst@DefSaveDef{`u}\lsts@myu{\lsts@myu\processletter}%
    \lst@DefSaveDef{`v}\lsts@myv{\lsts@myv\processletter}%
    \lst@DefSaveDef{`w}\lsts@myw{\lsts@myw\processletter}%
    \lst@DefSaveDef{`x}\lsts@myx{\lsts@myx\processletter}%
    \lst@DefSaveDef{`y}\lsts@myy{\lsts@myy\processletter}%
    \lst@DefSaveDef{`z}\lsts@myz{\lsts@myz\processletter}%
    \lst@DefSaveDef{`A}\lsts@myA{\lsts@myA\processletter}%
    \lst@DefSaveDef{`B}\lsts@myB{\lsts@myB\processletter}%
    \lst@DefSaveDef{`C}\lsts@myC{\lsts@myC\processletter}%
    \lst@DefSaveDef{`D}\lsts@myD{\lsts@myD\processletter}%
    \lst@DefSaveDef{`E}\lsts@myE{\lsts@myE\processletter}%
    \lst@DefSaveDef{`F}\lsts@myF{\lsts@myF\processletter}%
    \lst@DefSaveDef{`G}\lsts@myG{\lsts@myG\processletter}%
    \lst@DefSaveDef{`H}\lsts@myH{\lsts@myH\processletter}%
    \lst@DefSaveDef{`I}\lsts@myI{\lsts@myI\processletter}%
    \lst@DefSaveDef{`J}\lsts@myJ{\lsts@myJ\processletter}%
    \lst@DefSaveDef{`K}\lsts@myK{\lsts@myK\processletter}%
    \lst@DefSaveDef{`L}\lsts@myL{\lsts@myL\processletter}%
    \lst@DefSaveDef{`M}\lsts@myM{\lsts@myM\processletter}%
    \lst@DefSaveDef{`N}\lsts@myN{\lsts@myN\processletter}%
    \lst@DefSaveDef{`O}\lsts@myO{\lsts@myO\processletter}%
    \lst@DefSaveDef{`P}\lsts@myP{\lsts@myP\processletter}%
    \lst@DefSaveDef{`Q}\lsts@myQ{\lsts@myQ\processletter}%
    \lst@DefSaveDef{`r}\lsts@myR{\lsts@myR\processletter}%
    \lst@DefSaveDef{`S}\lsts@myS{\lsts@myS\processletter}%
    \lst@DefSaveDef{`T}\lsts@myT{\lsts@myT\processletter}%
    \lst@DefSaveDef{`U}\lsts@myU{\lsts@myU\processletter}%
    \lst@DefSaveDef{`V}\lsts@myV{\lsts@myV\processletter}%
    \lst@DefSaveDef{`W}\lsts@myW{\lsts@myW\processletter}%
    \lst@DefSaveDef{`X}\lsts@myX{\lsts@myX\processletter}%
    \lst@DefSaveDef{`Y}\lsts@myY{\lsts@myY\processletter}%
    \lst@DefSaveDef{`Z}\lsts@myZ{\lsts@myZ\processletter}%
   ,
    emph=[1]%
    {% more keywords
        exp
    },
    emphstyle=[1]{\bfseries\color{RawSienna}},
    %
    escapeinside={!!},
}

\makeatother

\begin{document}
 \begin{lstlisting}[style=mymatlab]

a = 1.1
b = 2.2
c = 3e3

SP_Leitung = !1!/(1-r^2*exp(-1i*2*b*l))*...
                                   [-r+r*exp(-1i*2*b*l)...   % S_11
                                     (1-r^2)*exp(-1i*b*l);   % S_12
                                     (1-r^2)*exp(-1i*b*l)... % S_21
                                     -r+r*exp(-1i*2*b*l)];   % S_22
\end{lstlisting}

\end{document}

enter image description here

0

A Trick Use moredelim=[is][\color{Black}]{!}{!}, instead of escapeinside={!!}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.