10

I've got a question (it's in the title) about listings that I do not believe have been answered anywhere (either that or my Google-fu isn't that sufficient). Before I knew of texcsstyle's star option, I tried to highlight double backslash (\\) by using the literate option (how monstrous):

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

With this I used to be able to highlight double backslash but since I didn't use texcsstyle I could not highlight the backslash that comes before all LaTeX commands. But when I finally DO use texcsstyle, the literate option suddenly no longer works for double backslash, even though all the other literate replacements (for &,[,],{,},...) still work. I guess texcsstyle is interfering with the literate option? How do I resolve this? Is it even possible to have both texcsstyle and double backslash highlighted?

Edit: added an example, sorry. Uncomment the line with texcsstyle and you will see the double backslash stop getting highlighted.

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

1 Answer 1

6

Here's a way of doing it. It's not particularly elegant, but does the work. The idea is to make \\ into a normal letter and then use keywords instead of texcs. Of course, with this way of doing things, you have to re-add by hand all the LaTeX keywords (but copy/paste from the listings source files would make that easy).

result of the code

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .