# Fancy code environment

I'm writing a small tutorial about LaTeX. So I'd like to insert some code and obtain the result on the right.

I'm almost there. I got the desired output but I'd like something more clear, that is, an easier to type the code.

Consider the following MWE:

\documentclass[draft,11pt,a4paper]{article}
\usepackage{showexpl}

\usepackage[section]{minted}

\newminted{tex}{
bgcolor=bg,
gobble=1,
linenos=true,
firstline=1,
firstnumber=1,
fontfamily=courier,
fontsize=\footnotesize,
numberblanklines=false,
numbersep=3mm,
xleftmargin=6mm,
xrightmargin=5mm,
}

\definecolor{bg}{rgb}{0.95,0.95,0.95}

\usepackage{lipsum}

\newlength\ideal
\setlength{\ideal}{.5\linewidth - 2\fboxsep -2\fboxrule}

\begin{document}

\lipsum[4]
\begin{listing}
\begin{texcode}
$$\frac{3}{2}=\frac{a}{b}$$
\end{texcode}
\end{listing}
\lipsum[4]
\begin{listing}
\begin{minipage}{\ideal}
\begin{texcode}
$$\frac{3}{2}=\frac{a}{b}$$
\end{texcode}
\end{minipage}
\hfill
\begin{minipage}{\ideal}
$$\frac{3}{2}=\frac{a}{b}$$
\end{minipage}
\caption{title}
\end{listing}
\lipsum[4]

\end{document}


The output is:

I'd like to know if this is the right way or if is possible to avoid typing code twice.

I'd like to define an environment myenv and then obtain the same as Listing 0.1 simply using

\begin{myenv}
$$\frac{3}{2}=\frac{a}{b}$$
\end{myenv}


And also the caption would be optional. I tried to use showexpl but no success until now.

Edit

I've just found this topic but the result there does not produce the output on the right.

-
Are you set on using minted? Would you consider using listings? –  Jubobs Jun 28 '14 at 9:57
@Jubobs, I'm using minted for the colored code. Only this. –  Sigur Jun 28 '14 at 12:19
@Jubobs Even if it's not useful for the OP, you can always post an answer, other people will appreciate it :) –  Manuel Jun 28 '14 at 12:21

Do you know tcolorbox? It was developed to help formating LaTeX tutorials.

Next code uses default settings, from there you can customize almost everything.

\documentclass[11pt,a4paper]{article}

\usepackage[most]{tcolorbox}

\usepackage{lipsum}

\begin{document}

\lipsum[4]

\begin{tcblisting}{listing only}
$$\frac{3}{2}=\frac{a}{b}$$
\end{tcblisting}
\lipsum[4]
\begin{tcblisting}{listing side text}
$$\frac{3}{2}=\frac{a}{b}$$
\end{tcblisting}
\lipsum[4]

\end{document}


-
Thanks so much. I'll check if it is possible to have colored code like in minted. When there are many packages to do almost the same job we get confused. Thanks again. –  Sigur Jun 28 '14 at 12:19
@Sigur tcolorbox works with minted too!. –  Harish Kumar Jun 28 '14 at 23:41

\documentclass{article}

\usepackage{minted}

\definecolor{bg}{rgb}{0.95,0.95,0.95}

% conditional caption (only gets printed if the argument is not empty)
\newcommand\condcaption[1]{%
\if\relax\detokenize{#1}\relax
\else
\caption{#1}
\fi
}

\newenvironment{myenv}[1]
{%
\listing
\condcaption{#1}
\VerbatimEnvironment
\begin{VerbatimOut}{example.out}%
}{%
\end{VerbatimOut}%
\vspace{1ex}%
\setlength{\parindent}{0pt}%
\begin{minipage}{0.5\linewidth}%
\inputminted[
resetmargins,
bgcolor          = bg,
gobble           = 1,
linenos          = true,
firstline        = 1,
firstnumber      = 1,
fontfamily       = courier,
fontsize         = \footnotesize,
numberblanklines = false,
numbersep        = 3mm,
xleftmargin      = 6mm,
xrightmargin     = 5mm,
]{latex}{example.out}%
\end{minipage}%
\hspace{0.05\linewidth}%
\begin{minipage}{0.4\linewidth}%
\input{example.out}%
\end{minipage}
\endlisting
}

\begin{document}
\begin{myenv}{A simple equation}
$$\frac{3}{2}=\frac{a}{b}$$
\end{myenv}

\begin{myenv}{}
$$\frac{3}{2}=1+\frac{1}{2}$$
\end{myenv}

\end{document}

-
Nice! I'd just inserted \begin{listing}\begin{minipage}{0.5\linewidth} to have the output inside the listing environment. Thanks. –  Sigur Jun 28 '14 at 12:55
@Sigur Yes, me too. I'm trying to get captions to work as an optional argument. –  Jubobs Jun 28 '14 at 12:56
@Sigur The bashful package may be of interest. –  Jubobs Jun 28 '14 at 13:24
@Sigur The error disappears if you make sure that \begin{myenv} is followed by an empty []. Surely, that could be made more robust, though... –  Jubobs Jun 28 '14 at 13:40
@Sigur I guess making the caption a mandatory argument (which you can leave empty) makes more sense. See my last edit. –  Jubobs Jun 28 '14 at 13:43

Take a look at the source code of minted, it does the exact same thing to typeset its examples in the documentation.

Here’s the relevant part:

\newenvironment{example}
{\VerbatimEnvironment
\begin{VerbatimOut}[gobble=3]{example.out}}
{\end{VerbatimOut}%
\vspace{1ex}%
\setlength{\parindent}{0pt}%
\fbox{\begin{minipage}{0.5\linewidth}%
\inputminted[resetmargins]{latex}{example.out}%
\end{minipage}%
\hspace{0.05\linewidth}%
\begin{minipage}{0.4\linewidth}%
\input{example.out}%
\end{minipage}%
\vspace{1ex}}}


It uses two side-by-side minipages to put the example code and output adjacently. Internally, \VerbatimOut is used to store the example code, verbatim, in a temporary file which is then passed to minted for pretty-printing (left side) and \inputted directly for typesetting (on the right side).

-
Ow, thanks so much for this nice piece of code. I'll try to adapt it and make some tests. –  Sigur Jun 28 '14 at 12:14