tags:

views:

64

answers:

2

I'm trying to make Latex usable, by introducing some timesavers, but I'm having trouble with defining new commands that terminate environments, completely at random.

This works:
\newcommand{\bcv}{\ensuremath{\begin{smallmatrix}}} \newcommand{\ecv}{\ensuremath{\end{smallmatrix}}} \newcommand{\be}{\begin{enumerate}}
\newcommand{\ee}{\end{enumerate}}

This does not work:
\newcommand{\bal}{\begin{align*}}
\newcommand{\eal}{\end{align*}}

\newcommand{\verbass}[1]{\begin{verbatim} #1 \end {verbatim}}

Specifically, I think the \end value is just ignored?

When I try to use \verbass{Halp} I get an error: !File ended while scanning use of \@xverbatim.

Obviously I can use \begin{foo} ... \end{foo} at all locations as needed, but really, this should work!

A: 

There's a very brief explanation here about verb and verbatim. Basically, LaTeX insists verb and verbatim get the first "look" at their contents.

hoyland
+1  A: 

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

  1. \begin{verbatim} is expanded to \verbatim.
  2. Then \verbatim sets category code of each characters to 11. Now all chars is letters.
  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{\verbass}[1]{\begin{verbatim} #1 \end {verbatim}} work.

  1. First of all \verbass{Halp} reads its argument.
  2. #1 --> Halp
  3. \verbass expands to \begin{verbatim} Halp \end {verbatim}. Important: backslash of \end has category 0 rather than 11. Moreover { and } have categories 1 and 2 rather than 11.
  4. Then \begin{verbatim} expands to \varbatim. \varbatim changes all categories and font. But (important) the category of backslash (in \end) remains equal to 0.
  5. Then \verbatim calls \@xverbatim.
  6. \@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 11. But in fact there are four letters with other category code: \ with category 0 and e,n,d with category 11.

  7. \@xverbatim is trying and trying to find \end where backslash (\) has category 11 but.... File ended while scanning use of \@xverbatim
Alexey Malistov
wow, ok thank you for this. Is there any way to persuade latex to understand that \end is indeed what it's supposed to be, i.e. force the \ to be of category 11?
gakera
yes. it is possible. But it is useless. All argument chars have their own category. `\verbass{ $_^# }` will not work. You should make something like `\verbatim` does.
Alexey Malistov
I .. don't understand, but ok. What about the other commands, why does \ee work fine for me but \eal not? Same if I split the verbatim into \begin and \end shorthands, the \end shorthand does not work :(
gakera