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 like very much the 'side-by-side' code examples in the TikZ/PGF manual...

enter image description here

... and I would like to use the same thing in a manual I'm writing. I have found the source code for the codeexample environment that the TikZ/PGF manual uses for this purpose (specifically, it's located at /usr/local/texlive/2012/texmf-dist/doc/generic/pgf/macros/pgfmanual-en-macros.tex).

My (probably rather basic) question is: how can I use the same codeexample environment in my own documents? I ask because I tried compiling the example in the related question below, but the file couldn't be found.

share|improve this question
1  
Do you need all the bells and whistles of codeexample like highlighted text and background color, or would a verbatim solution suffice? –  Steven B. Segletes Jun 24 '13 at 15:41
3  
You can refer to Is there a listings configuration for TikZ as used in PGF/TikZ manual? where Christian explains exactly how to use the side-by-side environments, but there's also tkzexample. –  Claudio Fiandrino Jun 24 '13 at 16:02
    
@ClaudioFiandrino Thanks, I think tkzexample is exactly what I want. (I should have looked more thoroughly before asking my question!) –  John Wickerson Jun 24 '13 at 16:09
    
@JohnWickerson: you're welcome :) –  Claudio Fiandrino Jun 24 '13 at 18:18
add comment

1 Answer

Here, I use tcolorbox to resemble the visual appearance of the TikZ examples. The mandatory parameter of my example environment sidebyside is the width of the picture.

Update: The first two examples use an automated invisible tikzpicture environment to display only a code snippet. The third example uses and displays the tikzpicture environment deliberately.

\documentclass{article}
\usepackage[most]{tcolorbox}
\pgfdeclarelayer{background}
\pgfsetlayers{background,main}

\lstdefinestyle{example}{style=tcblatex,
  classoffset=0,
  texcsstyle=*\color{blue},%
  deletetexcs={begin,end},
  moretexcs={,%
    pgfdeclarehorizontalshading,pgfuseshading,node,
    useasboundingbox,draw}%
  classoffset=1,
  keywordstyle=\color{blue},%
  morekeywords={tikzpicture,shade,fill,draw,path,node,child,line,width,rectangle},
  classoffset=0}

\tcbset{%
  fillbackground/.style={before lower pre={%
  \tikzset{every picture/.style={execute at end picture={\begin{pgfonlayer}{background}
    \fill[yellow!25!white]
    ([xshift=-1mm,yshift=-1mm]current bounding box.south west) rectangle
    ([xshift=1mm,yshift=1mm]current bounding box.north east);
    \end{pgfonlayer}}}}}},
  explicitpicture/.style={before lower=\begin{center},after lower=\end{center},fillbackground}}

\newtcblisting{sidebyside}[2][]{%
  enhanced,frame hidden,
  top=0pt,bottom=0pt,left=0pt,right=0pt,arc=0pt,boxrule=0pt,
  colback=blue!25!white,
  listing style=example,
  sidebyside,text and listing,text outside listing,sidebyside gap=2mm,
  lefthand width=#2,tikz lower,fillbackground,
  #1}

\begin{document}

\begin{sidebyside}[before lower app={\path[] (-2,-0.5) rectangle (2,0.5);}]{4.2cm}
\pgfdeclarehorizontalshading{myshadingA}
  {1cm}{rgb(0cm)=(1,0,0); color(2cm)=(green); color(4cm)=(blue)}
\pgfuseshading{myshadingA}
\end{sidebyside}

\begin{sidebyside}{4.2cm}
\node {root}
  child {node {left}}
  child {node {right}
    child {node {child}}
    child {node {child}}
};
\end{sidebyside}

\begin{sidebyside}[explicitpicture]{4.2cm}
\begin{tikzpicture}[line width=20pt]
  \useasboundingbox (0,-1.5) rectangle (3.5,1.5);
  \draw[red] (0,0) -- (3,0);
  \draw[gray,->] (0,0) -- (3,0);
\end{tikzpicture}
\end{sidebyside}

\end{document}

enter image description here

share|improve this answer
    
Nice! Is it possible to also highlight the backslash symbol as well? –  Kevin C Nov 18 '13 at 18:58
1  
@KevinC Yes. Here, the listings package does the formatting job in the background. The settings are made by \lstdefinestyle in my example code. I edited the example to add the backslash highlighting. I also changed the third example to display an explicit tikzpicture environment as in the TikZ manual. –  Thomas F. Sturm Nov 19 '13 at 12:37
    
Excellent! Thank you! –  Kevin C Nov 19 '13 at 15:24
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.