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

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.


% showexpl uses the listings package, so you need to check
% its documentation for that.
% Set up the basic parameters for showing the code
% set up the parameters for the examples themselves


\shade[left color=white, right color=red]
      (0,0) rectangle +(3,2);


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

More options


%define layout to tikz codes.

    basicstyle=\ttfamily\tiny, %
    identifierstyle=\color{colIdentifier}, %
    keywordstyle=\color{colKeys}, %
    stringstyle=\color{colString}, %
    commentstyle=\color{colComments}, %
    columns=flexible, %
    tabsize=4, %
    frame=single, %
    extendedchars=true, %
    showspaces=false, %
    showstringspaces=false, %
    numbers=none, %
    numberstyle=\tiny, %
    breaklines=true, %
    backgroundcolor=\color{hellgelb}, %
    breakautoindent=true, %


  \shade[ball color=yellow] (0,0) circle (1);
  \draw[->,blue] (0,0) -- (45:1);

\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


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.