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

  • Welcome to TeX.SX! Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. – BambOo Aug 21 '18 at 12:16
  • Welcome to TeX.SX! You can't have parameter substitution (#1) in the \end part of the environment. You could, in the \begin part do \def\thiscaption{#1} and then use \caption{\thiscaption} later. – Phelype Oleinik Aug 21 '18 at 12:16
0

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

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.