(This only shows some failed attempts, hence is not a standard answer.)
As I commented under @UlfikeFischer's answer, morestring=[s]\\\%
creates a string pattern which begins at \\
and ends with \%
. Therefore for input text \large % text
, \large %
is recognized as a string and colored red. And this cannot be overwritten by morekeywords
and keywordstyle
.
I also tried listings
aspect texcs and keyword, none of them succeeds.
- When
comment=[l]\%
is set (this is the default setting), control characters such as \#
and \;
can both be set and highlighted as texcs or keywords, but this fails on \%
.
- When another character other than
%
is used as comment char, then \%
can be set and highlighted as texcs or keyword.
\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}
% a texcs attempt
\lstset{
language=[latex]tex,
basicstyle=\ttfamily,
texcsstyle=*\color{orange}, % the star (*) means highlight the backslash
commentstyle=\color{violet},
columns=fullflexible,
keepspaces=true,
upquote=true,
showstringspaces=false,
comment=[l], % clear comment setting
alsoletter={\#\;\%},
moretexcs={cmd, Foo, AtBeginDocument, \#, \;, \%}
}
\begin{document}
Use \verb|C| as comment character
\begin{lstlisting}[comment={[l]C}]
\cmd \Foo % Comment 1
\#\; Bar % Comment 2
Baz \% Not a Comment
\AtBeginDocument
\end{lstlisting}
Use \verb|\%| as comment character
\begin{lstlisting}[comment={[l]\%}]
\cmd \Foo % Comment 1
\#\; Bar % Comment 2
Baz \% Not a Comment
\AtBeginDocument
\end{lstlisting}
\end{document}

I think it is the precedence that matters. It looks like there is a precedence order like "string ~ comment > keyword ~ texcs".
lstlisting
environment, backslash character\
is just a normal character, so\%
is not interpreted as a command anymore. What other contents will you use inside thatlstlisting
environment?lstlisting
then? When I have mentioned[latex]tex
as the language, shouldn't it use LaTeX grammar parsing rules for all content inlstlisting
environment? I mean, Vim gets it right and does not highlight\% Not a comment
as a comment. Shouldn'tlstlisting
do the same thing?