如何在 newcommand 中运行 minted listing

如何在 newcommand 中运行 minted listing

我尝试将我的列表封装到一个更大的项目中。如果我单独运行,一切都很好:

%LISTINGS
\documentclass[a4paper, 12pt]{scrartcl}

%USINGS
\usepackage{xcolor}
\usepackage{minted}
\usemintedstyle{borland}

%FILL TEXT
\usepackage{lipsum}


%--------------------------------------------------------------- STANDARDAUFBAU EINES SKRIPTES
\begin{document}
\begin{listing}[H]
\caption{Standardaufbau eines Skriptes}
\label{listing:Aufbau}
\begin{minted}[frame=lines, framesep=2mm, baselinestretch=1.0, bgcolor=white, fontsize=\footnotesize, linenos]{csharp}
//benötigte Usings
using System;
using System.IO;

namespace MyNamespace
{
    public class MyClass //Klassen definition
    {
        //skriptweite Variablen
        int a = ...


        public MyClass() //Klassenkonstruktor
        {
            //lokale Variablen
            int x = ...

            //auszuführendes Skript
            ...
        }
    }
}
\end{minted}
\end{listing}  
\end{document}

但在里面新命令

%LISTINGS
\documentclass[a4paper, 12pt]{scrartcl}

%USINGS
\usepackage{xcolor}
\usepackage{minted}
\usemintedstyle{borland}

%FILL TEXT
\usepackage{lipsum}


%--------------------------------------------------------------- STANDARDAUFBAU EINES SKRIPTES
\newcommand\listingAufbau{
\begin{listing}[H]
\caption{Standardaufbau eines Skriptes}
\label{listing:Aufbau}
\begin{minted}[frame=lines, framesep=2mm, baselinestretch=1.0, bgcolor=white, fontsize=\footnotesize, linenos]{csharp}
//benötigte Usings
using System;
using System.IO;

namespace MyNamespace
{
    public class MyClass //Klassen definition
    {
        //skriptweite Variablen
        int a = ...


        public MyClass() //Klassenkonstruktor
        {
            //lokale Variablen
            int x = ...

            //auszuführendes Skript
            ...
        }
    }
}
\end{minted}
\end{listing}
}

%--------------------------------------------------------------- DOCUMENT
\begin{document}
  \lipsum[1]  
  \listingAufbau
  \lipsum[2]
\end{document}

错误!段落在 \FV@BeginScanning 完成之前结束。发生。

我该如何解决这个问题?

错误日志:

Runaway argument?
 //benötigte Usings using System; using System.IO; 
! Paragraph ended before \FV@BeginScanning was complete.
<to be read again> 
                   \par 
l.66   \listingAufbau

I suspect you've forgotten a `}', causing me to apply this
control sequence to too much text. How can we recover?
My plan is to forget the whole thing and hope for the best.

! Bad space factor (0).
<recently read> \@savsf 

l.66   \listingAufbau

I allow only values in the range 1..32767 here.

(_minted-Listings/default-pyg-prefix.pygstyle)
(_minted-Listings/borland.pygstyle)
runsystem(pygmentize -l csharp -f latex -P commandprefix=PYG -F tokenmerge -o _
minted-Listings/D41D8CD98F00B204E9800998ECF8427EC151BAE480938F0991A0CCB99EA97B2
E.pygtex Listings.pyg)...executed.


(_minted-Listings/D41D8CD98F00B204E9800998ECF8427EC151BAE480938F0991A0CCB99EA97
B2E.pygtex

! LaTeX Error: Something's wrong--perhaps a missing \item.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.3 \end{Verbatim}

Try typing  <return>  to proceed.
If that doesn't work, type  X <return>  to quit.


! LaTeX Error: Something's wrong--perhaps a missing \item.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.3 \end{Verbatim}

Try typing  <return>  to proceed.
If that doesn't work, type  X <return>  to quit.

! Missing } inserted.
<inserted text> 
                }
l.3 \end{Verbatim}

I've inserted something that you may have forgotten.
(See the <inserted text> above.)
With luck, this will get me unwedged. But if you
really didn't forget anything, try typing `2' now; then
my insertion and my current dilemma will both disappear.

! Missing } inserted.
<inserted text> 
                }
l.3 \end{Verbatim}

I've inserted something that you may have forgotten.
(See the <inserted text> above.)
With luck, this will get me unwedged. But if you
really didn't forget anything, try typing `2' now; then
my insertion and my current dilemma will both disappear.


Underfull \hbox (badness 10000) detected at line 3
[][]
 []

! Missing } inserted.
<inserted text> 
                }
l.3 \end{Verbatim}

I've inserted something that you may have forgotten.
(See the <inserted text> above.)
With luck, this will get me unwedged. But if you
really didn't forget anything, try typing `2' now; then
my insertion and my current dilemma will both disappear.

! Extra }, or forgotten \endgroup.
\FV@ListProcessLine ...Frame \FV@RightListNumber }
                                                  \hss }
l.3 \end{Verbatim}

I've deleted a group-closing symbol because it seems to be
spurious, as in `$x}$'. But perhaps the } is legitimate and
you forgot something else, as in `\hbox{$x}'. In such cases
the way to recover is to insert both the forgotten and the
deleted material, e.g., by typing `I$}'.

! Extra }, or forgotten \endgroup.
\FV@ListProcessLine ...\FV@RightListNumber }\hss }

l.3 \end{Verbatim}

I've deleted a group-closing symbol because it seems to be
spurious, as in `$x}$'. But perhaps the } is legitimate and
you forgot something else, as in `\hbox{$x}'. In such cases
the way to recover is to insert both the forgotten and the
deleted material, e.g., by typing `I$}'.

)
Runaway argument?
! File ended while scanning use of \FancyVerbGetLine.
<inserted text> 
                \par 
l.66   \listingAufbau

I suspect you have forgotten a `}', causing me
to read past where you wanted me to stop.
I'll try to recover; but if the error is serious,
you'd better type `E' or `X' now and fix your file.

! FancyVerb Error:
  Couldn't find `\end{minted}' to end a verbatim environment on input line 66.
.
\FV@Error ... {FancyVerb Error:
\space \space #1
}

l.66   \listingAufbau

Probably you mistyped the environment name or included an extraneous 
space, or are using an improperly defined verbatim environment. 
Hit return and I will try to terminate this job.


! LaTeX Error: \begin{minted@colorbg} on input line 66 ended by \end{VerbatimOu
t}.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.66   \listingAufbau

Your command was ignored.
Type  I <command> <return>  to replace it with another command,
or  <return>  to continue without it.

runsystem(pygmentize -l csharp -f latex -P commandprefix=PYG -F tokenmerge -o _
minted-Listings/D41D8CD98F00B204E9800998ECF8427E57E4147185E2DF96167E69AACD75483
B.pygtex Listings.pyg)...executed.

! Infinite glue shrinkage found in a paragraph.
\MakeFramed #1->\par 
                     \ifdim \OuterFrameSep =\maxdimen \OuterFrameSep \topsep...
l.66   \listingAufbau

The paragraph just ended includes some glue that has
infinite shrinkability, e.g., `\hskip 0pt minus 1fil'.
Such glue doesn't belong there---it allows a paragraph
of any length to fit on one line. But it's safe to proceed,
since the offensive shrinkability has been made finite.


(_minted-Listings/D41D8CD98F00B204E9800998ECF8427E57E4147185E2DF96167E69AACD754
83B.pygtex)
runsystem(del _minted-Listings\9244FD39800C89051BC398AC05508E1BC151BAE480938F09
91A0CCB99EA97B2E.pygtex)...executed.



! LaTeX Error: \begin{listing} on input line 66 ended by \end{document}.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.66   \listingAufbau

Your command was ignored.
Type  I <command> <return>  to replace it with another command,
or  <return>  to continue without it.

(Listings.aux)
runsystem(del Listings.pyg)...executed.


! You can't use `\end' in internal vertical mode.
\enddocument ...cumenthook \deadcycles \z@ \@@end 

l.66   \listingAufbau

Sorry, but I'm not programmed to handle this case;
I'll just pretend that you didn't ask for it.
If you're in the wrong mode, you might be able to
return to the right one by typing `I}' or `I$' or `I\par'.


! LaTeX Error: \begin{listing} on input line 66 ended by \end{document}.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.66   \listingAufbau

Your command was ignored.
Type  I <command> <return>  to replace it with another command,
or  <return>  to continue without it.

! Missing } inserted.
<inserted text> 
                }
l.66   \listingAufbau

I've inserted something that you may have forgotten.
(See the <inserted text> above.)
With luck, this will get me unwedged. But if you
really didn't forget anything, try typing `2' now; then
my insertion and my current dilemma will both disappear.


Package framed Warning: Boxa is void -- discard it.  on input line 66.

! You can't use `\prevdepth' in horizontal mode.
\nointerlineskip ->\prevdepth 
                              -\@m \p@ 
l.66   \listingAufbau

Sorry, but I'm not programmed to handle this case;
I'll just pretend that you didn't ask for it.
If you're in the wrong mode, you might be able to
return to the right one by typing `I}' or `I$' or `I\par'.

! Missing $ inserted.
<inserted text> 
                $
l.66   \listingAufbau

I've inserted a begin-math/end-math symbol since I think
you left one out. Proceed, with fingers crossed.

