在 IfFileExists 中的新环境中使用 minted

在 IfFileExists 中的新环境中使用 minted

我想有条件地加载minted并定义一个环境,但使用后IfFileExists遇到错误:

FancyVerb Error:
  Extraneous input `resetoptions \setkeys {minted@opt}{frame=single} \begin {fi
gure}[htp] \centering \begin {minipage}{9cm} \begin {VerbatimOut}{\jobname .pyg
}' between \begin{Rcode}[<key=value>] and line end
.
\FV@Error ... {FancyVerb Error:
\space \space #1
}

l.35 \begin{Rcode}

将环境嵌入另一个环境的解决方案minted来自这里

我正在运行的代码如下所示:

\documentclass{article}

\usepackage{xcolor}

\IfFileExists{minted.sty}{
\usepackage{minted}
\usepackage{pgfkeys}
\pgfkeys{
    /terminal/.cd,
    caption/.code={\pgfkeyssetvalue{terminal/caption}{\caption{##1}}},
    label/.code={\pgfkeyssetvalue{terminal/label}{\label{##1}}},
}
\pgfkeyssetvalue{terminal/label}{}
\pgfkeyssetvalue{terminal/caption}{\caption{}}
\makeatletter
\newenvironment{Rcode}{%
  \VerbatimEnvironment
  \minted@resetoptions
  \setkeys{minted@opt}{frame=single}
  \begin{figure}[htp]
    \centering
    \begin{minipage}{9cm}    
      \begin{VerbatimOut}{\jobname.pyg}}
{%
      \end{VerbatimOut}
      \minted@pygmentize{r}
      \DeleteFile{\jobname.pyg}
    \end{minipage}
  \end{figure}}
\makeatother
}{}

\begin{document}
\begin{Rcode}
# Als je specifieke elementen wil aanhalen gebruik dan
c(1,2,3,7,10,15)
# Voor een rij van elementen gebruik
1:15
\end{Rcode}
\end{document}

答案1

\makeatletter应该在之前\IfFileExists\makeatother之后}{}

\documentclass{article}

\usepackage{xcolor}

\makeatletter
\IfFileExists{minted.sty}{
\usepackage{minted}
\usepackage{pgfkeys}
\pgfkeys{
    /terminal/.cd,
    caption/.code={\pgfkeyssetvalue{terminal/caption}{\caption{##1}}},
    label/.code={\pgfkeyssetvalue{terminal/label}{\label{##1}}},
}
\pgfkeyssetvalue{terminal/label}{}
\pgfkeyssetvalue{terminal/caption}{\caption{}}
\newenvironment{Rcode}{%
  \VerbatimEnvironment
  \minted@resetoptions
  \setkeys{minted@opt}{frame=single}
  \begin{figure}[htp]
    \centering
    \begin{minipage}{9cm}    
      \begin{VerbatimOut}{\jobname.pyg}}
{%
      \end{VerbatimOut}
      \minted@pygmentize{r}
      \DeleteFile{\jobname.pyg}
    \end{minipage}
  \end{figure}}
}{}
\makeatother

\begin{document}
\begin{Rcode}
# Als je specifieke elementen wil aanhalen gebruik dan
c(1,2,3,7,10,15)
# Voor een rij van elementen gebruik
1:15
\end{Rcode}
\end{document}

您希望@在进行条件定义时使用名称中带有 的宏,而不是在使用它时。

相关内容