4

I'm using the showexpl package for creating codes with the displayed result. For the example settings I used pos=b, placing the output below the code, which is what I want. The problem is that now I cannot add space below the whole example, because belowskip seems to always add the space below the code, so it now separates the code and the output. On the other side, aboveskip works fine.

This is a MWE of my setup:

\documentclass[parskip=full]{scrbook}
\usepackage{showexpl,xcolor,lipsum}
\definecolor{light-gray}{gray}{0.95}
\lstset{
explpreset=
{vsep=6pt,pos=b},
basicstyle=\small\ttfamily,
backgroundcolor=\color{light-gray},
xleftmargin=0cm,
numbers=none,
breaklines=true,
breakindent=0pt,
aboveskip=\baselineskip,
%belowskip=\baselineskip,
}

\begin{document}
\lipsum[1]
\begin{LTXexample}
$e^{\pi i}-1=0  $
\end{LTXexample}
\lipsum[2]
\end{document}

I've commented the line which produces the problem. Is there a way to add space after the example? I don't want to add space manually after every \end{LTXexample}.

EDIT: added parskip=full option, as it is being used and it affects the problem.

4
  • The problem seems to be in your use of vsep=6pt inside the \explpreset settings. If you change this to vsep=-9pt then the input and output come back together. I haven't posted this as a solution as the spaces at the top an bottom of the LTXeample environments are not equal. Incidentally, it should be possible to use lineskip inside \lstset but thisbreaks the backgroundcolor setting...which looks like a bug.
    – user30471
    May 31, 2015 at 9:47
  • Setting vsep=-9pt looks like a dirty trick, although it can certainly work. Something like that and aboveskip=.5\baselineskip gives approximately equal spaces, but I would like a cleaner solution (if possible).
    – han-tyumi
    Jun 1, 2015 at 11:14
  • Yes, I would like a cleaner solution too. That's why I didn't post it as an answer.
    – user30471
    Jun 1, 2015 at 14:56
  • 1
    I've just skimmed through the source code, and showexpl doesn't seem to have an option to add, let's say, \topsep or \bottomsep lengths precisely to control the top and bottom separation of the example code from the rest of the text. I think you may request these features to the maintainer.
    – jarnosc
    Jun 2, 2015 at 21:22

3 Answers 3

5
+100

The commented %belowskip=\baselineskip in the MWE adds the space at the end of the listing, i.e. before the result, not after the result; the aboveskip=\baselineskip assignment works because there is nothing before the code listing to interfere with the space.

I would add the space at the end of the definition of the LTXexample environment; but instead of redefining the whole environment, I would simply add the space to the definition of the \SX@put@code@result macro called at the end of the environment. So, using \g@addto@macro from LaTeX2e kernel, I would add the following before the \begin{document} in your MWE.

\makeatletter
\g@addto@macro{\SX@put@code@result}{\vskip\baselineskip}
\makeatother

This works on my console.

11
  • Nice use of \addto! Of course, then he has to load babel for completely non-language-related reasons. I thought about defining a wrapper for the LTXexample environment, but couldn't get it to work. In my solution, he can tweak it however he likes; yours is shorter. Jun 2, 2015 at 19:54
  • @dgoodmaniii. No: that's why I added the definition manually. Yes: with english as the main language. BTW, if you asked me, \addto is so handy that I would add \addto to the kernel of LaTeX :o)
    – jarnosc
    Jun 2, 2015 at 19:56
  • Oh, I see; skimmed too quickly. Yes, well played. Jun 2, 2015 at 19:57
  • 2
    @Lusr56: The LaTeX kernel has \g@addto@macro.
    – GuM
    Jun 2, 2015 at 21:50
  • 1
    @Majoko I don't think that's necessary; the current answer works without defining new commands or loading more packages, so it is fairly general.
    – jarnosc
    Jun 5, 2015 at 15:54
3

Well, I'm not sure what you mean by "more elegant", but have you considered simply cheating?

(I'm assuming here that the OP and commenter have exhausted the options with the package itself; the solution I'm positing here is not ideal, if there were a package option to cover the circumstance.)

That is, you're using LTXexample, which is defined in showexpl by means of a \lstnewenvironment command. You want behavior different from that in the default LTXexample environment. So don't use LTXexample; using something very similar, but which adds a bit more space at the bottom.

\documentclass[parskip=full]{scrbook}
\usepackage{showexpl,xcolor,lipsum,listings}
\definecolor{light-gray}{gray}{0.95}
\lstset{
explpreset=
{vsep=6pt,pos=b},
basicstyle=\small\ttfamily,
backgroundcolor=\color{light-gray},
xleftmargin=0cm,
numbers=none,
breaklines=true,
breakindent=0pt,
aboveskip=\baselineskip,
}
\makeatletter
\let\LTXexample\undefined
\let\LTXexample@\undefined
\let\endLTXexample@\undefined
\lstnewenvironment{myLTXexample}[1][]
{%
  \@temptokena{#1}%
  \begingroup
    \advance\c@ltxexample\@ne \advance\c@lstlisting\@ne
    \expandafter\lstset\expandafter{\SX@explpreset,#1}%
    \edef\x{\endgroup
      \def\noexpand\SX@codefile{\SX@codefile}%
      \def\noexpand\SX@graphicname{\SX@graphicname}%
      \def\noexpand\SX@graphicparam{\SX@graphicparam}}%
  \x
  \xdef\SX@@explpreset{\the\@temptokena,codefile=\SX@codefile,
    graphic={[\SX@graphicparam]{\SX@graphicname}}}%
  \setbox\@tempboxa=\hbox\bgroup% Warum noetig?
  \lst@BeginWriteFile{\SX@codefile}%
}
{%
  \lst@EndWriteFile\egroup
  \SX@put@code@result
  \vskip\baselineskip%
}
\makeatother
\begin{document}
\lipsum[1]
\begin{myLTXexample}
$e^{\pi i}-1=0  $
\end{myLTXexample}
\lipsum[2]
\end{document}

The only new line here is \vskip\baselineskip%; the remainder is verbatim the definition of LTXexample. But it seems to produce your desired result, if I understand it correctly:

myLTXexample

Of course, you can increase or decrease the space as desired to match aboveskip.

This solution bulks up your preamble, but it does solve your problem without the need for manually inserting \vskips after each LTXexample. I've defined a new environment, but you could just override LTXexample yourself if you'd like.

2

Yet, another approach would be to add the space after the environment with the etoolbox package saying

\usepackage{etoolbox}
\AfterEndEnvironment{LTXexample}{\vskip\baselineskip}

Throws a warning (apparently a hook from scrbook used by etoolbox is deprecated), but still works on my console.

3
  • 1
    As this approach follows basically the same idea from your other answer, would'nt it be better if to edit this one into your original one?
    – Ruben
    Jun 5, 2015 at 9:05
  • @Ruben I thought about the same thing; and in fact the first version was an addition to the first answer; but then I realized that pretty much any answer would go along the same lines (how to append \vspace to the list), the two answers are fairly different: one uses a built-in feature of LaTeX, and the other requires loading yet another package. I asked at Meta and the answer was that including different answers in the same post or in different ones may vary from case to case.
    – jarnosc
    Jun 5, 2015 at 15:53
  • I agree with Ruben: this would be better included as an edit to your first answer because it is basically the same solution but with a different (essentially) equivalent mechanism.
    – user30471
    Jun 7, 2015 at 19:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .