I defined several new code environments (using listings
' lstnewenvironment
); they all share the same attributes except one (the language used). For example:
\documentclass[a4paper,12pt]{book}
\usepackage{listings}
\usepackage[usenames,dvipsnames]{xcolor}
\lstnewenvironment{xml}
{
\lstset
{
literate=
{é}{{\'e}}1
{É}{{\'E}}1
{à}{{\`a}}1 {è}{{\`e}}1 {ì}{{\`i}}1 {ò}{{\`o}}1 {ù}{{\`u}}1 {î}{{\^i}}1
{À}{{\`A}}1 {È}{{\'E}}1 {Ì}{{\`I}}1 {Ò}{{\`O}}1 {Ù}{{\`U}}1 {Î}{{\^I}}1,
language=XML,
breaklines=true,
xleftmargin=\parindent,
showstringspaces=false,
showspaces=false,
showtabs=false,
keepspaces=true,
tabsize=2,
captionpos=t,
numbers=left,
numbersep=5pt,
numberstyle=\tiny\color{graycode},
escapeinside={(*}{*)},
basicstyle=\footnotesize\ttfamily,
keywordstyle=\bfseries\color{green!40!black},
commentstyle=\itshape\color{purple!40!black},
identifierstyle=\color{blue},
stringstyle=\color{orange}
}
}
{}
\begin{document}
\begin{xml}
<something> ... </something>
\end{xml}
\end{document}
I'd like to refactor my code in a way that I keep the common stuff in some sort of macro that dumps as-is (verbatim) whatever stored in the point where it's called. Something like:
\newcommand{\stringlstset}
{
literate = ...
breaklines = ...
...
}
\lstnewenvironment{xml}
{
\lstset
{
\stringlstset
language = xml
...
}
}
\lstnewenvironment{prolog}
{
\lstset
{
\stringlstset
language = prolog
...
}
}
I gather you have to use some low-level gimmick to get this done. I did some googling around but I couldn't come up with a working solution. ConTeXt's buffers, for example, looked promising, but they can't be used here.
There may be something in listings
to deal with this, but I'm interested in a general solution here.
\documentclass
and the appropriate packages that sets up the problem. While solving problems can be fun, setting them up is not. Then, those trying to help can simply cut and paste your MWE and get started on solving the problem. – Peter Grill Oct 18 '14 at 17:55\lstset{\stringlstset,language=prolog,…}
if you subtitute the first part with\expandafter\lstset\expandafter{\stringlstset,…
it should work. – Manuel Oct 18 '14 at 21:05