2

I want to conditionally load minted and define an environment, but after using IfFileExists I'm running into an error:

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}

The solution for embedding the minted environment inside another came from here.

The code I'm running is shown below:

\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}
4

Your \makeatletter should be before \IfFileExists and \makeatother after }{}

\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}

You want to use macros with @ in their name when you do the conditional definition, not when you use it.

  • A miracle... So why won't this work if I conditionally change the category code? – 1010011010 May 15 '14 at 12:17
  • @1010011010 As I said, you must enable usage of @-command before using them. Since those commands are in the argument to \IfFileExists, you need to have \makeatletter before the argument is grabbed. – egreg May 15 '14 at 12:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.