2

I am trying to create an environment that may or may not display source code with minted depending on an optional parameter. I did some tests and noticed that the following code does not work. The compiler says \begin{document} ended by \end{VerbatimOut}. What would be the problem?

\usepackage{verbatim} % for \comment
\usepackage{fancyvrb}
\usepackage{minted}

\newif\ifdisplaycode



\newenvironment{ccode}[1][]
{
    \IfSubStr{#1}{d}{\displaycodetrue}{\displaycodefalse}
    %\ifdisplaycodefalse

    \iftrue
        \comment
    \fi
        \VerbatimEnvironment
        \begin{minted}{julia}}
{
    \end{minted}
    \iftrue
        \endcomment
    \fi
}

\begin{ccode}[d]
   a=1
   b=2
\end{ccode}

1 Answer 1

1
\documentclass{article}

\usepackage{verbatim} % for \comment
\usepackage{fancyvrb}
\usepackage[cache=false]{minted}

\newif\ifdisplaycode


\makeatletter
\newenvironment{ccode}[1][]{%
    \IfSubStr{#1}{d}{\displaycodetrue}{\displaycodefalse}%
    \ifdisplaycode\expandafter\@secondoftwo\else\expandafter\@firstoftwo\fi
    {\comment}{%
      \VerbatimEnvironment
      \begin{minted}{julia}%
    }%
}{%
    \ifdisplaycode\expandafter\@secondoftwo\else\expandafter\@firstoftwo\fi
    {\endcomment}{\end{minted}}%
}%
\makeatother

\begin{document}

\begin{ccode}[d]
   a=1
   b=2
\end{ccode}

\end{document}
1
  • This is cool! I did not imagine such type of construction: ` \ifdisplaycode\expandafter\@secondoftwo\else\expandafter\@firstoftwo\fi`. Thank you a lot! Commented Jan 25, 2021 at 2:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .