Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
add comment

1 Answer

up vote 4 down vote accepted
\lstdefinestyle{styleA}{...}
\begin{lstlisting}[style=styleA]
...
\end{lststyle}

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

share|improve this answer
    
@Herbert, thanks for informing \lstdefinestyle{}. For electronic version I like using many colors. But for printed version, I will reduce the number of colors later. –  xport Jan 10 '11 at 10:49
    
@xsport: I were talking about electronic versions when I refer to less colors. –  Herbert Jan 10 '11 at 11:35
    
@Herbert: Can we use two or more defined styles at the same time such as \lstinputlisting[style={styleA,styleB}]{} ? –  xport Jan 10 '11 at 13:52
1  
@xport: no, not possible –  Herbert Jan 10 '11 at 14:18
1  
@xport: sure, it is the same as [tabsize=4cm,tabsize=2cm], the values are overwritten –  Herbert Jan 10 '11 at 16:12
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.