7

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

2
  • 1
    I'm not sure I understand the problem. This is the behaviour I expect. listings is designed in such a way that language and style be (mostly) orthogonal. Why not simply write identifierstyle= where you want to reset that style?
    – jub0bs
    Sep 4, 2015 at 18:24
  • 1
    I would think that an undefined language would not distinguish between identifiers and numbers. However, your suggestion led me to a solution: define a language named "none" where I reset the identifier style. Sep 5, 2015 at 5:42

2 Answers 2

8

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}
3
  • Nice approach! Unfortunately, the globally defined styles come from my publisher, so I cannot put them into an lstdefinelanguage block. Also, I'll be using many languages, so defining stylish statements for each language would be a significant burden. Sep 5, 2015 at 5:51
  • @DiomidisSpinellis you do not need to define any new language or style. You can always use/modify an old one. Nor do you need to include any language assignment in a style definition. Take a look at the other answer, you will see that the one and only point is that language and style are parallel unless you link them. There are still many approaches such as grouping or creating a new environment.
    – Symbol 1
    Sep 5, 2015 at 6:12
  • Thank you! I am not allowed to modify the old one, hence the approach used in the other answer. Sep 5, 2015 at 19:37
8

Based on the suggestion by @jubobs, a solution is to define a language name none that resets the globally defined styles.

\documentclass{minimal}
\usepackage{listings}
\usepackage{xcolor}
\lstset{identifierstyle=\color{purple}}
\lstdefinelanguage{none}{
  identifierstyle=
}
\begin{document}
\lstset{language=none}
\begin{lstlisting}
123 hello world 456
\end{lstlisting}
\end{document}
enter image description here

0

You must log in to answer this question.

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