How to define several listing environments

The following example shows how to define several listing environments with different highlightings and style settings.

\documentclass{scrartcl}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{bera}
 
\definecolor{hellgelb}{rgb}{1,1,0.8}
\definecolor{colKeys}{rgb}{0,0,1}
\definecolor{colIdentifier}{rgb}{0,0,0}
\definecolor{colComments}{rgb}{1,0,0}
\definecolor{colString}{rgb}{0,0.5,0}
 
\lstset{%
    basicstyle=\ttfamily\small%
}
 
% http://tex.stackexchange.com/questions/159419/different-colors-for-default-and-language-specific-listings
 
\lstdefinestyle{mytexstyle}{
    float=hbp,%
    identifierstyle=\color{colIdentifier}, %
    keywordstyle=\color{colKeys}, %
    stringstyle=\color{colString}, %
    commentstyle=\color{colComments}, %
    columns=flexible, %
    tabsize=2, %
 %   frame=single, %
    extendedchars=true, %
    showspaces=false, %
    showstringspaces=false, %
    backgroundcolor=\color{hellgelb}, %
    breakautoindent=true, %
    captionpos=b%
}
 
\lstdefinestyle{mypythonstyle}{
    float=hbp,%
    identifierstyle=\color{colIdentifier}, %
    keywordstyle=\color{colKeys}, %
    stringstyle=\color{colString}, %
    commentstyle=\color{colComments}, %
    columns=flexible, %
    tabsize=2, %
  %  frame=single, %
    extendedchars=true, %
    showspaces=false, %
    showstringspaces=false, %
    backgroundcolor=\color[gray]{0.95},
    breakautoindent=true, %
    captionpos=b%
 }
 
 
 
% http://stackoverflow.com/questions/741985/latex-source-code-listing-like-in-professional-books?rq=1
\usepackage{caption}
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox{gray}{\parbox{\textwidth}{#1#2#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white}
 
 
 
% http://www.golatex.de/caption-fuer-lstnewenvironment-t9200.html
 
\lstnewenvironment{mytex}[2]
  {\lstset{language={[LaTeX]TeX},style=mytexstyle,caption={#1},label={#2}}}
  {}
 
 \lstnewenvironment{mypython}[2]
  {\lstset{language={Python},style=mypythonstyle,caption={#1},label={#2}}}
  {} 
 
\begin{document}
 
Here's some \LaTeX\ source code.
 
\begin{mytex}{Some \LaTeX\ code}{latex}
%!TEX TS-program = Arara
% arara: pdflatex
\documentclass[12pt,ngerman]{scrartcl}
 
\begin{document}
Hello \LaTeX!
 
\end{document}
\end{mytex}
 
Here's some Python source code.
 
\begin{mypython}{Some Python source to calculate the Fibonacchi number for a given number. This caption exceeds one line.}{py1}
def calcFibo(n):
	if n==0:
		return 0
	if n==1:
		return 1
	else:
		return calcFibo(n-1)+calcFibo(n-2)
 
print(calcFibo(25))
\end{mypython}
 
 
See listing \ref{latex} on page \pageref{latex}
 
\end{document}

listi

Uwe

Uwe Ziegenhagen mag LaTeX und Python, auch gern in Kombination. Hat Dir dieser Beitrag geholfen und möchtest Du Dich dafür bedanken? Dann unterstütze doch vielleicht die Dingfabrik Köln e.V. mit einem kleinen Beitrag. Details zur Bezahlung findest Du unter Spenden für die Dingfabrik.

More Posts - Website