[Tex/LaTex] Default value for basicstyle in lstlisting

fontslistings

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}

Best Answer

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}
Related Question