[Tex/LaTex] How to avoid the use of default styles when no listings language is set

listings

When global listings styles have been defined identifiers are coloured differently from numbers even if no language is in effect. How can this be avoided?

\documentclass{minimal}
\usepackage{listings}
\usepackage{xcolor}
\lstset{identifierstyle=\color{purple}}
\begin{document}
\lstset{language=}
\begin{lstlisting}
123 hello world 456
\end{lstlisting}
\end{document}

enter image description here

Best Answer

As @jubobs commented, Listings treats language and style separately.

  • By defining a language, you tell Listings how it should analyze the code; such as "string in quotation marks" and //comment after double slashes.
  • By defining a style, you tell Listings how it should typeset the code; such as "strings goes italic" and //comments shrinks.

Go back to your question, a stylish setting is permanent unless you issue another stylish setting or the current group terminates. Similarly, a language assignment is permanent unless you assign another language, the current group terminates, or you issue a stylish setting that contains a language assignment.

In conclusion, perhaps the most systematic way to manage both language and style is to define an exhaustive style that contains a language assignment together with its associated stylish setting. (Just like a language-IDE pair.) And now you can switch between IDEs styles and the no-style-at-all style.

\documentclass{minimal}
\usepackage{listings,xcolor}
\begin{document}

\lstdefinestyle{my IDE setting}{
    language=Asymptote,
    identifierstyle=\color{purple}
}
\lstdefinelanguage{Asymptote}{
    keywords={draw,fill},
    morecomment=[l]{//}
}

\lstset{style=my IDE setting}
\lstinline{123 hello world 456}

\lstset{style=}
\lstinline{123 hello world 456}

\end{document}
Related Question