I'm working on a custom lstlisting
environment, in which I use TikZ to add a frame and a title. Because of the widespread curly braces of node
I cannot use newenvironment
, so I have to resort to NewEnviron
.
From the MWE and the picture below it should be clear what I'm trying to accomplish:
\documentclass[a4paper,11pt]{article}
\usepackage{listings}
\lstset{breaklines=true, postbreak={\mbox{$\hookrightarrow\space$}}, language=Mathematica}
\usepackage{tikz}
\usetikzlibrary{shapes,decorations}
\tikzstyle{boxround} = [draw=black, thick, rectangle, rounded corners, inner sep=10pt, inner ysep=20pt]
\tikzstyle{head} =[draw=black, fill=white, thick, rectangle]
\usepackage{environ}
\NewEnviron{code}{%
\bigskip
\noindent\begin{tikzpicture}
\node [boxround] (box){%
\begin{minipage}{.94\textwidth}
\BODY
\end{minipage}};
\node[head, right=10pt] at (box.north west) {\textbf{Code listing}};
\end{tikzpicture}
\bigskip
}
\begin{document}
\begin{code}
How to get some \textsc{Mathematica} code (shown below) in this box...
\end{code}
\footnotesize
\begin{lstlisting}
B11[x_, y_] = Piecewise[{{1, 0 <= x <= 1 && 0 <= y <= 1}}];
Plot3D[B11[x, y], {x, -.5, 1.5}, {y, -.5, 1.5}, PlotRange -> Full];
\end{lstlisting}
\normalsize
\end{document}
Which results in
If it weren't for tikzpicture
, I would just use lstnewenvironment
. But considering the widespread curly braces of node
I don't know how I could. Any ideas?
Best Answer
Here a suggestion with
mdframed
:Update
In the example below you have a new environment named
code
which has one optional and one mandatory argument. The optional argument is passed to\lstset
and the mandatory one is the title.