LaTeX Font Info:    External font `cmex10' loaded for size
(Font)              <12> on input line 66.
LaTeX Font Info:    External font `cmex10' loaded for size
(Font)              <8> on input line 66.
LaTeX Font Info:    External font `cmex10' loaded for size
(Font)              <6> on input line 66.
! Missing number, treated as zero.
<to be read again> 
                   \hbox 
l.66   \listingAufbau

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 unit of measure (pt inserted).
<to be read again> 
                   \hbox 
l.66   \listingAufbau

Dimensions can be in units of em, ex, in, pt, pc,
cm, mm, dd, cc, nd, nc, bp, or sp; but yours is a new one!
I'll assume that you meant to say pt, for printer's points.
To recover gracefully from this error, it's best to
delete the erroneous units; e.g., type `2' to delete
two letters. (See Chapter 27 of The TeXbook.)

! Missing $ inserted.
<inserted text> 
                $
l.66   \listingAufbau

I've inserted a begin-math/end-math symbol since I think
you left one out. Proceed, with fingers crossed.

Missing character: There is no è in font cmex10!

! LaTeX Error: \begin{listing} on input line 66 ended by \end{snugshade*}.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.66   \listingAufbau

Your command was ignored.
Type  I <command> <return>  to replace it with another command,
or  <return>  to continue without it.


! LaTeX Error: \begin{document} ended by \end{minted@colorbg}.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.66   \listingAufbau

Your command was ignored.
Type  I <command> <return>  to replace it with another command,
or  <return>  to continue without it.

! Extra \endgroup.
<recently read> \endgroup 

l.66   \listingAufbau

Things are pretty mixed up, but I think the worst is over.


! LaTeX Error: \begin{document} ended by \end{minted}.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.66   \listingAufbau

Your command was ignored.
Type  I <command> <return>  to replace it with another command,
or  <return>  to continue without it.

! Extra \endgroup.
<recently read> \endgroup 

l.66   \listingAufbau

Things are pretty mixed up, but I think the worst is over.

! Too many }'s.
\@endfloatbox ...pagefalse \outer@nobreak \egroup 
                                                  \color@endbox 
l.66   \listingAufbau

You've closed more groups than you opened.
Such booboos are generally harmless, so keep going.

[1

{C:/Users/Martin/AppData/Local/MiKTeX/2.9/pdftex/config/pdftex.map}]
! Extra \endgroup.
\color@endgroup ->\endgraf \endgroup 

l.66   \listingAufbau

Things are pretty mixed up, but I think the worst is over.

! Too many }'s.
\color@endbox ->\color@endgroup \egroup 

l.66   \listingAufbau

You've closed more groups than you opened.
Such booboos are generally harmless, so keep going.

! Undefined control sequence.
\endlisting ...floatbox \global \setbox \@currbox 
                                                  \float@makebox \columnwidt...
l.66   \listingAufbau

The control sequence at the end of the top line
of your error message was never \def'ed. If you have
misspelled it (e.g., `\hobx'), type `I' and the correct
spelling (e.g., `I\hbox'). Otherwise just continue,
and I'll forget about whatever was undefined.

! Missing number, treated as zero.
<to be read again> 
                   \vbox 
l.66   \listingAufbau

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.)

! Undefined control sequence.
\float@makebox ...ze =#1 \@parboxrestore \@fs@pre 
                                                  \@fs@iftopcapt \ifvoid \@f...
l.66   \listingAufbau

答案1

实际上不可能在命令的参数中嵌入逐字环境。通过将代码存储在环境中,可以获得非常相似的效果filecontents*,该环境将写入使用\inputminted.

\documentclass[a4paper, 12pt]{scrartcl}

\usepackage{xcolor}
\usepackage{minted}
\usemintedstyle{borland}

\usepackage{lipsum}


\newcommand\listingAufbau{%
  \begin{listing}[H]
  \caption{Standardaufbau eines Skriptes}
  \label{listing:Aufbau}
  \inputminted[
    frame=lines,
    framesep=2mm,
    baselinestretch=1.0,
    bgcolor=white,
    fontsize=\footnotesize, linenos
  ]{csharp}{aufbau.cs}
  \end{listing}
}
\begin{filecontents*}{aufbau.cs}
//benötigte Usings
using System;
using System.IO;

namespace MyNamespace
{
    public class MyClass //Klassen definition
    {
        //skriptweite Variablen
        int a = ...


        public MyClass() //Klassenkonstruktor
        {
            //lokale Variablen
            int x = ...

            //auszuführendes Skript
            ...
        }
    }
}
\end{filecontents*}

\begin{document}

\lipsum[1]  

\listingAufbau

\lipsum[2]

\end{document}

在此处输入图片描述

相关内容