10

I have most of the code in Python, which renders very nice with this simple configuration of lstlisting

At a certain point I have to include an XML file, so I switched to language=XML but it renders ugly with the default charset so I added also basicstyle=\ttfamily. Now I don't know how to switch back to default style after that.

\documentclass{article}
\usepackage{listings}
\lstset{tabsize=2, language=Python, breaklines=true, 
     breakatwhitespace=true,  xleftmargin=.25in}
\begin{document}

%some python listings here

%XML listing
\lstset{language=XML, basicstyle=\ttfamily}
\begin{lstlisting}
    <XML></XML>
\end{lstlisting}

%what do I put here?

%python listings again
\begin{lstlisting}
    for i in python:
        pass
\end{lstlisting}
\end{document}
unziberla
  • 277
  • Your second \lstset[language=XML] setting has overridden the previous one \lstset[language=Python] so you should invoke \lstset[language=Python] again before your last Python code. – kiss my armpit Mar 28 '13 at 08:05
  • yes but that is ok because I have python code before that XML. problem is to get back to previous settings after the XML – unziberla Mar 28 '13 at 08:06
  • It is better to use the optional argument of the lstlistings environment to switch between language. For example: \begin{lstlistings}[language=Python]. – kiss my armpit Mar 28 '13 at 08:09
  • Please see my answer here: http://tex.stackexchange.com/a/63962/19356 – kiss my armpit Mar 28 '13 at 08:12
  • it worked. thanks for the resource too, I was wondering how to do something like that but maybe I won't need it because I just have to switch language a couple of times. – unziberla Mar 28 '13 at 08:15
  • Make your own environment for each language. It should be regarded as the best practice. :-) Read the link given above. – kiss my armpit Mar 28 '13 at 08:17
  • Also, if this is the only XML code in your file, you can also use \begin{lstlisting}[language=XML, basicstyle=\ttfamily] <XML></XML> \end{lstlisting} – hpesoj626 Mar 28 '13 at 10:48
  • @Bugbusters Could you please consolidate your comments into an answer on this one? – kan May 05 '13 at 08:51
  • @mozartstraße: An answer please. – Marco Daniel Jul 07 '13 at 10:40

1 Answers1

11

enter image description here

\documentclass[preview,border=12pt]{standalone}% change it to your own document class

% begin
% this part is used to allow your readers to copy the code from a PDF viewer but without copying the line numbers.
\usepackage{accsupp}
\newcommand*{\noaccsupp}[1]{\BeginAccSupp{ActualText={}}#1\EndAccSupp{}}
% end

\usepackage{xcolor}
\usepackage{listings}

\lstdefinestyle{shared}
{
    numbers=left,
    numbersep=1em,
    numberstyle=\tiny\color{red}\noaccsupp,
    frame=single,
    framesep=\fboxsep,
    framerule=\fboxrule,
    rulecolor=\color{red},
    xleftmargin=\dimexpr\fboxsep+\fboxrule\relax,
    xrightmargin=\dimexpr\fboxsep+\fboxrule\relax,
    breaklines=true,
    tabsize=2,
    columns=flexible,
}


\lstdefinestyle{xml}
{
    style=shared,
    language={XML},
    %alsolanguage={PSTricks},
    basicstyle=\small\tt,
    keywordstyle=\color{blue},
    commentstyle=\color[rgb]{0.13,0.54,0.13},
    backgroundcolor=\color{yellow!10},
    morekeywords={
        graphicspath,
        includegraphics,
        blinddocument,
    },
}

\lstdefinestyle{python}
{
    style=shared,
    language={Python},
    %alsolanguage={[Sharp]C},
    basicstyle=\small\tt,
    keywordstyle=\color{blue},
    commentstyle=\color[rgb]{0.13,0.54,0.13},
    backgroundcolor=\color{cyan!10},
    morekeywords={
        Console,
        WriteLine,
        int,
    },
}

\lstnewenvironment{xml}
{\lstset{style=xml}}
{}

\lstnewenvironment{python}
{\lstset{style=python}}
{}

\begin{document}

\begin{xml}
    <XML></XML>
\end{xml}

\begin{python}
    for i in python:
        pass
\end{python}

\end{document}