2

I want to highlight some TeX code listings, so I use lstlisting.

\begin{lstlisting}[language=TeX] 
\begin{tabular}{ |p{4cm}|p{4cm}|p{4cm}|  }
\hline
\multicolumn{3}{|c|}{Country List} \\
\hline
Country Name & ISO ALPHA 2 & ISO ALPHA 3 \\
\hline
Afghanistan & AF & AFG \\
Aland Islands & AX & ALA \\
Albania & AL & ALB \\
\hline
\end{tabular}
\end{lstlisting}

My lstset is:

\lstset{extendedchars=\true,keepspaces=true,
basicstyle=\ttfamily,columns=flexible,
escapechar=$,escapebegin=\[,escapeend=\]
    backgroundcolor=\color{backcolour},   
    commentstyle=\color{codegreen},
    keywordstyle=\color{magenta},
    numberstyle=\color{codegray},
    stringstyle=\color{codepurple},
    breakatwhitespace=false,         
    breaklines=true,                 
    captionpos=b,                        
    numbersep=5pt,                  
    showspaces=false,                
    showstringspaces=false,
    showtabs=false,                  
    tabsize=2
}

But I'm not happy with the result - only end became magenta and other text is black:

Result

I believe begin is a keyword on an equal basis with end, but it's black too. Is there any way to make TeX code highlighted?

1

1 Answer 1

6

Your problem arises from asking listings to highlight TeX code when you are actually giving it LaTeX code. In TeX \begin, \hline, \multicolumn, etc. are not used and are accordingly not highlighted. Simply change your language to LaTeX by adding language=[LaTeX]{TeX} to your \lstset.

\documentclass{article}
\usepackage{listings, xcolor}
\lstset{extendedchars=\true,keepspaces=true,
basicstyle=\ttfamily,columns=flexible,
escapechar=$,escapebegin=\[,escapeend=\]
    backgroundcolor=\color{backcolour},   
    commentstyle=\color{codegreen},
    keywordstyle=\color{magenta},
    numberstyle=\color{codegray},
    stringstyle=\color{codepurple},
    breakatwhitespace=false,         
    breaklines=true,                 
    captionpos=b,                        
    numbersep=5pt,                  
    showspaces=false,                
    showstringspaces=false,
    showtabs=false,                  
    tabsize=2,
    language=[LaTeX]{TeX} %<--
}
\begin{document}
\begin{lstlisting}
\begin{tabular}{ |p{4cm}|p{4cm}|p{4cm}|  }
\hline
\multicolumn{3}{|c|}{Country List} \\
\hline
Country Name & ISO ALPHA 2 & ISO ALPHA 3 \\
\hline
Afghanistan & AF & AFG \\
Aland Islands & AX & ALA \\
Albania & AL & ALB \\
\hline
\end{tabular}
\end{lstlisting}
\end{document}

enter image description here

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