Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
1  
It would be helpful if you composed a fully compilable MWE including \documentclass and the appropriate packages that sets up the problem. While solving problems is fun, setting them up is not. Then those trying to help can simply cut and paste your MWE and get started on solving problem. –  Peter Grill Nov 29 '12 at 21:21
    
Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. –  Jubobs Mar 1 at 18:59

1 Answer 1

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}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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