1

Hey I am using code bellow to create custom lstnewenvironment, it all works fine if I have

\begin{test}{caption}{lst:label}

on one line. If I break it in any way for example

\begin{test}{Some very very very very
   long caption that spans multiple lines}{lst:tempeature_model}

the compilation will crash with following error:

(/usr/local/texlive/2015/texmf-dist/tex/latex/listings/lstlang1.sty) [1{/usr/lo
cal/texlive/2015/texmf-var/fonts/map/pdftex/updmap/pdftex.map}
! Argument of \@gobble has an extra }.
<inserted text>
                \par
l.53 \end{document}

it took me few hours to figure out what was wrong, but know knowing it I have no clue how to fix it. Help is very much appreciated!

\documentclass[10pt]{article} 
\usepackage[usenames,dvipsnames]{color}
\usepackage{graphicx} 
\usepackage{listings}

  \lstnewenvironment{test}[2]
{
\lstset{
language=Python,
belowcaptionskip=1\baselineskip,
breaklines=true, % sets automatic line breaking
tabsize=4,
numberstyle=\tiny\noncopynumber,
basicstyle=\footnotesize\ttfamily,
keywordstyle=\color[rgb]{0,0,1},
commentstyle=\itshape\color[rgb]{.133,.545,.133},
stringstyle=\color{mauve},
showstringspaces=false,
breakatwhitespace=false,
framexleftmargin=4mm,
captionpos=b,
frame=single,
caption= #1, 
label= #2
}%lstset
  \vspace{\baselineskip}
  \hfill%
  \minipage{.95\textwidth}%
}%lstnewenvironment
{
 \endminipage%
}

\begin{document}


\section{Fancy}
\label{sec:definitions}

\begin{test}{caption}{lst:label}
class BreakMe:
    def __init__():
        pass
\end{test}

\end{document}

1 Answers1

1

Place a % at the line break point, as in

\begin{test}{caption very %
long}{lst:label}
class BreakMe:
    def __init__():
        pass
\end{test}

Here is the MWE:

\documentclass[10pt]{article} 
\usepackage[usenames,dvipsnames]{color}
\usepackage{graphicx} 
\usepackage{listings}

  \lstnewenvironment{test}[2]
{
\lstset{
language=Python,
belowcaptionskip=1\baselineskip,
breaklines=true, % sets automatic line breaking
tabsize=4,
numberstyle=\tiny\noncopynumber,
basicstyle=\footnotesize\ttfamily,
keywordstyle=\color[rgb]{0,0,1},
commentstyle=\itshape\color[rgb]{.133,.545,.133},
stringstyle=\color{mauve},
showstringspaces=false,
breakatwhitespace=false,
framexleftmargin=4mm,
captionpos=b,
frame=single,
caption= #1, 
label= #2
}%lstset
  \vspace{\baselineskip}
  \hfill%
  \minipage{.95\textwidth}%
}%lstnewenvironment
{
 \endminipage%
}

\begin{document}


\section{Fancy}
\label{sec:definitions}

\begin{test}{caption very %
long}{lst:label}
class BreakMe:
    def __init__():
        pass
\end{test}

\end{document}

enter image description here