0

How can I make such an environment:

\documentclass[a4paper,12pt]{report}
\usepackage{minted,fancyvrb}
\newenvironment{mycode}[1]{
\begin{listing}
    \begin{minted}{bash}}{
    \end{minted}
    \caption{#1}
\end{listing}}

\begin{document}
    \begin{mycode}{My title}
        echo $PATH
    \end{mycode}
\end{document}

I tried to do it this way:

\documentclass[a4paper,12pt]{report}
\usepackage{minted,fancyvrb}
\newminted{bash}{}
\newenvironment{mysupercode}{\VerbatimEnvironment\begin{bashcode}}{\end{bashcode}}
\newenvironment{mycode}[1]{
    \VerbatimEnvironment\begin{listing}\begin{mysupercode}}{
        \end{mysupercode}\caption{#1}\end{listing}}

\begin{document}
    \begin{mycode}{My title}
        echo $PATH
    \end{mycode}
\end{document}

But got the following error:

Illegal parameter number in definition of \endmycode. \end{mysupercode}\caption{#1}\end{listing}}

1 Answers1

1

You can't have parameter substitution (#1) in the \end part of the environment. You could, in the \begin part do \def\mysupercodecaption{#1} and then use \caption{\mysupercodecaption} later, like this:

\documentclass[a4paper,12pt]{report}
\usepackage{minted,fancyvrb}
\newminted{bash}{}
\newenvironment{mysupercode}{\VerbatimEnvironment\begin{bashcode}}{\end{bashcode}}
\newenvironment{mycode}[1]{%
  \def\mysupercodecaption{#1}%
  \VerbatimEnvironment
  \begin{listing}
    \begin{mysupercode}%
}{%
    \end{mysupercode}%
    \caption{\mysupercodecaption}%
  \end{listing}%
}

\begin{document}
    \begin{mycode}{My title}
        echo $PATH
    \end{mycode}
\end{document}

This doesn't work the way you wanted to because the \newenvironment command basically replaces this:

\newenvironment{mycode}[1]{<begin-part>}{<end-part>}

by

\newcommand{\mycode}[1]{<begin-part>}
\newcommand{\endmycode}{<end-part>}

thus the \endmycode macro doesn't take arguments. This is mainly because if the \endmycode macro was defined to take an argument, you would have to use it like this:

\begin{mycode}
  ...
\end{mycode}{<argument>}