# How to put lstlisting in a minipage

I had a problem creating a new environment that would let me put a lstlisting inside a minipage. I could create a new environment that sets up the minipage, then in the document put a lstlisting inside that minipage, but I couldn't create environment that did both together.

\documentclass{article}
\usepackage{listings}
\lstset{language=SQL,keywordstyle=\underbar,numbers=none}
\newenvironment{egdlisting}%
{\hfill\begin{minipage}{\dimexpr\textwidth-2em}}%
{\end{minipage}}

\begin{document}

\begin{egdlisting}
\begin{lstlisting}
select *
from foo
where bar = 4;
\end{lstlisting}
\end{egdlisting}

\end{document}


Works. The obvious next step, putting the lstlisting inside the environment, does not.

\documentclass{article}
\usepackage{listings}
\lstset{language=SQL,keywordstyle=\underbar,numbers=none}
\newenvironment{egdlisting}%
{\hfill\begin{minipage}{\dimexpr\textwidth-2em}\begin{lstlisting}}%
{\end{lstlisting}\end{minipage}}

\begin{document}

\begin{egdlisting}
select *
from foo
where bar = 4;
\end{egdlisting}

\end{document}


The error message (shown for recognition purposes) is below.

\$ lualatex tex/sm.tex >& build.log


gave me

This is LuaHBTeX, Version 1.12.0 (TeX Live 2020/Cygwin)
restricted system commands enabled.
(./tex/sm.tex
LaTeX2e <2020-02-02> patch level 5
L3 programming layer <2020-03-06>
(/usr/share/texmf-dist/tex/latex/base/article.cls
Document Class: article 2019/12/20 v1.4l Standard LaTeX document class
(/usr/share/texmf-dist/tex/latex/base/size10.clo))
(/usr/share/texmf-dist/tex/latex/listings/listings.sty
(/usr/share/texmf-dist/tex/latex/graphics/keyval.sty)
(/usr/share/texmf-dist/tex/latex/listings/lstmisc.sty)
(/usr/share/texmf-dist/tex/latex/listings/listings.cfg))
(/usr/share/texmf-dist/tex/latex/listings/lstlang1.sty)
(/usr/share/texmf-dist/tex/latex/l3backend/l3backend-pdfmode.def)
(./WORK/sm/sm.aux) (/usr/share/texmf-dist/tex/latex/base/ts1cmr.fd))
*


Update Section 4.5 of the listings package documentation, specifically \lstnewenvironment, solves this... but you have to use a different syntax for the minipage.

\documentclass{article}
\usepackage{listings}
\usepackage{lipsum} % provide text around listings, to show indent
\lstset{numbers=none}
\lstnewenvironment{newlisting}{\hfill\minipage{\dimexpr\textwidth-1cm}}{\endminipage}

\begin{document}
\lipsum[1]

\begin{newlisting}
select *
from foo
where bar = 4;
\end{newlisting}

\lstset{numbers=left}

\begin{newlisting}
select *
from foo
where bar = 4;
\end{newlisting}

\lipsum[2]
\end{document}


gives me the output I'm looking for.

The critical bit, besides using the correct command (\lstnewenvironment) is to not use \begin{minipage}{width} ... \end{minipage}, but to use \minipage{width} ... \endminipage.

• see the listings documentation, section 4.5 Environments. – Ulrike Fischer Apr 11 at 17:29
• this is the same as the standard verbatim environment and verb command, no such constructs work in the argument of another command or in standard definitions. But see Ulrike's link – David Carlisle Apr 11 at 17:37
• No it will work I said to do what Ulrike says (Not that I ever do what she says:-) – David Carlisle Apr 11 at 17:40
• @Rmano: That's not really necessary, as a minipage-like effect is also possible by changing the xleftmargin and xrightmargin keys. – Werner Apr 11 at 18:13
• @Werner better yet, xleftmargin (which I also overlooked, reading fail) meets my purpose without having to create a new environment. Still, how to create that new environment was not at all obvious, so I learned two things today :) – Keith Davies Apr 11 at 18:24

Reading page 43 of the documentation, and using the trick of replacing \begin{env}...\end{env} with \env ... \endenv(*):

\documentclass{article}
\usepackage{listings}
\lstset{language=SQL,keywordstyle=\underbar,numbers=none}

\lstnewenvironment{egdlisting}
{\minipage{\dimexpr\textwidth-2em}}
{\endminipage}

\begin{document}

\begin{egdlisting}
select *
from foo
where bar = 4;
\end{egdlisting}

\end{document}


I am not sure if this is what you are looking for...

(*) not sure if this is a documented thing or a dirty hack...

• Oh! The missing piece! I'd forgotten about that method of starting and ending environments. Solved! Thank you. – Keith Davies Apr 11 at 18:01
• Happy to be useful! – Rmano Apr 11 at 18:21