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
add comment

2 Answers 2

up vote 3 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
add comment

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
add comment

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.