1

Screenshot

alt text


Scenario

I want to define two sets of settings for \lstinputlisting--One setting for CSharp A and the other one for CSharp B. They are different only in the color settings.

\def\CSharpSettingsA%
{%
 \lstset%
 {%
  language={[Sharp]C},
  literate={ï}{}0{»}{}0{¿}{}0,
  backgroundcolor=\color{White},
   basicstyle=\scriptsize\color{Black}\ttfamily,
   keywordstyle=\color{Orange},
   identifierstyle=\color{Cyan},
   stringstyle=\color{Red}, 
   commentstyle=\color{Green}% 
 }
}

\def\CSharpSettingsB%
{%
 \lstset%
 {%
  language={[Sharp]C},
  literate={ï}{}0{»}{}0{¿}{}0,
  backgroundcolor=\color{Black},
   basicstyle=\scriptsize\color{White}\ttfamily,
   keywordstyle=\color{Orange},
   identifierstyle=\color{Cyan},
   stringstyle=\color{Red}, 
   commentstyle=\color{Green}% 
 }
}

There is a common setting as well.

\lstset%
{%
 breaklines=true,
 tabsize=2,  
 showstringspaces=false,
 aboveskip=0pt,
 belowskip=0pt%
}

NOTE: because each set contains many key-value pairs, it is not good idea to pass those key-value pairs directly to the optional argument of \lstinputlisting.

In the preamble, I define two commands as follows:

\newcommand{\InputCSharpA}[1]{{\CSharpSettingsA\lstinputlisting{#1}}}
\newcommand{\InputCSharpB}[1]{{\CSharpSettingsB\lstinputlisting{#1}}}

And in the document body, I switch between them as follows:

\begin{document}
\InputCSharpA{CSharp/MyCode/Program.cs}

\InputCSharpB{CSharp/MyCode/Program.cs}
\end{document}

Question

Without defining a newcommand for each setting, is it possible to pass the set of settings to the optional argument of lstinputlisting ?

Such as \lstinputlisting[key=CSharpSettingsA]{} for example.


Complete Minimal Code

\documentclass[dvipsnames]{article}
\usepackage[a4paper,margin=20mm]{geometry}
\usepackage{xcolor}

\usepackage{listings}
\lstset%
{%
 breaklines=true,
 tabsize=2,  
 showstringspaces=false,
 aboveskip=0pt,
 belowskip=0pt%
}

\def\CSharpSettingsA%
{%
 \lstset%
 {%
  language={[Sharp]C},
  literate={ï}{}0{»}{}0{¿}{}0,
  backgroundcolor=\color{White},
   basicstyle=\scriptsize\color{Black}\ttfamily,
   keywordstyle=\color{Orange},
   identifierstyle=\color{Cyan},
   stringstyle=\color{Red}, 
   commentstyle=\color{Green}% 
 }
}

\def\CSharpSettingsB%
{%
 \lstset%
 {%
  language={[Sharp]C},
  literate={ï}{}0{»}{}0{¿}{}0,
  backgroundcolor=\color{Black},
   basicstyle=\scriptsize\color{White}\ttfamily,
   keywordstyle=\color{Orange},
   identifierstyle=\color{Cyan},
   stringstyle=\color{Red}, 
   commentstyle=\color{Green}% 
 }
}

\newcommand{\InputCSharpA}[1]{{\CSharpSettingsA\lstinputlisting{#1}}}
\newcommand{\InputCSharpB}[1]{{\CSharpSettingsB\lstinputlisting{#1}}}

\begin{document}
\InputCSharpA{CSharp/MyCode/Program.cs}

\InputCSharpB{CSharp/MyCode/Program.cs}
\end{document}
Display Name
  • 46,933

1 Answers1

5
\lstdefinestyle{styleA}{...}
\begin{lstlisting}[style=styleA]
...
\end{lststyle}

and by the way: if you will make your listings more readable, use less colors ...