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

share|improve this question
1  
Are you set on using minted? Would you consider using listings? –  Jubobs Jun 28 at 9:57
    
@Jubobs, I'm using minted for the colored code. Only this. –  Sigur Jun 28 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 at 12:21
    
Have you considered accepting an answer? or are you not happy with the existing answers? –  Jubobs Jul 31 at 17:51

3 Answers 3

up vote 4 down vote accepted

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

share|improve this answer
    
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 at 12:19
1  
@Sigur tcolorbox works with minted too!. –  Harish Kumar Jun 28 at 23:41

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

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

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}

\makeatletter
% conditional caption (only gets printed if the argument is not empty)
\newcommand\condcaption[1]{%
  \def\@tempa{#1}%
  \ifx\@tempa\@empty\else
    \caption{#1}
  \fi
}
\makeatother

\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}
share|improve this answer
    
Nice! I'd just inserted \begin{listing}\begin{minipage}{0.5\linewidth} to have the output inside the listing environment. Thanks. –  Sigur Jun 28 at 12:55
    
@Sigur Yes, me too. I'm trying to get captions to work as an optional argument. –  Jubobs Jun 28 at 12:56
1  
@Sigur The bashful package may be of interest. –  Jubobs Jun 28 at 13:24
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... –  Jubobs Jun 28 at 13:40
1  
@Sigur I guess making the caption a mandatory argument (which you can leave empty) makes more sense. See my last edit. –  Jubobs Jun 28 at 13:43

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.