如何定义自定义列表环境?

如何定义自定义列表环境?

我正在尝试为特定编程语言 F# 定义一个环境。这是我的代码:

\definecolor{bluekeywords}{rgb}{0.13,0.13,1}
\definecolor{greencomments}{rgb}{0,0.5,0}
\definecolor{turqusnumbers}{rgb}{0.17,0.57,0.69}
\definecolor{redstrings}{rgb}{0.5,0,0}

\lstdefinelanguage{FSharp}
    {morekeywords={let, new, match, with, rec, open, module, namespace, type, of, member, and, for, in, do, begin, end, fun, function, try, mutable, if, then, else},
    keywordstyle=\color{bluekeywords},
    sensitive=false,
    morecomment=[l][\color{greencomments}]{///},
    morecomment=[l][\color{greencomments}]{//},
    morecomment=[s][\color{greencomments}]{{(*}{*)}},
    morestring=[b]",
    stringstyle=\color{redstrings}
    }

\newenvironment{fslisting}
  {
    \lstset{
        language=FSharp,
        basicstyle=\ttfamily,
        breaklines=true,
        columns=fullflexible}
    \begin{lstlisting}
  }
  {
    \end{lstlisting}
  }

然后我尝试这样使用它:

\begin{fslisting}
type 'a Process = 'a Signal IObservable
\end{fslisting}

但它不起作用。输出日志显示列表未终止 - 或者被文档终止。因此文档的其余部分也被格式化为列表,这当然不是我们的意图。

我在这里做错了什么?

答案1

lstlisting您无法通过创建新环境\newenvironment。您需要使用\lstnewenvironment。请参阅listings 文档, 部分4.5 环境(第 42 页)。

在此处输入图片描述

\documentclass{article}

\usepackage{listings,xcolor}

\definecolor{bluekeywords}{rgb}{0.13,0.13,1}
\definecolor{greencomments}{rgb}{0,0.5,0}
\definecolor{turqusnumbers}{rgb}{0.17,0.57,0.69}
\definecolor{redstrings}{rgb}{0.5,0,0}

\lstdefinelanguage{FSharp}
    {morekeywords={let, new, match, with, rec, open, module, namespace, type, of, member, and, for, in, do, begin, end, fun, function, try, mutable, if, then, else},
    keywordstyle=\color{bluekeywords},
    sensitive=false,
    morecomment=[l][\color{greencomments}]{///},
    morecomment=[l][\color{greencomments}]{//},
    morecomment=[s][\color{greencomments}]{{(*}{*)}},
    morestring=[b]",
    stringstyle=\color{redstrings}
    }

\lstnewenvironment{fslisting}
  {
    \lstset{
        language=FSharp,
        basicstyle=\ttfamily,
        breaklines=true,
        columns=fullflexible}
  }
  {
  }
\begin{document}

\begin{fslisting}
type 'a Process = 'a Signal IObservable
\end{fslisting}

\end{document}

答案2

只需使用提供的\lstnewenvironment命令。

\documentclass{article}


\usepackage{xcolor}
\usepackage{listings}

\definecolor{bluekeywords}{rgb}{0.13,0.13,1}
\definecolor{greencomments}{rgb}{0,0.5,0}
\definecolor{turqusnumbers}{rgb}{0.17,0.57,0.69}
\definecolor{redstrings}{rgb}{0.5,0,0}

\lstdefinelanguage{FSharp}
    {morekeywords={let, new, match, with, rec, open, module, namespace, type, of, member, and, for, in, do, begin, end, fun, function, try, mutable, if, then, else},
    keywordstyle=\color{bluekeywords},
    sensitive=false,
    morecomment=[l][\color{greencomments}]{///},
    morecomment=[l][\color{greencomments}]{//},
    morecomment=[s][\color{greencomments}]{{(*}{*)}},
    morestring=[b]",
    stringstyle=\color{redstrings}
    }

\lstnewenvironment{fslisting}
    {\lstset{language=FSharp,
                basicstyle=\ttfamily,
                breaklines=true,
                columns=fullflexible
    }}
    {}

\begin{document}

\begin{fslisting}
type 'a Process = 'a Signal IObservable
\end{fslisting}
\end{document}

相关内容