18

Thanks to this answer, I know it is possible to float and wrap text around a listing. I would like to setup an environment for mini-code snippets that always floats and wraps the same way. I can achieve the effect manually, but every attempt I have made at combining the environments falls flat.

Here is a MWE that shows what I'm after and what doesn't work:

\documentclass{scrartcl}

\usepackage{listings}
\usepackage{blindtext}
\usepackage{wrapfig}

\lstnewenvironment{mysnippets}[1][]{%
    \lstset{aboveskip=-12pt,belowskip=-12pt,caption=#1,captionpos=b}
    \ttfamily
}{%
}
\lstnewenvironment{yoursnippets}[1][]{%
    \lstset{aboveskip=-12pt,belowskip=-12pt,caption=#1,captionpos=b}
    \wrapfigure{R}{.5\textwidth}
    \ttfamily
}{%
    \endwrapfigure
}

%% This sort of arrangement doesn't work
%\newenvironment{oursnippets}{%
%    \wrapfigure{R}{.5\textwidth}
%    \mysnippets[test]
%}{%
%    \endmysnippets
%    \endwrapfigure
%}

\begin{document}

\subsection*{This works if you do it by hand}

\begin{wrapfigure}{R}{.5\textwidth}
\begin{mysnippets}[ac karni cozumu]
if (aciktim) {
    manti yeyeyim
}
\end{mysnippets}
\end{wrapfigure}

\blindtext

\subsection*{This arrangement floats but not to anywhere useful}

\begin{yoursnippets}[ac karni cozumu]
if (aciktin) {
    manti yeyin
}
\end{yoursnippets}
\blindtext

%\subsection*{This and all my other attempts don't even compile}
%
%\begin{oursnippets}[ac karni cozumu]
%if (aciktik) {
%    manti yeyelim
%}
%\end{oursnippets}
%\blindtext

\end{document}

Which currently only produces something like:

MWE

I am not attached to wrapfig, but would really prefer to stick with listings as possible since I use it extensively for larger blocks of code samples already. Is there a way to combine these environments so I have a snippets environment that always floats here and wraps, but is typeset by listings?

Caleb
  • 1,988

0 Answers0