问题描述

问题描述

问题描述

我正在寻找一种方法来让listings包自动将我喜欢的风格应用到给定的语言,而无需我通过手动设置它\lstset{style=...}

具体来说,我正在使用该sclang-prettifier包来排版 SuperCollider 代码,并且我希望每当我使用该包的语言定义SuperCollider-IDE引入代码示例时自动调用它的样式。SuperCollider

长期目标是能够在同一文档中排版多种编程语言,而不必为每个代码示例指定语言和样式。这就是为什么它对\lstset我来说不起作用。

最小(非)工作示例

将以下示例保存到main.tex并使用pdflatex main或进行编译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}

相关内容