リスト \def 問題

リスト \def 問題

次のコードを xelatex でコンパイルすると、%\end{lstlisting}エラーを防ぐために必要な行があります。問題は何でしょうか?

\documentclass[a4paper,11pt, onecolumn, openany,]{memoir}
\usepackage[english]{babel} % English please
%\usepackage[final]{microtype} % Less badboxes
\usepackage{fontspec}


\usepackage{listings}
\usepackage{xcolor}


\newfontfamily{\lstcode}[Scale=0.85]{Ubuntu Mono}
\lstdefinestyle{csharp}
{
     language=[Sharp]C,
     frame=shadowbox,
     rulecolor=\color{white!80!black},
     rulesepcolor=\color{white!80!black},
     basicstyle=\lstcode,
     showstringspaces=false,     
     breaklines=true
}

\def\bcsh#1\ncsh
{
    \lstset{style=csharp}
    \begin{lstlisting}[numbers=left]
    #1
    \end{lstlisting}
}

\begin{document}
\bcsh
class Program
{  
    static void Main()
    {
        System.Console.WriteLine("Hello World!");
        System.Console.ReadKey();
    }
}
\ncsh
%\end{lstlisting}

\end{document}

答え1

lstlisting短い答え:マクロへの引数として環境を渡すことはできません。

代わりに、新しい環境を定義することもできます。

\documentclass[a4paper,11pt, onecolumn, openany,]{memoir}
\usepackage[english]{babel} % English please
%\usepackage[final]{microtype} % Less badboxes
\usepackage{fontspec}


\usepackage{listings}
\usepackage{xcolor}


\newfontfamily{\lstcode}[Scale=0.85]{Ubuntu Mono}
\lstdefinestyle{csharp}
{
     language=[Sharp]C,
     frame=shadowbox,
     rulecolor=\color{white!80!black},
     rulesepcolor=\color{white!80!black},
     basicstyle=\lstcode,
     showstringspaces=false,     
     breaklines=true
}

\lstnewenvironment{bcsh}[1][]
 {\lstset{style=csharp,numbers=left,#1}}
 {}

\begin{document}
\begin{bcsh}
class Program
{  
    static void Main()
    {
        System.Console.WriteLine("Hello World!");
        System.Console.ReadKey();
    }
}
\end{bcsh}

\end{document}

ここに画像の説明を入力してください

関連情報