When documenting a style file, it would be nice to be able to easily put side by side different examples of code, side-by-side with the output they produce.
What recommendations to people have to do this efficiently, and æsthetically?
When documenting a style file, it would be nice to be able to easily put side by side different examples of code, side-by-side with the output they produce.
What recommendations to people have to do this efficiently, and æsthetically?
My package tcolorbox provides another possible answer for the question. It allows to place the source code before or after the output. With version 2.20, source and output can be set side-by-side. The output can be placed inside a colored box or outside, before the source or after the source.
\documentclass{article}
\usepackage[skins,listings]{tcolorbox}
\newtcblisting{exampleA}[2][]{%
colframe=red!50!yellow!50!black,
colback=red!50!yellow!5!white,
coltitle=red!50!yellow!3!white,
bicolor,colbacklower=white,
fonttitle=\sffamily\bfseries,
sidebyside,
title=#2,#1}
\newtcblisting{exampleB}[2][]{%
colframe=red!50!yellow!50!black,
colback=white,
coltitle=red!50!yellow!3!white,
bicolor,colbacklower=red!50!yellow!5!white,
fonttitle=\sffamily\bfseries,
sidebyside,text and listing,
title=#2,#1}
\begin{document}
\begin{exampleA}[righthand width=3.5cm,center lower]{Side-by-side (inside box)}
\begin{tikzpicture}
\path[fill=yellow!50!white] (0,0) circle (11mm);
\path[fill=white] (0,0) circle (9mm);
\foreach \w/\c in {90/red,210/green,330/blue}
{\path[shading=ball,ball color=\c] (\w:1cm) circle (7mm);}
\end{tikzpicture}
\end{exampleA}
\begin{exampleA}[righthand width=3.5cm,listing outside text]{Side-by-side (outside box)}
\begin{tikzpicture}
\path[fill=yellow!50!white] (0,0) circle (11mm);
\path[fill=white] (0,0) circle (9mm);
\foreach \w/\c in {90/red,210/green,330/blue}
{\path[shading=ball,ball color=\c] (\w:1cm) circle (7mm);}
\end{tikzpicture}
\end{exampleA}
\begin{exampleB}[lefthand width=3.5cm]{}
\begin{tikzpicture}
\path[fill=yellow!50!white] (0,0) circle (11mm);
\path[fill=white] (0,0) circle (9mm);
\foreach \w/\c in {90/red,210/green,330/blue}
{\path[shading=ball,ball color=\c] (\w:1cm) circle (7mm);}
\end{tikzpicture}
\end{exampleB}
\begin{exampleB}[lefthand width=3.5cm,text outside listing,
colback=red!50!yellow!5!white,top=0mm,bottom=0mm,left=0mm,right=0mm,
arc=0mm,boxrule=1pt,watermark text=Source,
watermark color=yellow!75!red!30!white]{}
\begin{tikzpicture}
\path[fill=yellow!50!white] (0,0) circle (11mm);
\path[fill=white] (0,0) circle (9mm);
\foreach \w/\c in {90/red,210/green,330/blue}
{\path[shading=ball,ball color=\c] (\w:1cm) circle (7mm);}
\end{tikzpicture}
\end{exampleB}
\end{document}

title=#2,#1 stand for?. If I want to use only one style, how the command \newtcblisting{exampleA}[2][] needs to be modified?
– user149418
Aug 08 '19 at 18:17
title=#2,#1 stands for setting the title to the mandatory parameter #2 and adding further options from the optional parameter #1. If no title and no further options are needed, use \newtcblisting{exampleA}{colframe=...
– Thomas F. Sturm
Sep 10 '19 at 11:16
I made a package tkzexample that I use for all my documentation. I upload yesterday on ctan a new version (you need to wait several days before see it) but without documentation actually ( I have a little work to do to finish the doc. The main macro is based on the \codeexampleof T. Tantau. Till gave me permission to use and modify the macro.
\codeexample don't work with accents and utf8 but tkzexample accepts accents and utf8. I add the possibility to num the lines and I created an option to save the example on an extern file with \usepackage[saved]{tkzexample}.
\documentclass[]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{tikz}
\usepackage{amsmath,amssymb,booktabs}
\usepackage[frenchb]{babel}
\usepackage{tkzexample}
% possible \usepackage[saved]{tkzexample} permet de sauvegarder l'exemple
\usepackage[protrusion = true,
expansion, final,
verbose = false, babel = true]{microtype}
\DisableLigatures{encoding = T1, family = tt*}
\parindent=0pt
\begin{document}
\colorlet{graphicbackground}{blue!10!white}%
\colorlet{codebackground}{red!10}%
1) How to use
\begin{tkzltxexample}[]
\documentclass{scrartcl}
\usepackage{tkzexample}
\begin{document}
\begin{tkzexample}[]
\begin{tikzpicture}
\path coordinate (A) at (0,0)
coordinate (B) at (-60:12cm)
coordinate (C) at (240:12cm);
\foreach \density in {20,30,...,160}{%
\draw[fill=MidnightBlue!\density] (A)--(B)--(C)--cycle;
\path (A) coordinate (X) -- (B) coordinate[pos=.15](A)
-- (C) coordinate[pos=.15](B) -- (X) coordinate[pos=.15](C);}
\end{tikzpicture}
\end{tkzexample}
\end{document}
\end{tkzltxexample}
2) Usage classique
\begin{tkzexample}[latex=6cm]
\begin{tikzpicture}[scale=.5]
\path coordinate (A) at (0,0)
coordinate (B) at (-60:12cm)
coordinate (C) at (240:12cm);
\foreach \density in {20,30,...,160}{%
\draw[fill=MidnightBlue!\density]
(A)--(B)--(C)--cycle;
\path (A) coordinate (X)
-- (B) coordinate[pos=.15](A)
-- (C) coordinate[pos=.15](B)
-- (X) coordinate[pos=.15](C);
}
\end{tikzpicture}
\end{tkzexample}
3) Un tableau
\begin{tkzexample}[width=6cm,frame tex=Maroon,frame code=Maroon,num ]
\begin{tabular}{ll}
\toprule
Essai avec un tabeau &\\
\midrule
Un premier & texte\\
Un second & texte\\
\bottomrule
\end{tabular}
\end{tkzexample}
4) Les accents en français
\begin{tkzexample}[small,num]
\begin{tikzpicture}
\node[draw] {éè§çà};
\end{tikzpicture}
\end{tkzexample}
\end{document}

