5

The following LaTeX manuscript

\documentclass{article}
\usepackage{xparse,minted}
\NewDocumentEnvironment{MyMinted}{}
  {\begin{minted}[escapeinside=||,mathescape=true]{c}}
  {\end{minted}}
\begin{document}
\begin{MyMinted} 
  double diff(double (*f)(double), double |$x_0$|) {
      return |$\frac{d(*f)}{dx}(x_0)$|;
  }
\end{MyMinted}
\end{document}

produces (upon compilation with pdflatex -shell-escape) the following error message:

Runaway argument?
! File ended while scanning use of \FancyVerbGetLine.
<inserted text> 
                \par

What's the problem, and how can I fix it?

Evan Aad
  • 11,066
  • You know that it's a bad idea to wrap verbatim environments, don't you? – TeXnician Nov 07 '17 at 11:44
  • You can't hide verbatim-like environments inside other environment. – Ulrike Fischer Nov 07 '17 at 11:45
  • @UlrikeFischer: I don't understand what this means, and how it relates to my code. – Evan Aad Nov 07 '17 at 11:45
  • 2
    minted has to do a lot of catcode magic and needs to find the end command verbatim. Use e.g. the \newminted command to create a new environment, not \newenvironment or something similar. See section "Defining shortcuts" in the docu. – Ulrike Fischer Nov 07 '17 at 11:46
  • 1
    inside minted (like inside verbatim) \ does not start a tex command it is just a literal character so \end{MyMinted} does not end the environment, minted is looking for the literal string \end{minted} – David Carlisle Nov 07 '17 at 11:48
  • @UlrikeFischer: Could you please spell out what I need to write, because I can't figure it out based on the documentation section "Defining shortcuts". – Evan Aad Nov 07 '17 at 11:53
  • actually after rereading your question I think you want \VerbatimEnviromment, not \newminted. I will add an answer. – Ulrike Fischer Nov 07 '17 at 12:01
  • 1
    What's the drawback with \newminted? I see none. Use \newminted[MyMinted]{c}{escapeinside=||,mathescape=true} – egreg Nov 07 '17 at 12:53
  • 1
    @EvanAad Works flawlessly for me – egreg Nov 07 '17 at 13:44
  • @egreg: Sorry, you're right. I've tried it again, and it does work. You should write it as an answer. – Evan Aad Nov 07 '17 at 13:58

1 Answers1

5

How to add new minted environments is described in the section "Defining shortcuts" in the minted documentation. For you this means:

\documentclass{article}
\usepackage{xparse,minted}


\newenvironment{MyMinted}{\VerbatimEnvironment\begin{minted}[escapeinside=||,mathescape=true]{c}}{\end{minted}} 
\begin{document}

\begin{MyMinted}
  double diff(double (*f)(double), double |$x_0$|) {
      return |$\frac{d(*f)}{dx}(x_0)$|;
  }
\end{MyMinted}

\end{document}

enter image description here

Ulrike Fischer
  • 327,261