1

Problem description

I am looking for a way to make the listings package automatically apply my preferred style to a given language, without me having to manually set it via \lstset{style=...}.

Specifically, I am using the sclang-prettifier package to typeset SuperCollider code, and I would like to automatically invoke its SuperCollider-IDE style whenever I introduce a code example using the package's SuperCollider language definition.

The long-term objective is to be able to typeset multiple programming languages in the same document, without having to specify both, language and style for each code example. That's why \lstset won't work for me.

Minimal (non-)working example

Save the following example to main.tex and compile with pdflatex main or lualatex main.

\documentclass{article}

\usepackage{listings}%

% 'sclang-prettifier' package defines 'SuperCollider' language
% and 'SuperCollider-IDE' style for 'listings' package
\usepackage{sclang-prettifier}%

% HACK! Define new 'sc' language to couple
% 'SuperCollider-IDE' style to 'SuperCollider' language
\lstdefinelanguage{sc}{%
  style=SuperCollider-IDE,%
}%


\begin{document}


\section{Automatically use preferred style with given language (does not work)}

% NOTE: 'SuperCollider' language defined by \usepackage{sclang-prettifier}

I'd like to \emph{automatically} apply the \verb|SuperCollider-IDE| style to any
code listing that uses \verb|language=SuperCollider|. Currently this does not work,
which is why the following listing will \emph{not} be colored: 

\begin{lstlisting}[language=SuperCollider]
  { SinOsc.ar(440, 0, 0.5) }.play;  // Comment
\end{lstlisting}


\section{Define new language to automatically set style (works partially)}

One idea I had was to define a new language \verb|sc|, which sets the \verb|SuperCollider-IDE|
style, and which I could use to replace the \verb|SuperCollider| language definition.
However, while this colors comments in red as expected, UGens such as \verb|SinOsc| remain black.

\begin{lstlisting}[language=sc]
  { SinOsc.ar(440, 0, 0.5) }.play;  // Comment
\end{lstlisting}


\section{Workaround: Manually define style (inconvenient)}

The only solution that colors both, comments and UGens, as expected is to manually define the
\verb|SuperCollider-IDE| style. However, I'd like to avoid having to do this and would instead
like to \emph{automatically} use the \verb|SuperCollider-IDE| style with the \verb|SuperCollider|
language. 

\lstset{style=SuperCollider-IDE}
\begin{lstlisting}[language=SuperCollider]
  { SinOsc.ar(440, 0, 0.5) }.play;  // Comment
\end{lstlisting}


\end{document}
  • It would be great if someone with sufficient reputation could create a sclang-prettifier tag and add it to this question. – Florian H. Nov 26 '16 at 2:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.