Note that \NewEnviron
does not support verbatim. The documentation should point that out. As already commented the \lstnewenvironment
must be used for this. Alternatively place the verbatim files into external files which are then read using \listinputlisting
.
Find below my solution which stores the resulting listings first into a box which is then used inside the TikZ node. IMHO you should not mix the listings
frame
argument with additional frames from TikZ. You should produce all framing using TikZ (not to be confused with beamers \frame
macro/environment).
Beamer provides some nice framed boxes as well!
I had to add the linewidth
argument and a \makebox
, otherwise the listings frame is to wide. You might want to adjust that.
Best Regards,
Martin
\documentclass{beamer}
\usepackage{listings}
\usepackage{tikz}
\tikzstyle{sourcecodebox} = [
draw=blue, very thick,
rectangle, rounded corners,
inner sep=10pt
]
\tikzstyle{sourcecodetitle} = [
fill=black, text=white,
rectangle, rounded corners
]
\makeatletter
\lstnewenvironment{pythoncode}[1][]{%
\def\pythoncodetitle{#1}%
\lstset{%
language=Python,
basicstyle=\ttfamily\footnotesize,
escapeinside={(*@}{@*)},
%numbers=left,
breaklines=true,
breakatwhitespace=true,
showspaces=false,
showstringspaces=false,
frame=shadowbox,
frameround=rrrt,
linewidth=.75\linewidth,
rulecolor=\color{black},
rulesepcolor=\color{gray}
}%
\setbox\@tempboxa=\hbox\bgroup\color@setgroup
}%
{%
\color@endgroup\egroup
\begin{tikzpicture}
\node[sourcecodebox] (box)
% Makebox is needed to take the frame added by listings into account
{\makebox[.75\linewidth][l]{\box\@tempboxa}};
\node[sourcecodetitle] at (box.north west) {\pythoncodetitle};
\end{tikzpicture}
}
\begin{document}
\begin{frame}[fragile]{Frame 1}
Some \structure{Python} code here:
\begin{pythoncode}[hello.py]
#!/usr/bin/env python
def main():
print "Hello, World!"
if __name__ == '__main__':
main()
\end{pythoncode}
\end{frame}
\end{document}
Here, I use tcolorbox
to resemble the visual appearance of the TikZ examples. The mandatory parameter of my example environment sidebyside
is the width of the picture.
Update:
The first two examples use an automated invisible tikzpicture
environment to display only a code snippet. The third example uses and displays the tikzpicture
environment deliberately.
\documentclass{article}
\usepackage[most]{tcolorbox}
\pgfdeclarelayer{background}
\pgfsetlayers{background,main}
\lstdefinestyle{example}{style=tcblatex,
classoffset=0,
texcsstyle=*\color{blue},%
deletetexcs={begin,end},
moretexcs={,%
pgfdeclarehorizontalshading,pgfuseshading,node,
useasboundingbox,draw}%
classoffset=1,
keywordstyle=\color{blue},%
morekeywords={tikzpicture,shade,fill,draw,path,node,child,line,width,rectangle},
classoffset=0}
\tcbset{%
fillbackground/.style={before lower pre={%
\tikzset{every picture/.style={execute at end picture={\begin{pgfonlayer}{background}
\fill[yellow!25!white]
([xshift=-1mm,yshift=-1mm]current bounding box.south west) rectangle
([xshift=1mm,yshift=1mm]current bounding box.north east);
\end{pgfonlayer}}}}}},
explicitpicture/.style={before lower=\begin{center},after lower=\end{center},fillbackground}}
\newtcblisting{sidebyside}[2][]{%
enhanced,frame hidden,
top=0pt,bottom=0pt,left=0pt,right=0pt,arc=0pt,boxrule=0pt,
colback=blue!25!white,
listing style=example,
sidebyside,text and listing,text outside listing,sidebyside gap=2mm,
lefthand width=#2,tikz lower,fillbackground,
#1}
\begin{document}
\begin{sidebyside}[before lower app={\path[] (-2,-0.5) rectangle (2,0.5);}]{4.2cm}
\pgfdeclarehorizontalshading{myshadingA}
{1cm}{rgb(0cm)=(1,0,0); color(2cm)=(green); color(4cm)=(blue)}
\pgfuseshading{myshadingA}
\end{sidebyside}
\begin{sidebyside}{4.2cm}
\node {root}
child {node {left}}
child {node {right}
child {node {child}}
child {node {child}}
};
\end{sidebyside}
\begin{sidebyside}[explicitpicture]{4.2cm}
\begin{tikzpicture}[line width=20pt]
\useasboundingbox (0,-1.5) rectangle (3.5,1.5);
\draw[red] (0,0) -- (3,0);
\draw[gray,->] (0,0) -- (3,0);
\end{tikzpicture}
\end{sidebyside}
\end{document}
Best Answer
Use the
showexpl
package. It useslistings
"under the hood", but also compiles your code example for you.Output of this document: