关于这个问题是重复的

关于这个问题是重复的

我将 Prolog 代码添加到我的幻灯片中,如下所示:

\documentclass{beamer}

\usepackage{listings}
\usepackage{mdframed}

% No navigation symbols at the bottom of each slide
\beamertemplatenavigationsymbolsempty

\definecolor{light-gray}{gray}{0.89}

\begin{document}

\defverbatim[colored]\lstMiaFood{
\begin{mdframed}[
    frametitle=mia.pl,
    frametitlerule=true,
    frametitlebackgroundcolor=light-gray!30,
    backgroundcolor=light-gray,
    roundcorner=2pt,
    leftmargin=1,
    rightmargin=1,
    innerleftmargin=1,
    innertopmargin=1,
    innerbottommargin=1,
    outerlinewidth=2,
    linecolor=gray
  ]

  \begin{lstlisting}[
    language=Prolog,
    basicstyle=\ttfamily,
    keywordstyle=\color{blue}
  ]
likes(mia, Food) :- isItalian(Food).

likes(mia, Food) :- isAmerican(Food),
                    isVegetarian(Food).
isItalian(pizza).
isItalian(lasagna).
isItalian(spaghetti).

isAmerican(frenchFries).
isAmerican(hamburger).
isAmerican(macaroniAndCheese).

isVegetarian(frenchFries).
isVegetarian(macaroniAndCheese).

  \end{lstlisting}
\end{mdframed}
}

\frame{
  \frametitle{Test code}
  \lstMiaFood
}

\end{document}

这工作正常并产生如下幻灯片:

带代码的幻灯片

我想重复使用列表和框架的样式,这就是我尝试定义的原因\newcommand

\newcommand{\prologCode}[1]{
  \defverbatim[colored]\lstMiaFood{
    \begin{mdframed}[
        frametitle=mia.pl,
        frametitlerule=true,
        frametitlebackgroundcolor=light-gray!30,
        backgroundcolor=light-gray,
        roundcorner=2pt,
        leftmargin=1,
        rightmargin=1,
        innerleftmargin=1,
        innertopmargin=1,
        innerbottommargin=1,
        outerlinewidth=2,
        linecolor=gray
      ]
      \begin{lstlisting}[
        language=Prolog,
        basicstyle=\ttfamily,
        keywordstyle=\color{blue}
      ]
{#1}
      \end{lstlisting}
    \end{mdframed}
  }
}

但这个错误是:

(/usr/share/texmf-dist/tex/latex/listings/lstlang2.sty)
Runaway argument?
{ 
! Paragraph ended before \\lstlisting@ was complete.
<to be read again> 
                  \par 
l.94 }

有没有办法解决此错误并重新使用列表和框架样式?


关于这个问题是重复的

我知道这个答案但是它的两种解决方案都会使用我上面提供的 MWE 产生编译错误:

! LaTeX3 Error: The verbatim command '\code' cannot be used inside an
(LaTeX3)        argument.

由制作

\ExplSyntaxOn
\NewDocumentCommand{\code}{m +v}{
  \exp_args:Nx \scantokens
  {
    \string\begin{lstlisting}[\unexpanded{language=#1,numbers=none,xleftmargin=0.35cm,framesep=0mm}]
      #2
    \string\end{lstlisting}
  }
}
\ExplSyntaxOff

\begin{document}
\frame{
  \frametitle{Test code}
    \code{C}{
    int x = 200;
    unsigned int y = 0;
    }
}
\end{document}

Package Listings Warning: Text dropped after begin of listing on input line 110

由于。。。导致的结果

\lstnewenvironment{code}[1]{\lstset{language=#1,numbers=none,xleftmargin=0.35cm,framesep=0mm}}{}

\begin{document}
\frame{
  \frametitle{Test code}
    \begin{code}{C}
    int x = 200;
    unsigned int y = 0;
    \end{code}
}
\end{document}

相关内容