18

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}
 \begin{equation}
  \frac{3}{2}=\frac{a}{b}
 \end{equation}
\end{texcode}
\end{listing}
\lipsum[4]
\begin{listing}
\begin{minipage}{\ideal}
\begin{texcode}
 \begin{equation}
  \frac{3}{2}=\frac{a}{b}
 \end{equation}
\end{texcode}
\end{minipage}
\hfill
\begin{minipage}{\ideal}
 \begin{equation}
  \frac{3}{2}=\frac{a}{b}
 \end{equation}
\end{minipage}
\caption{title}
\end{listing}
\lipsum[4]

\end{document}

The output is:

enter image description here

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}
 \begin{equation}
  \frac{3}{2}=\frac{a}{b}
 \end{equation}
\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.

Sigur
  • 37,330

3 Answers3

12

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}
 \begin{equation}
  \frac{3}{2}=\frac{a}{b}
 \end{equation}
\end{tcblisting}
\lipsum[4]
\begin{tcblisting}{listing side text}
 \begin{equation}
  \frac{3}{2}=\frac{a}{b}
 \end{equation}
\end{tcblisting}
\lipsum[4]

\end{document}

enter image description here

Ignasi
  • 136,588
  • 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
  • 1
    @Sigur tcolorbox works with minted too!. –  Jun 28 '14 at 23:41
10

This merely is a refinement of Konrad's answer that adheres to your specs a bit closer.

enter image description here

\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}
 \begin{equation}
  \frac{3}{2}=\frac{a}{b}
 \end{equation}
\end{myenv}

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

\end{document}
jub0bs
  • 58,916
  • 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. – jub0bs Jun 28 '14 at 12:56
  • The problem with this kind of env is if I insert a MWE with preamble, the result will be broken since we can not input that code on the right minipage. It works well for piece of codes that are compilable. – Sigur Jun 28 '14 at 13:00
  • @Sigur You could always comment out the preamble part and let your readers know that they should uncomment it. – jub0bs Jun 28 '14 at 13:02
  • Good idea. Now I'll try to make the example code does not influence on my doc structure. For example, if the example code has a \section, I don't want its output following my numbering sections. The same for equation numbering. – Sigur Jun 28 '14 at 13:05
  • @Sigur Tough one. I guess you'd have to redefine all the commands that modify macros and counters at the global scope. – jub0bs Jun 28 '14 at 13:08
  • Well, for equations, I was thinking to define a ex.equation environment and try to replace (maybe using sed) the code within example.out. Can we insert bash commands inside the environment definitions? – Sigur Jun 28 '14 at 13:12
  • 1
    @Sigur The bashful package may be of interest. – jub0bs Jun 28 '14 at 13:24
  • Your code above is producing ! FancyVerb Error: Extraneous input \begin {equation} between \begin{myenv}[<key=value>] and line end. If I use myenv only once, no problem. – Sigur Jun 28 '14 at 13:33
  • 1
    @Sigur The error disappears if you make sure that \begin{myenv} is followed by an empty []. Surely, that could be made more robust, though... – jub0bs Jun 28 '14 at 13:40
  • It is not too optional. It is a kind of opitionatory (mandatory option lol). Nice! – Sigur Jun 28 '14 at 13:41
  • 1
    @Sigur I guess making the caption a mandatory argument (which you can leave empty) makes more sense. See my last edit. – jub0bs Jun 28 '14 at 13:43
7

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).

Konrad Rudolph
  • 39,394
  • 22
  • 107
  • 160
  • 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