I use the listings
package to typeset code in my document. I have defined a new environment via \lstnewenvironment
in which I use \lstset
to set a large number of options through a list of key-values. It looks like the following:
\lstnewenvironment{mycodebox}[1][]
{
\lstset{
key1=val1,
key2=val2,
% some more options are set
#1 % so that it can accept more options
}
}{}
I use this new environment mostly in two variants where in each variant I need to override some existing (or add new) options. For example:
% variant 1
\begin{mycodebox}[newkey=newval, key1=newval]
% my code
\end{mycodebox}
and
% variant 2
\begin{mycodebox}[anotherkey=newval, key2=newval]
% my code
\end{mycodebox}
The two variants share a lot of options but they are different. Can I define two macros/commands like \variant1settings
and \variant2settigns
that can be used in some way like the following?
\begin{mycodebox}[\variant1settings]
% my code
\end{mycodebox}
I know that I can use \lstset{options...}
to do this before using the environment each time but
- I don't want the options to run through when I don't specify them before subsequent instances;
- I want the options to be in the form of a macro (or similar) so that they can be tweaked from one central position.
\documentclass{...}
and ending with\end{document}
.\newcommand\variantA{\lstset{...}}
and{\variantA\begin{mycodebox}...\end{mycodebox}}
in the text, but it is slightly less convenient than the syntax you asked for.