There's showexpl which has the LTXexample environment for exactly this purpose. It's based on listings
showexpl quite a while ago for my DTX files and it had issues with the special handling of % in these files. I can't remember the exact details now, but I gave up on it and coded something by myself as part of my ydoc class (which is still an unstable alpha version).
– Martin Scharrer
May 28 '11 at 16:55
.tex files, not in .dtx files, and I've not encountered any (serious) problems. If you could add an answer about ydoc I'm sure people would be interested in it.
– Seamus
May 28 '11 at 17:01
ydoc is not really finished yet and has almost no documentation. I didn't do much on that part of the code for over a year and have issues recalling its current state. ;-)
– Martin Scharrer
May 28 '11 at 17:03
showexpl for .dtx writers (which is what this question is about) then even the unstable ydoc might be better. It's surely worth a mention at least!
– Seamus
May 28 '11 at 17:05
showexpl... I assume the problem is only with comments in the sample code being displayed itself, am I right?
– Niel de Beaudrap
May 28 '11 at 18:06
docstrip requires all documentation lines to start with a % and showexpl takes these verbatim. Also, while compiling the documentation all % are ignored, which will print all comments in the example code.
– Martin Scharrer
May 28 '11 at 18:12
% out of the documentation, which is of course the context in which I'll be using showexpl; for the time being I may be able to deal with example code that contains no comments. However, now I'm more concerned about your remarks about showexpl taking the comments of the documentation lines verbatim: wouldn't the functionality of showexpl only be engaged when all of the % characters have been stripped anyhow?
– Niel de Beaudrap
May 28 '11 at 19:01
% are not comment characters but instead ignored.
– Martin Scharrer
May 28 '11 at 19:20
docstrip will stymie any attempts to have comments in the content of a LTXexample environment. I was concerned that you were hinting at additional problems.
– Niel de Beaudrap
May 28 '11 at 19:29
For normal documents the showexpl package would be the way to go, as Seamus already stated in his answer. However, special care must be taken for DTX files because of the special handling of % in them.
I wrote some own code for the my ydoc bundle. The ydoc-expl package of this bundle provides an example float where the actual code is wrapped into examplecode. You can add a caption and also display a list of examples. It uses listings and float for this. You can use \lstset{...} to modify its display. Its sets the compiled output on the left or top and the code on the right or bottom depending on the amount of horizontal space both require.
Note that ydoc is still an alpha version and under development. Things might change in future revisions and there a still a few bugs in it.
You can use the ydoc class instead of ltxdoc or just load the ydoc-expl package.
% \iffalse meta-comment
%
% Copyright (C) 2009 by Martin Scharrer <martin@scharrer-online.de>
% -----------------------------------------------------------------
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3c
% of this license or (at your option) any later version.
% The latest version of this license is in
%
% http://www.latex-project.org/lppl.txt
%
% and version 1.3c or later is part of all distributions of LaTeX
% version 2008/05/04 or later.
%
% This work has the LPPL maintenance status `maintained'.
%
% The Current Maintainer of this work is Martin Scharrer.
%
% This work consists of the files j.dtx, j.ins
% and the derived file j.sty.
%
% \fi
%
% \iffalse
%<*driver>
\documentclass{ltxdoc}
\usepackage{ydoc-expl}
%\documentclass{ydoc}
\usepackage{tikz}
\EnableCrossrefs
%\CodelineIndex
\RecordChanges
%\OnlyDescription
\begin{document}
\DocInput{\jobname.dtx}
\PrintChanges
%\newpage\PrintIndex
\end{document}
%</driver>
% \fi
%
% {Titlepage ..}
% \listof{example}{List of Examples}
%
% \section{Usage}
% \begin{example}
% \begin{examplecode}
% \begin{tikzpicture}
% \draw (2.5,2.5) circle (2cm);
% \draw (0,0) -- (5,5);
% \end{tikzpicture}
% \end{examplecode}
% \caption{Some example}
% \end{example}
%
% \StopEventually{}
% \iffalse
%<*package>
% \fi
%
% \section{Implementation}
% \subsection{Package Header}
%
% \begin{macro}{\mymacro}
% \begin{macrocode}
\def\mymacro{%
}
% \end{macrocode}
% \end{macro}
%
% \Finale
%</package>

There are several packages for this purpose:
fvrb-ex, based on fancyvrb, contributed with fancyvrb together.lstdoc, part of listings package, used by document of listings itself.examplep, based on listings.showexpl, based on listings.sverb, provides demo environment.And Martin may explain ydoc himself.
verbatim package for myself. (I didn't know these packages that time.) It seems the function is implemented time and time again.
– Leo Liu
May 28 '11 at 17:13
lstdoc.sty works fine in DTX files. It is used in listings.dtx. However, it is not well documented.
– Leo Liu
May 28 '11 at 17:18
fvrb-ex (used in fvrb-ex.dtx) and sverb (used in sverb.dtx) in DTX files too.
– Leo Liu
May 28 '11 at 17:38
For documenting my own packages I wrote a custom class cnltx-doc which comes bundled with a package called cnltx-example:
\usepackage{cnltx-example}
or
\usepackage[example]{cnltx}
It provides an environment {example} which can display code and output side by side (for which there's also the {sidebyside} environment)) or above each other or only the code (for which there's also the {sourcecode} environment).
It allows for compiling the example as separate document if needed (with the engine of choice) – this needs shell-escape enabled at least for the first compilation, of course.
The package comes with predefined styles for LaTeX, BibTeX and makeindex listings. Output (colors, frames…) can be customized.
Behind the scenes the listings package is used.
\documentclass{article}
\usepackage{cnltx-example}
\setcnltx{
add-cmds = {foo}
}
\begin{document}
\begin{example}
This is a \LaTeX\ example.
\newcommand*\foo{foo}
Some inline math $x^2$ and some display math:
\begin{equation}
x^2 + y^2 = z^2
\end{equation}
\end{example}
\begin{example}[side-by-side]
This is a \LaTeX\ example.
\newcommand*\foo{foo}
Some inline math $x^2$ and some display math:
\begin{equation}
x^2 + y^2 = z^2
\end{equation}
\end{example}
\begin{example}[side-by-side,code-left=false]
This is a \LaTeX\ example.
\newcommand*\foo{foo}
Some inline math $x^2$ and some display math:
\begin{equation}
x^2 + y^2 = z^2
\end{equation}
\end{example}
\newpage
\begin{example}[compile,graphics={trim={2cm 22cm 2cm 2cm},clip}]
\documentclass{article}
\begin{document}
This is a \LaTeX\ example.
\newcommand*\foo{foo}
Some inline math $x^2$ and some display math:
\begin{equation}
x^2 + y^2 = z^2
\end{equation}
\end{document}
\end{example}
\end{document}
\begin{example}%[side-by-side].
– Sigur
Nov 12 '18 at 23:45
%. These must first be removed from the start of the code lines, but remaining ones must be handled as comments again when processing the output... – Martin Scharrer May 28 '11 at 17:05