列表突出显示“escapeinside”导致用户代码意外换行

列表突出显示“escapeinside”导致用户代码意外换行

我只是想突出显示变量初始化的数字,

为了避免方程式和变量名中出现彩色数字,我使用了Jubobs的方法如何突出显示紧跟等号的所有形式为 [0-9][A-Za-z0-9]* 的单词?然后我就得到了结果

在此处输入图片描述

为了避免公式中的彩色数字,我尝试使用escapeinside={!!},但它会导致像这样的意外换行

在此处输入图片描述

有人能帮我修复它吗?提前谢谢

平均能量损失

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

答案1

代码中有很多虚假空格,导致列表中出现空格,因此会换行。添加很多%帮助:

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

在此处输入图片描述

答案2

技巧使用moredelim=[is][\color{Black}]{!}{!},而不是escapeinside={!!}

在此处输入图片描述

相关内容