Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question

3 Answers 3

up vote 4 down vote accepted

Use the showexpl package. It uses listings underlyingly, 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,
    commentstyle=\itshape\ttfamily\small,
    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}
\shade[left color=white, right color=red]
      (0,0) rectangle +(3,2);
\end{tikzpicture}

\end{LTXexample}
\end{document}

Output of this document:

Sample code and tikz image

share|improve this answer
    
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}

enter image description here

share|improve this answer

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{colComments}{rgb}{1,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}, %
    commentstyle=\color{colComments}, %
    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}
  \shade[ball color=yellow] (0,0) circle (1);
  \draw[->,blue] (0,0) -- (45:1);
 \end{tikzpicture}
\end{LTXexample}

\end{document} ​
share|improve this answer
    
Thanks for the example. However, it seems that xleftmargin or xrightmargin for verbatim no longer has any effect when used in LTXexample. (they still work in the lstlisting environment.) –  ltxsun Jan 15 '11 at 4:43
    
After playing around with the test file from showexpl, I found that when numbers=none, xleftmargin does not have any effect on verbatim. It only works when numbers=left or right. probably a bug? –  ltxsun Jan 15 '11 at 6:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.