1

I want to write some LaTeX code and insert it in a snippet, so I used \lstset in my main code, like this:

\documentclass[11pt]{article}
\usepackage{listings}
\usepackage{color}

\lstset{
language=TeX, 
backgroundcolor=\color{white}, 
basicstyle=\ttfamily, 
breakatwhitespace=false, 
breaklines=true, 
captionpos=b, 
commentstyle=\usefont{T1}{pcr}{m}{sl}\color{red}, 
deletekeywords={}, 
escapeinside={\%}, 
firstnumber=1, 
frame=leftline,
framerule=2.0pt,
keywordstyle=[1]\color{red}\ttfamily\bfseries, 
morekeywords=[1]{\documentclass}, 
keywordstyle=[2]\color{blue}\ttfamily, 
morekeywords=[2]{\begin, \end}, 
keywordstyle=[3]\color{green}\ttfamily, 
morekeywords=[3]{\item}, 
numbers=left, 
numbersep=10pt, 
numberstyle=\tiny\color{red}, 
rulecolor=\color{black}, 
showstringspaces=false, 
showtabs=false, 
stepnumber=2, 
stringstyle=\color{green}, 
tabsize=2, 
}


\begin{document}
This is the snippet code:
\lstinputlisting{demo_code.tex}

According to this code, for example, "documentclass" should look like

{\color{red}$\backslash$documentclass} instead of $\backslash${\color{red}{documentclass}}

Likewhise, "begin" should look like {\color{blue}$\backslash$begin} instead of $\backslash$\color{blue}{begin}

\end{document}

And this is the demo code inserted in the main code:

\documentclass[12pt]{article}

\begin{document}

    \begin{itemize}
        \item this is the first item
        \item this is the second item
        \item this is the third item
    \end{itemize}

\end{document}

So, the problem is that I don't know how to insert in the keywordstyle, commands with backslash symbol.

  • @TH I wouldn't like to argue, yet I feel that my answer is not duplicated. At least, the answer given is different than the one provided in the link "Listings, keywords with backslash in them". Thank you in advance. – user3060854 Jun 12 '17 at 20:32
2
  • Package listings has special option names for TeX command names (texcs).
  • An star in front of the value texcsstyle also includes the backslash in the formatting.
  • \item and \end needs to be removed from the keyword/texcs lists to redefine them with a different class.
  • The example also adds columns=fullflexible. It looks nicer to me.
  • Color green is quite bright with less contrast to the background color white as the other colors. Therefore, the example makes it a little darker.
\documentclass[11pt]{article}
\usepackage{listings}
\usepackage{xcolor}
\colorlet{lstgreen}{green!80!black}

\lstset{
  language=TeX, 
  backgroundcolor=\color{white}, 
  basicstyle=\ttfamily, 
  breakatwhitespace=false, 
  breaklines=true, 
  captionpos=b, 
  commentstyle=\usefont{T1}{pcr}{m}{sl}\color{red}, 
  deletetexcs={end,item},
  escapeinside={\%}, 
  firstnumber=1, 
  frame=leftline,
  framerule=2.0pt,
  texcsstyle=*[1]\color{red}\ttfamily\bfseries, 
  moretexcs=[1]{documentclass}, 
  texcsstyle=*[2]\color{blue}\ttfamily, 
  moretexcs=[2]{begin, end}, 
  texcsstyle=*[3]\color{lstgreen}\ttfamily, 
  moretexcs=[3]{item}, 
  numbers=left, 
  numbersep=10pt, 
  numberstyle=\tiny\color{red}, 
  rulecolor=\color{black}, 
  showstringspaces=false, 
  showtabs=false, 
  stepnumber=2, 
  stringstyle=\color{green}, 
  tabsize=2, 
  columns=fullflexible,
}

\begin{document}
\lstinputlisting{demo_code.tex}
\end{document}

Result

| improve this answer | |

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