# How to make a box containing LaTeX output?

I am writing a short introductory book on LaTeX and I would like to implement nice looking code example boxes. Using this answer: Source code listing with frame around code? I made the boxes with the listings packages by setting in the preamble:

\usepackage{listings}
\usepackage{xcolor}
\usepackage{caption}
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{%
\parbox{\textwidth}{\colorbox{gray}{\parbox{\textwidth}{#1#2#3}}\vskip-4pt}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white}
\lstset{frame=lrb,xleftmargin=\fboxsep,xrightmargin=-\fboxsep,language=[LaTeX]{TeX},columns=flexible}
\renewcommand{\lstlistingname}{Example}


and in the text for example:

\begin{lstlisting}[label=firstlook,caption=A First Look]
\documentclass{article}
\begin{document}
Hello World!
\end{document}
\end{lstlisting}


Which results in:

And looks nice. I would like to make something similar for the output, and was wondering how I could do that (by manually writing the output I mean, not automatically taking what I enter there and evaluate it). Just the same looking box would be cool, having this box split in the middle and show an "output panel" or something would be even cooler. Does anyone know how I can do such a thing?

• something like the examplep package? Jun 18, 2011 at 15:22
• plus one for @Spike: it is very nice "Special care is taken so section, page and footnote numbers do not interfere with the main document." Jun 18, 2011 at 16:41
• The bclogo package is also a good alternative. Jun 18, 2011 at 18:25
• examplep looks like exactly what I am looking for. Can you make it an answer so I can accept it? Jun 19, 2011 at 10:19

I think the good package is showexpl .This package works with listings.

I work actually on the tkzexample package. The last version is on ctan but I try to finish the doc. You can present the source code and the result but it's not possible to highlight the syntax.

 \documentclass[]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{tikz,tkzexample}

\usetikzlibrary{shapes,arrows}
\colorlet{graphicbackground}{red!10!white}%
\colorlet{codebackground}{blue!10!white}%
\colorlet{codeonlybackground}{gray!20}
\begin{document}
\parindent=0pt

\begin{tkzexample}[small,width=8cm,overhang,frame tex=red,frame code=blue]
\begin{minipage}{6cm}
\section{Minipage et  èçéà\&§}
Ceci est un test pour "minipage"
\end{minipage}
\end{tkzexample}

\hspace{24pt}
\begin{tkzexample}[small,width=3cm,frame tex=red,frame code=blue]
\begin{tikzpicture}
\draw (0,0)  node[circle,
ball color=Peach,minimum size=2cm]{};
\end{tikzpicture}
\end{tkzexample}

\hspace{24pt}
\begin{tkzltxexample}[line frame width=2pt]
\begin{tkzexample}[width=4cm,frame tex=red,frame code=blue]
\tikz[baseline]
\node [circle,line width=1ex,draw=blue,fill=blue]
{\textcolor{white}{\Large{TikZ}}};
\end{tkzexample}
\end{tkzltxexample}

\hspace{24pt}
\begin{tkzexample}[width=4cm,frame tex=red,frame code=blue]
\tikz[baseline]
\node [circle,line width=1ex,draw=blue,fill=blue]
{\textcolor{white}{\Large{TikZ}}};
\end{tkzexample}

\hspace{24pt}
\begin{tkzexample}[width=3cm,frame tex=red,frame code=blue]
$x \mathbin{\tikz[baseline] \draw[|->,>=triangle 45]% (0pt,.5ex) -- (8ex,.5ex);} f(x)$
\end{tkzexample}

\end{document}


• Would you mind to remove your picture with the pstricks-»example«? I seems not decent to me. Jun 18, 2011 at 20:36
• @Keks No problem but it was only to say that I never use pstricks :) but I can add Vi, emacs, listings and a lot of things! Jun 18, 2011 at 21:55
• Looks cool, I'll surely look into this too:) Jun 19, 2011 at 10:19
• On CTAN this is ctan.org/pkg/tkz-doc. May 5, 2020 at 21:24

I know it's an old question, but in case somebody arrives here, it's worth to mention tcolorbox which original goal was:

to have accentuated and colored boxes to display source code and compiled text in combination.

Next there are some little examples:

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage{listings}
\usepackage[most]{tcolorbox}
\usepackage{lipsum}

\begin{document}

\begin{tcblisting}{title=A simple example}
Hello World, this is \LaTeX
\end{tcblisting}

\begin{tcblisting}{title=Side by side, listing outside text}
Hello World, this is \LaTeX
\end{tcblisting}

\begin{tcblisting}{title=Side by side, text side listing}
Hello World, this is \LaTeX
\end{tcblisting}
\end{document}


Some more complex examples:

Code examples, like in the TikZ/PGF manual

How to automatically arrange an unknown number of images?

tcolorbox listing error using beamers begin{frame}