着色)列表中

着色)列表中

\我正在尝试使用该包在代码示例(PStricks)中创建一种使用颜色和括号的样式listings;我尝试使用 Marco Daniel 给出的答案这个问题literate但是,在我的情况下,它不起作用( ),因为它只使用颜色(而不是)。这是 MWE:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[svgnames]{xcolor}
\usepackage{listings}
\usepackage{beramono}
\definecolor{colKeys}{rgb}{0,0,1}
\definecolor{colIdentifier}{rgb}{0,0,0}
\definecolor{colString}{rgb}{0,0.5,0}
% Create a personal style colorate \ 
\lstdefinestyle{mystyle}{%
    language=PSTricks,%
    texcsstyle=*\color{blue},
    keepspaces=true,
    basicstyle=\ttfamily\small, %
    identifierstyle=\color{colIdentifier}, %
    keywordstyle=\color{colKeys}, %
    morekeywords={psrotate},
    stringstyle=\color{colString}, %
    commentstyle=\itshape\color{gray}, %
    columns=flexible, %
    keywordstyle=[2]\color{Purple}, %
    morekeywords=[2]{showgrid,nodesep,linecolor,dotscale,linewidth,unit,showpoints},
    deletekeywords=[1]{begin,end,pspicture,endpspicture},%
    keywordstyle=[3]\color{NavyBlue}, %
    morekeywords=[3]{begin,end,pspicture,endpspicture},
    keywordstyle=[4]\color{Orange}, %
    morekeywords=[4]{psEllipseTangents},%
    extendedchars=true, %
    showspaces=false, %
    showstringspaces=false, %
    breaklines=true, %
    breakautoindent=true, %
    xleftmargin=0pt,
    literate=*{\{}{{\textcolor{NavyBlue}{\{}}}{1}
        {\}}{{\textcolor{NavyBlue}{\}}}}{1}
        {[}{{\textcolor{NavyBlue}{[}}}{1}
        {]}{{\textcolor{NavyBlue}{]}}}{1}
        {(}{{\textcolor{NavyBlue}{(}}}{1}
        {)}{{\textcolor{NavyBlue}{)}}}{1}%
}[tex] % need for \ color
\lstset{style=mystyle}
\begin{document}
First
\begin{lstlisting}
\begin{pspicture}[showgrid=true](10,10)
\psdot(2,4) % a simple dot
\psellipse(7,7)(3,1.5)
\psEllipseTangents(7,7)(3,1.5)(2,4)
\pcline[nodesep=-1cm,linecolor=blue](2,4)(EllipseT1)
\pcline[nodesep=-1cm,linecolor=blue](2,4)(EllipseT2)
\psdots(EllipseT1)(EllipseT2)
\uput[-80](EllipseT1){T1}
\uput[115](EllipseT2){T2}
\end{pspicture}
\end{lstlisting}
Second
\begin{lstlisting}
\pspicture(-0.5,-3.5)(8.5,4.5)
\psset{unit=0.75}
\psaxes{->}(0,0)(-0.5,-3)(8.5,4.5)
\psdots[linecolor=red,dotscale=1.5](2,1)
\endpspicture
\end{lstlisting}

\end{document}

答案1

那是一个已知问题listings: 设置breaklinestrue中断任何涉及右括号的文字替换。

在此处输入图片描述

幸运的是,假设您的列表中没有出现反引号,您可以通过应用此补丁来补救这种情况(改编自egreg 的这个答案

\patchcmd{\lsthk@SelectCharTable}{`)}{``}{}{}

在此处输入图片描述

注意:如果需要,可以使用以下命令撤消补丁

\patchcmd{\lsthk@SelectCharTable}{``}{`)}{}{}

完整代码

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[svgnames]{xcolor}
\usepackage{listings}
\usepackage{beramono}
\definecolor{colKeys}{rgb}{0,0,1}
\definecolor{colIdentifier}{rgb}{0,0,0}
\definecolor{colString}{rgb}{0,0.5,0}
% Create a personal style colorate \ 
\lstdefinestyle{mystyle}{%
    language=PSTricks,%
    texcsstyle=*\color{blue},
    keepspaces=true,
    basicstyle=\ttfamily\small, %
    identifierstyle=\color{colIdentifier}, %
    keywordstyle=\color{colKeys}, %
    morekeywords={psrotate},
    stringstyle=\color{colString}, %
    commentstyle=\itshape\color{gray}, %
    columns=flexible, %
    keywordstyle=[2]\color{Purple}, %
    morekeywords=[2]{showgrid,nodesep,linecolor,dotscale,linewidth,unit,showpoints},
    deletekeywords=[1]{begin,end,pspicture,endpspicture},%
    keywordstyle=[3]\color{NavyBlue}, %
    morekeywords=[3]{begin,end,pspicture,endpspicture},
    keywordstyle=[4]\color{Orange}, %
    morekeywords=[4]{psEllipseTangents},%
    extendedchars=true, %
    showspaces=false, %
    showstringspaces=false, %
    breaklines=true, %
    breakautoindent=true, %
    xleftmargin=0pt,
    literate=*{\{}{{\textcolor{NavyBlue}{\{}}}{1}
        {\}}{{\textcolor{NavyBlue}{\}}}}{1}
        {[}{{\textcolor{NavyBlue}{[}}}{1}
        {]}{{\textcolor{NavyBlue}{]}}}{1}
        {(}{{\textcolor{NavyBlue}{(}}}{1}
        {)}{{\textcolor{NavyBlue}{)}}}{1}%
}[tex] % need for \ color
\lstset{style=mystyle}

\usepackage{etoolbox} % provides the \patchcmd macro
\makeatletter
\patchcmd{\lsthk@SelectCharTable}{`)}{``}{}{} % patch listings
%\patchcmd{\lsthk@SelectCharTable}{``}{`)}{}{} % undo patch if needed
\makeatother

\begin{document}
First
\begin{lstlisting}
\begin{pspicture}[showgrid=true](10,10)
\psdot(2,4) % a simple dot
\psellipse(7,7)(3,1.5)
\psEllipseTangents(7,7)(3,1.5)(2,4)
\pcline[nodesep=-1cm,linecolor=blue](2,4)(EllipseT1)
\pcline[nodesep=-1cm,linecolor=blue](2,4)(EllipseT2)
\psdots(EllipseT1)(EllipseT2)
\uput[-80](EllipseT1){T1}
\uput[115](EllipseT2){T2}
\end{pspicture}
\end{lstlisting}
Second
\begin{lstlisting}
\pspicture(-0.5,-3.5)(8.5,4.5)
\psset{unit=0.75}
\psaxes{->}(0,0)(-0.5,-3)(8.5,4.5)
\psdots[linecolor=red,dotscale=1.5](2,1)
\endpspicture
\end{lstlisting}

\end{document}

相关内容