lstnewenvironment 参数跨越多行

lstnewenvironment 参数跨越多行

嘿,我正在使用下面的代码来创建自定义 lstnewenvironment,如果我有,一切都会正常

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

在同一行。如果我以任何方式打破它,例如

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

编译将崩溃并出现以下错误:

(/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}

我花了几个小时才弄清楚问题出在哪里,但我知道我不知道该如何修复它。非常感谢您的帮助!

\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

将 a 放置%在换行符处,例如

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

以下是 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}

在此处输入图片描述

相关内容