Latex \newcommand for \end{verbatim} et.al not working - latex

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!

How \begin{verbatim} works. briefly and roughly.
\begin{verbatim} is expanded to \verbatim.
Then \verbatim sets category code of each characters to 11.
Now all chars is letters.
Then \verbatim sets font, parindent and calls \#xverbatim.
\#xverbatim catches the end of verbatim using the following trick:
\def\#xverbatim#1\end{#1\end}
Then \end{verbatim} finishes work.
How \newcommand{\verbass}[1]{\begin{verbatim} #1 \end {verbatim}} work.
First of all \verbass{Halp} reads its argument.
#1 --> Halp
\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.
Then \begin{verbatim} expands to \varbatim.
\varbatim changes all categories and font. But (important)
the category of backslash (in \end) remains equal to 0.
Then \verbatim calls \#xverbatim.
\#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.
\#xverbatim is trying and trying to find \end where backslash (\) has category 11 but.... File ended while scanning use of \#xverbatim

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

I am far away from being a professional programmer, but a quick-and-dirty-solutions could be:
\newcommand{\myverbatim}[1]{\begin{tt} \detokenize{#1} \end{tt} \\ }
The output is not very nice, because linebreaks are ignored. If this is desired one could repeat the command line-by-line (?).

Related

user defined commands in verbatim environment

is it possible to create a new command with a parameter in a verbatim environment and some indentation?
\newcommand{codeblock}[1]{\begin{quote}\begin{verbatim}#1\end{verbatim}\end{quote}}
This does not work. Is there an other way?
How \begin{verbatim} works. briefly and roughly.
\begin{verbatim} is expanded to \verbatim.
Then \verbatim sets category code of each special characters to 12.
Now all chars is like digits or puncts.
Then \verbatim sets font, parindent and calls \#xverbatim.
\#xverbatim catches the end of verbatim using the following trick:
\def\#xverbatim#1\end{#1\end}
Then \end{verbatim} finishes work.
How \newcommand{\codeblock}[1]{\begin{quote}\begin{verbatim}#1\end{verbatim}\end{quote}} works.
First of all \codeblock{Some {}$&%^_} reads its argument.
#1 --> Some code {}$&%^_
Note: {,},$,&,%,^,_ have categories 1,2,3,4,6,7,8 rather than 11 or 12!!!)
\codeblock expands to \begin{quote}\begin{verbatim} Some {}$&%^_\end {verbatim}\end {quote}.
Important: backslash of \end has category 0 rather than 11.
Moreover { and } have categories 1 and 2 rather than 11.
And $,&,%,^,_ have categories 3,4,6,7,8.
\begin{quote} expands to \quote and \quote executes.
Then \begin{verbatim} expands to \varbatim.
\varbatim changes all categories and font. But (important)
the category of backslash (in \end) remains equal to 0. And the categories of {, }, $, &, %, ^, _ typed after Some remains because of "argument reading" executes before \verbatim changes all categories. But you need that all char has categories 11 o 12.
Then \verbatim calls \#xverbatim.
\#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 four letters with other category code:
\ with category 0 and e,n,d with category 11.
It is possible (I am not sure) that trick is more masterly:
Red chars have category 12. Green chars have category 11.
\def, \#xverbatim, \end are macros with \ (category 0) and letters (category 11).
\#xverbatim is trying and trying to find \end where backslash (\) has category 11 but.... File ended while scanning use of \#xverbatim
If you want to create new macro \codeblock you must do something like above text.
It looks like you want to have code in your document, in which case you're probably better served by the package listings than by verbatim. Listings also gives you nice features like line numbering and syntax highlighting for many common languages. See http://www.ctan.org/tex-archive/macros/latex/contrib/listings/ if it's not already installed with your LaTeX distribution.

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.
\begin{verbatim} is expanded to \verbatim.
Then \verbatim sets category code of each special characters to 12.
Now all chars is like digits or puncts.
Then \verbatim sets font, parindent and calls \#xverbatim.
\#xverbatim catches the end of verbatim using the following trick:
\def\#xverbatim#1\end{#1\end}
Then \end{verbatim} finishes work.
How newcommand{\bc}{\small\begin{verbatim}} works.
\bс expands to \small\begin{verbatim}.
Then \begin{verbatim} expands to \varbatim.
\varbatim changes all categories and font.
Then \verbatim calls \#xverbatim.
\#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.
\#xverbatim is trying and trying to find \end where backslash (\) has category 12 but.... File ended while scanning use of \#xverbatim
verbatim is special, it scans for a literal \end{verbatim}, as any macro substitutions are not executed after the \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.

how write this in verbatim latex

i have a little question about the verbatim environment in latex.....
my problem is how can i write "\end{verbatim}" inside of a verbatim environment, it supposed that everything inside a verbatim is written just like it looks but this is the string to end the environment, so how can i do this?
\\end{verbatim}
Should do it by making the initial \ into a literal backslash.
I think it might be impossible in a verbatim environment -- at least, that's what Lamport says in the LaTeX book (first paragraph on pg. 64). However, you can do it with the \verb command: \verb+\end{verbatim}+ works just fine (and in fact, you can use any character instead of + as long as you make them match).
That said, the listings package is smart enough to have an escape character. For example,
\begin{lstlisting}[escapechar=+]
+\verb!\end{lstlisting}!+
\end{lstlisting}
does exactly what you're looking for. (Again, pick a different character than + if you're already using that for something else.)
Use the fancyvrb package instead of the default verbatim package.
Craig's answer is good. Alternatively, \expandafter\string\csname end{verbatim}\endcsname.
These solutions won't linebreak nicely: fixing this is fiddly.
I have a simpler solution. Write a non-breaking space, in Emacs it's C-x 8 SPC (Windows, though I haven't tried it is Alt+0160 (on numeric keypad)). So, you can do this:
\begin{verbatim}
\_end{verbatim}
\end{verbatim}
where the first \end{verbatim} has this "_", the non-breaking character.
Here is my method, thanks #S.Lott for his advice.
If you want to write \end{verbatim} in the verbatim environment, you have to using another 'verbatim' layer to nest your code (this is where fancyvr package comes to play). Here it is:
\documentclass{article}
\usepackage{fancyvrb}
\begin{document}
Some text here
\begin{Verbatim} % note 'uppercase V' in verbatim
\begin{verbatim}
Code example
\end{verbatim}
\end{Verbatim}
\end{document}

Triple-wrapping of \colorbox → \NewEnviron → \newenvironment fails

I am trying to wrap an environment created with \NewEnviron (package 'environ') into an old good \newenvironment:
\NewEnviron{test}{\colorbox[gray]{0.7}{\BODY}}
\newenvironment{wrapper}{\begin{test}}{\end{test}}
\begin{wrapper}
debug me
\end{wrapper}
However, this gives me a strange error:
LaTeX Error: \begin{test} on input line 15 ended by \end{wrapper}.
LaTeX Error: \begin{wrapper} on input line 15 ended by \end{document}.
If I replace \NewEnviron{test}{aaa(\BODY)bbb} with \newenvironment{test}{aaa(}{)bbb} — everything works as expected! It seems like \NewEnviron fails to find its end for some reason.
I'm trying to do some magic with 'floatfig' wrapped into a \colorbox so I need a way to convert \colorbox to an environment and wrap it into another one. I can define a new command but it's not a very good idea.
The thing is that \NewEviron and \newenvironment works in different ways.
1) \newenvironment{test}{aaa(}{)bbb} defines two commands: \test is aaa( and \endtest is )bbb.
\begin{test} is expanded to \test.
\end{test} is expanded to \endtest and checks that your scope begins with begin{test} rather \begin{something else}, for example \begin{wrapper}.
2) \NewEviron{test}{aaa(\BODY)bbb} defines \test in different way. First of all \test catches the \BODY using the following trick
\def\test#1\end{\def\BODY{#1}aaa(\BODY)bbb\testcontinue}
(name \testcontinue may be different) and inserts aaa(\BODY)bbb. Then \testcontinue checks that \end on some input line ended by \end{test} rather than \end{something else}. Macro \endtest
is not needed because it is never executed.
Look on your code:
\begin{wrapper}
debug me
\end{wrapper}
\begin{wrapper} is expanded to \begin{test}. Then
\begin{test} is expanded to \test. \test catch \BODY.
Attention! \BODY is equal to debug me. And now \testcontionue checks
that \end after \BODY ended by \end{test}. It is not true. \end{test} is absent.
There is \end{wrapper}.
You want to say that \end{wrapper} must be expanded to \end{test}. But \end before wrapper was eaten by
macro \test: #1\end{\def\BODY{#1}aaa(\BODY)bbb\testcontinue}
and can not be executed.
I hope I success to explain.
I've found a hacky trick to create an environment that can be wrapped in another one. One should use saveBoxes like this:
\newenvironment{example}[2][]{%
\newsavebox{\exampleStore} % Box storage
\begin{lrbox}{\exampleStore} % Start capturing the input
}{%
\end{lrbox} % Stop capturing the input
\colorbox[gray]{0.7}{%
\usebox{\NBstorage} % Load the box's contents
}%
}%
}%

