12

I'm new to LaTeX and wand to add some code with minted, which will only be rendered, when a \boolean variable is true. Here's my code:

\documentclass{article}

\usepackage {minted}
\usepackage {ifthen}

\newboolean{printCode}
\setboolean{printCode}{true}

\begin{document}
\ifthenelse{\boolean{printCode}}{
\begin{minted}{c}
// comment
int i = 1;
\end{minted}
}{}
\end{document}

This causes an error:

! FancyVerb Error:
  Extraneous input ` // comment int i = 1; \end {minted} ' between \begin{minte
d}[<key=value>] and line end
.
\FV@Error ... {FancyVerb Error:
\space \space #1
}

l.15 }{}

When I remove the \ifthenelse part, it works fine. Also if printCode is false, it works (code is not printed). So the question is: What am I doing wrong here?

Thanks! Ingo.

1 Answer 1

9

You can't have minted environments as arguments to other commands: this is a limitation for every “verbatim” kind of things, so verbatim, lstlisting and minted.

You can avoid the argument using a more basic syntax:

\documentclass{article}

\usepackage {minted}
\usepackage {ifthen}

\newboolean{printCode}
\setboolean{printCode}{true}

\begin{document}
Text to be printed so there's output even if the \verb|\setboolean|
line is commented out.

\ifprintCode
\begin{minted}{c}
// comment
int i = 1;
\end{minted}
\fi

\end{document}
5
  • That's a better solution. I'll delete my answer.
    – jub0bs
    Apr 10, 2014 at 12:44
  • @Jubobs Your answer is a good alternative when the code is in external files, which it frequently is: you can define a \perhapsinputminted command that takes as argument just the file name.
    – egreg
    Apr 10, 2014 at 12:47
  • Thanks! Also the package 'ifthen' isn't needed any more. Apr 10, 2014 at 12:58
  • @IngoDellwig Well, no: just \newif\ifprintCode is sufficient. But the interface is cleaner.
    – egreg
    Apr 10, 2014 at 13:01
  • Thank you -- this has saved my camera-ready vs tech-report woes!
    – Savara
    Jun 22, 2017 at 15:19

You must log in to answer this question.

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