This is what I want to do:
\documentclass{beamer}
\begin{document}
\begin{frame}[fragile]{Frame 1}
Some \structure{Python} code here:
% I want to define a new "pythoncode" environment, so I can use it like this:
\begin{pythoncode}[hello.py]
#!/usr/bin/env python
def main():
print "Hello, World!"
if __name__ == '__main__':
main()
\end{pythoncode}
\end{frame}
\end{document}
But I want to customize the source-code box beyond what listings
package allow me. Jan Hlavacek suggested me to use tikz
package for that, and pointed me to a few examples.
Okay, that's great! Tikz gives me enough power and freedom for customization! All I need now is creating a new environment, so that I can use this environment just like in the code above.
However, I'm having trouble mixing tikz
with listings
inside a new environment. I've tried the following code, but it doesn't work (I get Missing \endcsname inserted.):
\usepackage{listings}
\usepackage{tikz}
\usepackage{environ}
\tikzstyle{sourcecodebox} = [
draw=blue, very thick,
rectangle, rounded corners,
inner sep=10pt
]
\tikzstyle{sourcecodetitle} = [
fill=black, text=white,
rectangle, rounded corners
]
\NewEnviron{pythoncode}[1]{
\begin{tikzpicture}
\node[sourcecodebox](box){
\begin{lstlisting}[
language=Python,
basicstyle=\ttfamily\footnotesize,
escapeinside={(*@}{@*)},
%numbers=left,
breaklines=true,
breakatwhitespace=true,
showspaces=false,
showstringspaces=false,
frame=shadowbox,
frameround=rrrt,
rulecolor=\color{black},
rulesepcolor=\color{gray}
]
\BODY
\end{lstlisting}
};
\node[sourcecodetitle] at (box.north west) {#1};
\end{tikzpicture}
}
Related questions:
- Defining a new environment whose contents go in a TikZ node - It suggests using the
environ
package. - define a new environment to combine tikzpicture and lstlisting - The question title looks similar, but in fact the question is very different. That question is solved using
showexpl
package.
hello.py
file with the code, and then use\lstinputlisting{hello.py}
. You avoid the problem altogether because you don't need to define an environment anymore. – Juan A. Navarro Jan 20 '11 at 14:52\lstnewenvironment
– Yiannis Lazarides Jan 20 '11 at 15:16\lstnewenvironment
, but I failed completely, because I needed to open\begin{tikzpicture}
inside the first pair of braces and then close it inside another set of braces. Since that is not possible, another question from this site suggested using theenviron
package. Anyway, if you have a working solution, please share with us! – Denilson Sá Maia Jan 21 '11 at 2:08