[Tex/LaTex] Defining a command for a minted environment

environmentsmacrosminted

How can I define a newcommand macro or an environment for minted?
What is causing the problem that the following does not work?

\newcommand{\CppSourceCode}{
\begin{minted}[linenos=true, mathescape, xleftmargin=1cm]{c++}
 #1 
\end{minted}
}

output :

    ! Missing number, treated as zero.
<to be read again> 
                   }
l.23 ...pe, xleftmargin=1cm]{c++} #1 \end{minted}}
                                                  
A number should have been here; I inserted `0'.
(If you can't figure out why I needed to see a number,
look up `weird error' in the index to The TeXbook.)

! Illegal parameter number in definition of \CppSourceCode.
<to be read again> 
                   1
l.23 ...pe, xleftmargin=1cm]{c++} #1 \end{minted}}
                                                  
You meant to type ## instead of #, right?
Or maybe a } was forgotten somewhere earlier, and things
are all screwed up? I'm going to assume that you meant ##.

(C:\Users\ZfMGPU\Desktop\Xetextest\main\main.aux)
LaTeX Font Info:    Checking defaults for OML/cmm/m/it on input line 35.
LaTeX Font Info:    ... okay on input line 35.
LaTeX Font Info:    Checking defaults for T1/cmr/m/n on input line 35.
LaTeX Font Info:    ... okay on input line 35.
LaTeX Font Info:    Checking defaults for OT1/cmr/m/n on input line 35.
LaTeX Font Info:    ... okay on input line 35.
LaTeX Font Info:    Checking defaults for OMS/cmsy/m/n on input line 35.
LaTeX Font Info:    ... okay on input line 35.
LaTeX Font Info:    Checking defaults for OMX/cmex/m/n on input line 35.
LaTeX Font Info:    ... okay on input line 35.
LaTeX Font Info:    Checking defaults for U/cmr/m/n on input line 35.
LaTeX Font Info:    ... okay on input line 35.
LaTeX Font Info:    Checking defaults for EU1/lmr/m/n on input line 35.
LaTeX Font Info:    ... okay on input line 35.
runsystem(pygmentize -S default -f latex > main.pyg)...executed.

 (C:\Users\ZfMGPU\Desktop\Xetextest\main\main.pyg)
! Illegal parameter number in definition of \@tempa.
<to be read again> 
                   1
l.37 \CppSourceCode{asdasdasd asd asd }
                                       
You meant to type ## instead of #, right?
Or maybe a } was forgotten somewhere earlier, and things
are all screwed up? I'm going to assume that you meant ##.

! FancyVerb Error:
  Extraneous input ` ##1 \end {minted}{asdasdasd asd asd }' between \begin{minted}[<key=value>] and line end
.
\FV@Error ...ncyVerb Error:^^J\space \space #1^^J}
                                                  
l.37 \CppSourceCode{asdasdasd asd asd }
                                       
This input will be discarded. Hit <return> to continue.

If read that post here environment minted
Iam using xelatex in miktex 2.9
but is this the only solution?

Here one possibility:

Thats the minted command for having a new environment as in the package explained, probably should use this code !!

\newcommand\newminted[3][]{
      \ifthenelse{\equal{#1}{}}
       {\def\minted@envname{#2code}}
       {\def\minted@envname{#1}}
      \newenvironment{\minted@envname}
       {\VerbatimEnvironment\begin{minted}[#3]{#2}}
       {\end{minted}}
      \newenvironment{\minted@envname *}[1]
       {\VerbatimEnvironment\begin{minted}[#3,##1]{#2}}
       {\end{minted}}}

Does anybody know why this works? whats the key difference? why does mine not work?

Thanks a lot!!

Best Answer

See Martin's comment for the reason why your method doesn't work. See the minted manual, under "Defining shortcuts", for the \newminted command, which does what you want (but via an environment rather than a command):

\newminted[CppSourceCode]{c++}{linenos=true, mathescape, xleftmargin=1cm}

allows

\begin{CppSourceCode}
    c++ ; // goes here
\end{CppSourceCode}

If you prefer a command, use \newmint instead - see the manual for details.

Related Question