5

The following LaTeX document:

\documentclass{amsart}

\usepackage{showexpl}

\begin{document}

Lorem ipsum dolor sit amet, ut ius vero dolores.

\begin{LTXexample}[caption=Example]
\documentclass{amsart}
\begin{document}
    Hello, world!
\end{document}
\end{LTXexample}

Te tollit ignota suscipit eum, id omnes corrumpit deseruisse duo, oblique recusabo qui eu.

\end{document}

results in the following pdf rendering:

pdf rendering of the above LaTeX code

How can I add space (1) above and (2) below the listing caption as well as (3) between the listing and the following paragraph?


My attempted solution

According to the showexpl package's user manual (under section 2 'Usage' on p. 1)

showexpl doesn't know any options by itself, but all options for the underlying packages (listings and graphicx) will be passed to the respective packages.

According to the listings package's user manual (under section 2.7 'Layout elements', paragraph 'Vertical space' on p. 16)

The keys aboveskip and belowskip control the vertical space above and below displayed listings.

Therefore, I tried adding aboveskip=25pt, respectively belowskip=25pt, as options to the LTXexample environment.

The aboveskip option added space below the caption, but the output was no longer vertically centered with respect to the code:

Result of adding the aboveskip option

On the other hand, the belowskip option caused the LaTeX compiler to fail with the following error message:

ERROR: Missing number, treated as zero.

--- TeX said ---  
<to be read again> 
                   \let  
l.14 \end{LTXexample}

--- HELP ---
This is usually caused by a LaTeX command expecting but not finding either a number
or a length as an argument. You may have omitted an argument, or a square bracket
in the text may have been mistaken for the beginning of an optional argument. This
error is also caused by putting \protect in front of either a length command or a
command such as \value that produces a number.
  • 2
    Your output consists of two parts: the listing and the output, and showexpl puts them side-by-side. So keys to change the spacing of the listing are only in part relevant. I would suggest that you use instead of showexpl tcolorbox and its listing library. Much more powerful ... (and better documentated). – Ulrike Fischer May 24 '17 at 9:10
  • @UlrikeFischer: Thanks. tcolorbox looks amazing. Exactly what I need. – Evan Aad May 24 '17 at 10:06
1

Here I rely on \addstackgap[<length>]{} from the stackengine package to provide the gap. However, the quirk here is that the LTXexample cannot go into an \hbox, but only a \vbox. So I save it in a temporary \box0 and then use \makebox to set \box0 in a full width box, to which I can add the stacking gap, shown here twice, once with 5pt and then with 25pt gap.

\documentclass{amsart}
\usepackage{showexpl,stackengine}
\begin{document}
Lorem ipsum dolor sit amet, ut ius vero dolores.

\setbox0=\vbox{
\begin{LTXexample}[caption=Example]
\documentclass{amsart}
\begin{document}
    Hello, world!
\end{document}
\end{LTXexample}
}
\noindent\addstackgap[5pt]{\makebox[\textwidth]{\box0}}
Te tollit ignota suscipit eum, id omnes corrumpit deseruisse duo, oblique recusabo qui eu.

\setbox0=\vbox{
\begin{LTXexample}[caption=Example]
\documentclass{amsart}
\begin{document}
    Hello, world!
\end{document}
\end{LTXexample}
}
\noindent\addstackgap[25pt]{\makebox[\textwidth]{\box0}}
Te tollit ignota suscipit eum, id omnes corrumpit deseruisse duo, oblique recusabo qui eu.
\end{document}

enter image description here

  • Thanks. Your solution only addresses points 1 (space above listing) and 3 (space below listing) in my question but not 2 (space below listing's caption), correct? – Evan Aad Jun 23 '17 at 2:28
  • 1
    @EvanAad That is correct. I think something different will be needed for the last point. I'm giving it some more thought. – Steven B. Segletes Jun 23 '17 at 2:30
  • Thanks, though I wouldn't sweat over it, unless the challenge gives you a kick, because there is a way superior solution in the form of the tcolorbox package, as suggested by Ulrike Fischer in his comment to my question. – Evan Aad Jun 23 '17 at 2:32
  • 1
    @EvanAad FYI, Ulrike is a "she". – Steven B. Segletes Jun 23 '17 at 2:35
  • 1
    @EvanAad Perhaps, since Ulrike failed to post an answer, you could post the tcolorbox answer and accept it as the best answer. – Steven B. Segletes Jun 23 '17 at 2:37
0

As Ulrike Fischer suggested, the best practical way to solve my problem is to dump the showexpl package in favor of the amazingly versatile and infinitely customizable tcolorbox package.

  • 1
    If you post a minimum working example and an image of how to do it, you will get more votes. – Steven B. Segletes Jun 23 '17 at 15:42
  • @StevenB.Segletes: I haven't actually used it yet, just browsed through the manual. – Evan Aad Jun 23 '17 at 20:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.