# Define a new environment to combine tikzpicture and lstlisting

I would like to define a new environment to combine the tikzpicture environment from package pgf/tikz and the lstlisting environment from package listings. The result would be to first produce the picture, then, include verbatim code afterwards. It would be similar to the examples in the PGF manual. Till Tantau uses an environment codeexample, but it is too complicated for me to comprehend, and I don't need other functionalities such as extracting keywords etc.

-

Use the showexpl package. It uses listings "under the hood", but also compiles your code example for you.

\documentclass{article}
\usepackage{showexpl}
\usepackage{tikz}

% showexpl uses the listings package, so you need to check
% its documentation for that.
% Set up the basic parameters for showing the code
\lstset{%
basicstyle=\ttfamily\scriptsize,
showspaces=false,
showstringspaces=false,
breaklines=true,
breakautoindent=true,
captionpos=t
}
% set up the parameters for the examples themselves
\lstset{explpreset={rframe={},xleftmargin=1em,columns=flexible,language={}}}

\begin{document}
\begin{LTXexample}[pos=b]

\begin{tikzpicture}
(0,0) rectangle +(3,2);
\end{tikzpicture}

\end{LTXexample}
\end{document}


Output of this document:

-
Thanks! It looks like exactly what I want. I will read the manual. – ltxsun Jan 15 '11 at 1:37

This can be easily done with tcolorbox package help. Like showexpl it's able to print some LaTeX code and show its result at the same time.

Next code defines a tikzexample environment which will show TiKZ code on the left and the figure on the right. Option tikz lower saves us from typing \begin{tikzpicure} and \end{tikzpicture}.

listing side text is used as default appearance (first example) which can be changed through the optional parameter as is shown in second example.

The code for the figure has been copied form tcolorbox documentation.

\documentclass{article}

\usepackage[most]{tcolorbox}

\newtcblisting{tikzexample}[1][]{tikz lower,
listing side text, fonttitle=\bfseries,
bicolor, colback=blue!15!white, colbacklower=white,
colframe=black, righthand width=3cm, #1}

\begin{document}

\begin{tikzexample}[title=Example]
\path[fill=yellow,draw=yellow!75!red] (0,0) circle (1cm);
\fill[red] (45:5mm) circle (1mm);
\fill[red] (135:5mm) circle (1mm);
\draw[line width=1mm,red] (215:5mm) arc (215:325:5mm);
\end{tikzexample}

\begin{tikzexample}[title=Example, listing outside text]
\path[fill=yellow,draw=yellow!75!red] (0,0) circle (1cm);
\fill[red] (45:5mm) circle (1mm);
\fill[red] (135:5mm) circle (1mm);
\draw[line width=1mm,red] (215:5mm) arc (215:325:5mm);
\end{tikzexample}

\end{document}


-

More options

\documentclass{article}
\usepackage{showexpl}
\usepackage{tikz}

%define layout to tikz codes.
\definecolor{hellgelb}{rgb}{1,1,0.8}
\definecolor{colKeys}{rgb}{0,0,1}
\definecolor{colIdentifier}{rgb}{0,0,0}
\definecolor{colString}{rgb}{0,0.5,0}

\usepackage{listings}
\lstset{%
language={},%
float=hbp,%
basicstyle=\ttfamily\tiny, %
identifierstyle=\color{colIdentifier}, %
keywordstyle=\color{colKeys}, %
stringstyle=\color{colString}, %
columns=flexible, %
tabsize=4, %
frame=single, %
rframe={},%
extendedchars=true, %
showspaces=false, %
showstringspaces=false, %
numbers=none, %
numberstyle=\tiny, %
breaklines=true, %
backgroundcolor=\color{hellgelb}, %
breakautoindent=true, %
captionpos=b,%
xleftmargin=0pt%
}

\begin{document}

\begin{LTXexample}[width=2cm]
\begin{tikzpicture}