使用 texcsstyle 列表突出显示双反斜杠

使用 texcsstyle 列表突出显示双反斜杠

我有一个关于列表的问题(在标题中),我认为没有任何地方回答过这个问题(要么是我的 Google 能力不够)。在我知道 的星号选项之前,我尝试使用以下选项texcsstyle突出显示双反斜杠 ( ) (多么可怕):\\literate

literate=*{\\\\}{{\textcolor{red}{\textbackslash{}\textbackslash{}}}}{1}

有了这个,我以前能够突出显示双反斜杠,但由于我没有使用,texcsstyle我无法突出显示所有 LaTeX 命令之前的反斜杠。但当我最终使用时texcsstyle,该literate选项突然不再适用于双反斜杠,即使所有其他literate替换(对于、、、、、&... )仍然有效。我猜是干扰了选项?我该如何解决这个问题?是否有可能同时突出显示和双反斜杠?[]{}texcsstyleliteratetexcsstyle

编辑:添加了一个示例,抱歉。取消注释 texcsstyle 行,您将看到双反斜杠不再突出显示。

\documentclass{article}
\usepackage[T1]{fontenc}  
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{listings}
\usepackage{charter}
\definecolor{lightgrey}{rgb}{0.9,0.9,0.9}
\definecolor{darkgreen}{rgb}{0,0.6,0}
\definecolor{darkred}{rgb}{0.6,0,0}
\definecolor{myblue}{RGB}{20,105,176}
\definecolor{darkgreen}{rgb}{0,0.6,0}
\lstdefinelanguage{mytex}[LaTeX]{TeX}{
  moretexcs={includegraphics,multicolumn,multirow,newcolumntype,intertext,},
  morekeywords=[2]{figure,tabular,tabulary,document,minipage,verbatim,table,
    enumerate,tikzpicture,preview},  
  % texcsstyle=*\bfseries\color{darkred},
  literate=
            *{\{}{{\textcolor{myblue}{\{}}}{1}
            {\}}{{\textcolor{myblue}{\}}}}{1}
            {\&}{{\textcolor{red}{\&}}}{1}
            {\\\\}{{\textcolor{red}{\textbackslash{}\textbackslash{}}}}{1}
            {[}{{\textcolor{myblue}{[}}}{1}
            {]}{{\textcolor{myblue}{]}}}{1},
}
\lstset{language=mytex}
\lstdefinestyle{mystyle1}{
basicstyle=\small\ttfamily,
keywordstyle=\color{purple}\bfseries,
keywordstyle=[2]{\color{magenta}},
commentstyle=\color{darkgreen}, 
stringstyle=\color{orange},
identifierstyle=\ttfamily,
showstringspaces=true,
breaklines=true,
tabsize=4,
columns=fullflexible,
keepspaces=true,
}
\lstset{style=mystyle1}
\begin{document}



\begin{lstlisting}
\begin{alignat*}{4}
   y & = -4   & + 3 & +4     & -7      \\
   y & =      & + 3 &        & -7      \\
   \intertext{therefore}
   a & = b    & d   & = cccc & e & = d \\
   a & = bbbb & d   & = c    & e & =   \\
\end{alignat*}
\end{lstlisting}

\end{document}

答案1

这是一种方法。它不是特别优雅,但确实有效。这个想法是将其变成\\普通字母,然后使用keywords代替texcs。当然,使用这种方法,您必须手动重新添加所有 LaTeX 关键字(但从列表源文件中复制/粘贴会使这变得容易)。

代码结果

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{listings}
\usepackage{lmodern}
\usepackage{charter}

\definecolor{lightgrey}{rgb}{0.9,0.9,0.9}
\definecolor{darkgreen}{rgb}{0,0.6,0}
\definecolor{darkred}{rgb}{0.6,0,0}
\definecolor{myblue}{RGB}{20,105,176}
\definecolor{darkgreen}{rgb}{0,0.6,0}

\lstdefinelanguage{mytex}[LaTeX]{TeX}{
  alsoletter={\\,*,\&},
  morekeywords={\\begin,
                \\end,
                \\intertext,
                \\\\,
                \&},  
  morekeywords=[2]{figure,
                   tabular,
                   tabulary,
                   document,
                   minipage,
                   verbatim,
                   table,
                   enumerate,
                   tikzpicture,
                   preview,
                   alignat*},  
  literate=*{\{}{{\textcolor{myblue}{\{}}}{1}
            {\}}{{\textcolor{myblue}{\}}}}{1}
            {[}{{\textcolor{myblue}{[}}}{1}
            {]}{{\textcolor{myblue}{]}}}{1},
}

\lstset{language=mytex}

\lstdefinestyle{mystyle1}{
  basicstyle=\small\ttfamily,
  keywordstyle=\bfseries\color{red},
  keywordstyle=[2]{\color{magenta}},
  commentstyle=\color{darkgreen}, 
  stringstyle=\color{orange},
  identifierstyle=\ttfamily,
  showstringspaces=true,
  breaklines=true,
  tabsize=4,
  columns=fullflexible,
  keepspaces=true,
}

\lstset{style=mystyle1}

\begin{document}

\begin{lstlisting}
\begin{alignat*}{4}
   y & = -4   & + 3 & +4     & -7      \\
   y & =      & + 3 &        & -7      \\
   \intertext{therefore}% comment
   a & = b    & d   & = cccc & e & = d \\
   a & = bbbb & d   & = c    & e & =   \\
\end{alignat*}
\end{lstlisting}

\end{document}

相关内容