Suppress indentation after environment in LaTeX

I'm trying to create a new environment in my LaTeX document where indentation in the next paragraph following the environment is suppressed.
I have been told (TeXbook and LaTeX source) that by setting \everypar to {\setbox0\lastbox}, the TeX typesetter will execute this at the beginning of the next paragraph and thus remove the indentation:
\everypar{\setbox0\lastbox}
So this is what I do, but to no effect (following paragraph is still indented):
\newenvironment{example}
{\begin{list}
{}
{\setlength\leftmargin{2em}}}
{\end{list}\everypar{\setbox0\lastbox}}
I have studied LaTeX's internals as well as I could manage. It seems that the \end routine says \endgroup and \par at some point, which may be the reason LaTeX ignores my \everypar setting. \global doesn't help either. I know about \noindent but want to do this automatically.
Example document fragment:
This is paragraph text. This is paragraph text, too.
\begin{example}
\item This is the first item in the list.
\item This is the second item in the list.
\end{example}
This is more paragraph text. I don't want this indented, please.
Internal routines and switches of interest seem to be \#endpetrue, \#endparenv and others. Thanks for your help.
I couldn't get anything to work without redefining \end, but I'm certainly no expert.
The following is quite hacky, but worked in my limited testing. Of course this will interfere with nested environments (you should be able to redefine \begin to restore the old \end if you have problems).
\newenvironment{example}{%
\bgroup
\let\oldend=\end
\def\end##1{\oldend{##1}\csname #afterindentfalse\endcsname
\csname #afterheading\endcsname}
\begin{list}{}
{\setlength\leftmargin{2em}}
}{%
\end{list}
\egroup
}
Can't you avoid this by not having a blank line between your environment and the next line?
This is paragraph text. This is paragraph text, too.
\begin{example}
\item This is the first item in the list.
\item This is the second item in the list.
\end{example}
% (No blank line)
This is more paragraph text. I don't want this indented, please.
Something as simple as this works for me:
\makeatletter
\newenvironment{example}{%
\bgroup
\list{}{}
}{%
\endlist
\#afterindentfalse
\#afterheading
\egroup
}
\makeatother
But, it doesn't work before the first \section (or \chapter, in the case of classes "book" and "report") is called. I don't know why.
I tried the Ivan's answer, but it wasn't working for me. But I did get it working! Here's what I did:
\makeatletter
\renewenvironment{quotation}{%
\bgroup%
\let\oldend=\end%
\def\end##1{\oldend{##1}\csname #afterindentfalse\endcsname%
\csname #afterheading\endcsname}%
\list{}{\listparindent 1.5em%
\itemindent \listparindent%
\leftmargin 1.5em% This controls the size of the indentation
\rightmargin \leftmargin
\parsep \z# \#plus\p#}% This line reduces inter-paragraph space to normal values.
\item\relax%
}{%
\endlist%%
\egroup%
}
\makeatother
The advantage to this is that it typesets your blockquotes very nicely, and removes the indentation from paragraph after the blockquote.
You can do this without redefining \end
\makeatletter
\newenvironment{example}
{\begin{list}
{}
{\setlength\leftmargin{2em}}}
{\end{list}%
\def\if#endpe{%
\#doendpe
\let\par\##par
\iffalse}}
\makeatother
Explanation
\end changes \everypar after expanding \endexample. To make things even more complicated it sets \par to restore \everypar{}. Appearently \#doendpe is ment to make sure that there is no indentation if the paragraph continues after the environment, but to restore normal behavior if there is a \par (or empty line) after the environment.
You may want to avoid changing \end because it would have to be changed at the begining of the environment and may therefore disturb nested environments. Luckily the definition of \end contains \expandafter\endgroup\if#endpe. We can use \if#endpe as a hook to inject our code to the outer scope. After the \endgroup \if#endpe is automatically restored.
Include \#afterindentfalse\#afterheading at the end of your definition.
I had the same problem. I just used this:
\noindent \newenvironment
You should not mess with the \everypar token list, unless you know exactly what you are doing. Use
\setlength{\parindent}{0pt}
to get rid of indenting in the whole document.
ending your environment with \noindent could help you

Resources