3

I'm using expex for linguistic examples but in my work I sometimes want to include code snippets instead. I like wrapping code snippets in lstlistings with particular language defined syntax stuff. But to get everything to play nice (aligned at the top of the example number, no page breaks in the middle) , I put that into a minipage. By now, I end up with a large about of stuff:

\ex\label{somelabel}
\begin{minipage}[t]{.9\linewidth}
\begin{lstlisting}
some_code_in_here = \0
\end{lstlisting}
\end{minipage}
\xe

That's a lot of boilerplate, so I'd like to define an environment to make it more convenient:

\newenvironment{exlisting}[1]
 {\ex\label{#1}
  \begin{minipage}[t]{.9\linewidth}
  \begin{lstlisting}
 }
 {\end{minipage}
  \end{lstlisting}
  \xe
 }

But that doesn't work, somehow, my latex ends up saying that it's missing a } (but I think that error is red herring).

Anyway, I believe I've boiled it down to a minimal problem which is that I can't actually wrap up lstlisting in an environment. This MWE illustrates the problem.

\documentclass{article}
\usepackage{listings}
\newenvironment{mylisting}
{\begin{lstlisting}}
{\end{lstlisting}}
\begin{document}
\begin{mylisting}
somecode
\end{mylisting}
\end{document}

I did see the part of the listtings manual (5.16) which says that listings doesn't interact well with newenvironment and that is supposed to be the way to use listings with newenvironments. However, in that case, I end up with a different error, the dreaded *, preceded by a warning which is coming from the listings code. Another MWE:

\documentclass{article}
\usepackage{listings}
\lstnewenvironment{mplisting}
{\begin{minipage}[t]{.9\linewidth}}
{\end{minipage}}
\begin{document}
\begin{mplisting}
something = anotherthing
\end{mplisting}
\end{document}

If I try to compile this on my system with pdflatex it looks like this:

$ pdflatex mwe.tex This is pdfTeX, Version 3.14159265-2.6-1.40.17 (TeX Live 2016/Debian) (preloaded format=pdflatex) restricted \write18 enabled. entering extended mode (./mwe.tex LaTeX2e <2017/01/01> patch level 3 Babel <3.9r> and hyphenation patterns for 3 language(s) loaded. (/usr/share/texlive/texmf-dist/tex/latex/base/article.cls Document Class: article 2014/09/29 v1.4h Standard LaTeX document class (/usr/share/texlive/texmf-dist/tex/latex/base/size10.clo)) (/usr/share/texlive/texmf-dist/tex/latex/listings/listings.sty (/usr/share/texlive/texmf-dist/tex/latex/graphics/keyval.sty) (/usr/share/texlive/texmf-dist/tex/latex/listings/lstmisc.sty) (/usr/share/texlive/texmf-dist/tex/latex/listings/listings.cfg)) (./mwe.aux) (/usr/share/texlive/texmf-dist/tex/latex/base/omscmr.fd)) *

And just hangs at the *. So, I'm pretty lost. My first foray into environment defining hasn't really gotten me anywhere. As in-place code, my expex wrapping minipage wrapping listings looks great, but I have not made any progress on creating an environment to do some of that typing for me.

1

In the definition of \lstnewenvironment you can use

\minipage[t]{.9\linewidth}

instead of \begin{minipage}[t]{.9\linewidth}. In the sample below I use such a definition where I have also inserted some negative vertical space to fit better with the label, the second item shows what your original coding produces.

Sample output

\documentclass{article}

\usepackage{listings,expex}

\lstnewenvironment{mplisting}
{\minipage[t]{.9\linewidth}\vspace{-14pt}}
{\endminipage}

\begin{document}

\ex\label{newversion}
\begin{mplisting}
something = anotherthing
something = anotherthing
\end{mplisting}
\xe

\ex\label{oldversion}
\begin{minipage}[t]{.9\linewidth}
  \begin{lstlisting}
something = anotherthing
something = anotherthing
  \end{lstlisting}
\end{minipage}
\xe

\end{document}

Using this idea you can make an exlisting environment as follows:

\documentclass{article}

\usepackage{listings,expex}

\lstnewenvironment{exlisting}[1]{%
\ex\label{#1}\minipage[t]{.9\linewidth}\vspace{-14pt}}
{\endminipage\xe}

\begin{document}

\begin{exlisting}{name}
something = anotherthing
something = anotherthing
\end{exlisting}

\end{document}

producing

Second sample

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.