简体   繁体   中英

Latex \newcommand problem

can someone please tell what I'm doing wrong here?

\newcommand{\bc}{\small\begin{verbatim}}
\newcommand{\ec}{\normalsize\end{verbatim}}

and then

\bc
1  3       6 7 89 10
 22 7  7  45
\ec

but I get

Runaway argument?
^^M1  3       6 7 89 10^^M 22 7  7  45^^M\ec^^M^^M\section{Reading on\ETC.
! File ended while scanning use of \@xverbatim.
<inserted text> 
                \par 
<*> i4c.tex

? 
! Emergency stop.
<inserted text> 
                \par 
<*> i4c.tex

I thought it was pretty safe to do that, since most commands are just text substitutions... any hints?

How \\begin{verbatim} works. briefly and roughly.

  1. \\begin{verbatim} is expanded to \\verbatim .
  2. Then \\verbatim sets category code of each special characters to 12. Now all chars is like digits or puncts.
  3. Then \\verbatim sets font, parindent and calls \\@xverbatim .
  4. \\@xverbatim catches the end of verbatim using the following trick:

     \\def\\@xverbatim#1\\end{#1\\end} 
  5. Then \\end{verbatim} finishes work.

How newcommand{\\bc}{\\small\\begin{verbatim}} works.

  1. \\bс expands to \\small\\begin{verbatim} .
  2. Then \\begin{verbatim} expands to \\varbatim . \\varbatim changes all categories and font.
  3. Then \\verbatim calls \\@xverbatim .
  4. \\@xverbatim tries to catch your argument using the following trick:

     \\def\\@xverbatim#1\\end{#1\\end} 

    but it is impossible because of \\@xverbatim tries to catch \\end where all letters ( \\ , e , n , d ) have the category 12 and 11. But in fact there are only \\ec exsits.

  5. \\@xverbatim is trying and trying to find \\end where backslash ( \\ ) has category 12 but.... File ended while scanning use of \\@xverbatim

verbatim是特殊的,它扫描文字 \\end{verbatim} ,因为在\\begin{verbatim}之后没有执行任何宏替换。

Some environment, such as verbatim , need to scan in the text ahead to find their end manually. So unlike “normal” environments, verbatim needs to find the text \\end{verbatim} in the input text, verbatim.

In your example, it doesn't (since the input text contains \\ec instead.

As a workaround, you can instead use the fancyvrb package that defines a Verbatim package and allows definition custom verbatim environments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM