Discussion:
How to use 'verbatim' environment when construct a new environment
(too old to reply)
iamsolo
2007-04-21 05:17:25 UTC
Permalink
Hi, all

I want to create a new environment called 'code' whose content is
smaller than normal text, and is verbatim. Intuitively, I can do
following:

\begin{small}
\begin{verbatim}
code code code...
\end{verbatim}
\end{small}


I want to make these two environment into one by writing:

\newenvironment{code}%
{\begin{small}\begin{verbatim} }%
{\end{verbatim}\end{small}}

But it seems LaTeX can not do such encapsulation. When use
\begin{code} and \end{code}, LaTeX reports 'verbatim' environment
encounters file end.

Anyone knows how to constructs such 'code' environment? Thanks!!


iamsolo
José Carlos Santos
2007-04-21 08:14:24 UTC
Permalink
Post by iamsolo
I want to create a new environment called 'code' whose content is
smaller than normal text, and is verbatim. Intuitively, I can do
\begin{small}
\begin{verbatim}
code code code...
\end{verbatim}
\end{small}
\newenvironment{code}%
{\begin{small}\begin{verbatim} }%
{\end{verbatim}\end{small}}
But it seems LaTeX can not do such encapsulation. When use
\begin{code} and \end{code}, LaTeX reports 'verbatim' environment
encounters file end.
Anyone knows how to constructs such 'code' environment? Thanks!!
Read the FAQ:

http://www.tex.ac.uk/cgi-bin/texfaq2html?label=verbwithin

Best regards,

Jose Carlos Santos
David R
2007-04-22 14:48:01 UTC
Permalink
Post by José Carlos Santos
Post by iamsolo
I want to create a new environment called 'code' whose content is
smaller than normal text, and is verbatim. Intuitively, I can do
\begin{small}
\begin{verbatim}
code code code...
\end{verbatim}
\end{small}
\newenvironment{code}%
{\begin{small}\begin{verbatim} }%
{\end{verbatim}\end{small}}
But it seems LaTeX can not do such encapsulation. When use
\begin{code} and \end{code}, LaTeX reports 'verbatim' environment
encounters file end.
Anyone knows how to constructs such 'code' environment? Thanks!!
http://www.tex.ac.uk/cgi-bin/texfaq2html?label=verbwithin
Best regards,
Jose Carlos Santos- Hide quoted text -
- Show quoted text -
The following is from verbatim.pdf

i.e. everything inside its body is ignored.
You may also add special commands after the \verbatim macro is
invoked,
e.g.
\newenvironment{myverbatim}%
{\verbatim\myspecialverbatimsetup}%
{\endverbatim}

so is
You must not use the \begin or the \end command inside a definition,
e.g. the following two examples will not work:
\newenvironment{myverbatim}%
{\endgraf\noindent MYVERBATIM:%
\endgraf\begin{verbatim}}%
{\end{verbatim}}
\newenvironment{fred}
{\begin{minipage}{30mm}\verbatim}
{\endverbatim\end{minipage}}
If you try these examples, TEX will report a "runaway argument" error.
More
generally, it is not possible to use \begin. . . \end or the related
environments
in the definition of the new environment. Instead, the correct way to
define
this environment would be
\newenvironment{fred}
{\minipage{30mm}\verbatim}
{\endverbatim\endminipage}

try
\newenvironment{mine}%
{\begin{small}\verbatim} }%
{\endverbatim\end{small}}

Hope this helps
David R
Christian Ebert
2007-04-22 16:59:26 UTC
Permalink
Post by iamsolo
I want to create a new environment called 'code' whose content is
smaller than normal text, and is verbatim. Intuitively, I can do
\begin{small}
\begin{verbatim}
code code code...
\end{verbatim}
\end{small}
\small is not an environment.
Post by iamsolo
\newenvironment{code}%
{\begin{small}\begin{verbatim} }%
{\end{verbatim}\end{small}}
\documentclass{article}
\usepackage{verbatim}% !!
\newenvironment{code}{\small\verbatim}{\endverbatim}
\begin{document}
\begin{code}
hello world
\end{code}

More text.
\end{document}

You might want to have a look at the listings package.

c
--
_B A U S T E L L E N_ lesen! --->> <http://www.blacktrash.org/baustellen.html>
David R
2007-04-22 17:38:34 UTC
Permalink
Post by Christian Ebert
\small is not an environment.
Good point.
I thought that something was a bit funny.
I assumed that verbatim was already loaded and that small must have
been created as an environment within his code
I should have done a proper minimal example, but I can't get emacs
working with gnus and the news server on tiscali habiutally doesn't
work, so I am using google and testing is a five minute cut and paste
job. too much hassle at the moment.
David R
Donald Arseneau
2007-04-23 03:56:32 UTC
Permalink
Post by David R
Post by Christian Ebert
\small is not an environment.
Good point.
Lousy point. \small can be used as an environment, and
the LaTeX manual says so (not specifically for \small).
--
Donald Arseneau ***@triumf.ca
Christian Ebert
2007-04-23 07:55:42 UTC
Permalink
Post by Donald Arseneau
Post by David R
Post by Christian Ebert
\small is not an environment.
Good point.
Lousy point.
heh
Post by Donald Arseneau
\small can be used as an environment, and
the LaTeX manual says so (not specifically for \small).
I stand corrected.

At least in the example of the OP, not using the environment
saves some typing, as \endverbatim closes the group ;)

c
--
LaTeX package to typeset drama with special verse support:
<http://www.blacktrash.org/cgi-bin/mercurial/dramatic/>
Donald Arseneau
2007-04-24 20:56:27 UTC
Permalink
Post by Christian Ebert
At least in the example of the OP, not using the environment
saves some typing, as \endverbatim closes the group ;)
So you suggest using a \small declaration inside the verbatim
environment? I hope not.

A useful internal command to redefine is \***@font.
--
Donald Arseneau ***@triumf.ca
David R
2007-04-24 22:06:43 UTC
Permalink
I have a slightly unrelated question about the verbatim package

\def\***@processline{\the\***@line\par}

typesets all the characters in the

\newtoks\***@line

Okay. now I am probably going to ask a stupid question, but is there any
way to expand some of those tokens in \***@line

take the contents elsewhere

\catcode`\'13 \def'{apost]

then put those back in
\***@newline{}
\***@addtocontents{\altered}
\***@processline

Sorry to waste your time if this is impossible.

I have tried hacking the verbatim package to make some catcodes active
between \verbatim@ and \verbatim@@
but there are too many internals happening for this to work. Missing
number then missing control sequence or \string\end{#1} or something
similar is reported. no textual substitutions in the output occur.

Is my assumption that once something is in the token list it is
impossible to expand it any further correct?

Still having fun.
David R
Enrico Gregorio
2007-04-24 22:56:39 UTC
Permalink
Post by David R
I have a slightly unrelated question about the verbatim package
typesets all the characters in the
Okay. now I am probably going to ask a stupid question, but is there any
take the contents elsewhere
\catcode`\'13 \def'{apost]
then put those back in
Sorry to waste your time if this is impossible.
I have tried hacking the verbatim package to make some catcodes active
but there are too many internals happening for this to work. Missing
number then missing control sequence or \string\end{#1} or something
similar is reported. no textual substitutions in the output occur.
Is my assumption that once something is in the token list it is
impossible to expand it any further correct?
The problem is that ' /is/ active in verbatim mode! That's done
in order to avoid combinations like '' triggering the ligature.

All you need is to change the definition of the active quote:

{\catcode`\'=\active
\gdef\doactivequoteinverbatim{\def'{<WHATEVER>}}}

\newenvironment{myverbatim}
{\verbatim\doactivequoteinverbatim}
{\endverbatim}

Do you see? The \verbatim command defines its version of the active
quote, but we reinstate the desired one afterwards.

I suspect that something will break, however. Try it.

Your statement about what's in a token list is incorrect: /category/
/codes/ of tokens already absorbed in a token list are immutable. The
meaning of control sequences can change till the last moment.

But, with e-TeX (which is used by standard distributions nowadays),
there is \scantokens ...

Ciao
Enrico
David R
2007-04-25 11:17:12 UTC
Permalink
Post by Enrico Gregorio
Post by David R
I have a slightly unrelated question about the verbatim package
typesets all the characters in the
Okay. now I am probably going to ask a stupid question, but is there any
take the contents elsewhere
\catcode`\'13 \def'{apost]
then put those back in
Sorry to waste your time if this is impossible.
I have tried hacking the verbatim package to make some catcodes active
but there are too many internals happening for this to work. Missing
number then missing control sequence or \string\end{#1} or something
similar is reported. no textual substitutions in the output occur.
Is my assumption that once something is in the token list it is
impossible to expand it any further correct?
The problem is that ' /is/ active in verbatim mode! That's done
in order to avoid combinations like '' triggering the ligature.
{\catcode`\'=\active
\gdef\doactivequoteinverbatim{\def'{<WHATEVER>}}}
\newenvironment{myverbatim}
{\verbatim\doactivequoteinverbatim}
{\endverbatim}
Do you see? The \verbatim command defines its version of the active
quote, but we reinstate the desired one afterwards.
I suspect that something will break, however. Try it.
Your statement about what's in a token list is incorrect: /category/
/codes/ of tokens already absorbed in a token list are immutable. The
meaning of control sequences can change till the last moment.
But, with e-TeX (which is used by standard distributions nowadays),
there is \scantokens ...
Ciao
Enrico
Thanks for the information.
I cannot find any mention of making \catcode`\'=\active in verbatim.sty
or even the latex source.

verbatim.sty
\def\@makeother#1{\catcode`#112\relax}

ltplain.dtx
Here is a list of the characters that have been specially catcoded:
13 \def\dospecials{\do\ \do\\\do\{\do\}\do\$\do\&%
14 \do\#\do\^\do\_\do\%\do\~}
(not counting ascii null, tab, linefeed, formfeed, return, delete) Each
symbol in
the list is preceded by , which can be defined if you want to do
something to every
item in the list.
We make @ signs act like letters, temporarily, to avoid conflict between
user
names and internal control sequences of plain format.
15 \catcode‘@=11

Some difficulty I have had was that \@sanitize seems to change the
catcode into 12, but I can't find any mention of what \do does except in
texcfg.sys
All others are \def\***@...
So \do on its own is a mystery to me.

ltdirchk.dtx
\def\***@d#1START^^M{\let\do\@makeother\dospecials\***@a}%
74 \catcode‘\%=12
75 \def\***@c{%END}
76 \***@d

ltdefns.dtx
\@sanitize The command \@sanitize changes the catcode of all special
characters except
for braces to ‘other’. It can be used for commands like \index that want
to write
their arguments verbatim. Needless to say, this command should only be
executed
within a group, or chaos will ensue.
277 \def\@sanitize{\@makeother\ \@makeother\\\@makeother\$\@makeother\&%
278 \@makeother\#\@makeother\^\@makeother\_\@makeother\%\@makeother\~}

NEVER MIND all this.
I tried what you suggested and it does work first time
Thanks
Further problems are controlling expansion and protection. I am not
very good at this.
Here is the problem

\documentclass{article}
\usepackage{verbatim}
\begin{document}
hello chuck
\makeatletter

{\catcode`\'=\active
\gdef\doactivequoteinverbatim{\let'\apostchar}}
\newenvironment{myverbatim}
{\verbatim\doactivequoteinverbatim}
{\endverbatim}

\def\apostchar{\ifx\protect\relax \expandafter\activeapost \else
\protect\apostchar\fi}

\def\activeapost{\ifx\protect\relax
\expandafter\afterassignment\expandafter\absorb\expandafter\let\expandafter\next=
\else\protect\activeapost}

\def\absorb{\ifx\protect \ifx\next=p next is a p\else next is not a p\fi
\expandafter\tryingtoprotectthree \else\protect\absorb}


\def\tryingtoprotectthree{\ifx\protect\relax \expandafter\laps \else
\protect\tryingtoprotectthree\fi}

\protected\def\laps{laps}

\begin{myverbatim}
This is original verbatim' environmemt and this apost is the answer
\end{myverbatim}

%\begin{verbatimwrite}{nancy.tex}
%Hello everybody this is a 'famous catchphrase.
%\end{verbatimwrite}
some more


\end{document}
Enrico Gregorio
2007-04-25 12:37:16 UTC
Permalink
Post by David R
Thanks for the information.
I cannot find any mention of making \catcode`\'=\active in verbatim.sty
or even the latex source.
[...]
Here is the problem
\documentclass{article}
\usepackage{verbatim}
\begin{document}
hello chuck
\makeatletter
{\catcode`\'=\active
\gdef\doactivequoteinverbatim{\let'\apostchar}}
\newenvironment{myverbatim}
{\verbatim\doactivequoteinverbatim}
{\endverbatim}
\def\apostchar{\ifx\protect\relax \expandafter\activeapost \else
\protect\apostchar\fi}
\def\activeapost{\ifx\protect\relax
\expandafter\afterassignment\expandafter\absorb\expandafter
\let\expandafter\next= \else\protect\activeapost}
AFAICT, this sequence of \expandafter "expands" the equal sign, not
the \else.

Ciao
Enrico
David R
2007-04-26 10:39:45 UTC
Permalink
I am still stuck with this problem
in the thread which began 17/04/2007 called "advice wanted about \write
and using active characters."
Thans
David R
David R
2007-04-26 23:55:11 UTC
Permalink
Post by Enrico Gregorio
Post by David R
Is my assumption that once something is in the token list it is
impossible to expand it any further correct?
The problem is that ' /is/ active in verbatim mode! That's done
in order to avoid combinations like '' triggering the ligature.
{\catcode`\'=\active
\gdef\doactivequoteinverbatim{\def'{<WHATEVER>}}}
\newenvironment{myverbatim}
{\verbatim\doactivequoteinverbatim}
{\endverbatim}
Do you see? The \verbatim command defines its version of the active
quote, but we reinstate the desired one afterwards.
I suspect that something will break, however. Try it.
No, surprisingly the verbatim code is perfectly what I want in the
example below.
but I want it to write to a file now.
using \afterassignment and \let which are not expandable.
Post by Enrico Gregorio
Your statement about what's in a token list is incorrect: /category/
/codes/ of tokens already absorbed in a token list are immutable. The
meaning of control sequences can change till the last moment.
But, with e-TeX (which is used by standard distributions nowadays),
there is \scantokens ...
I could do with some pointers about \scantokens
I can't find much informative documentation about it. it seems missing
from the latex source.
can you magically put the \doactivequoteinenvironment somewhere in
myverbatimwrite like you did with myverbatim?


\documentclass{article}
\usepackage{verbatim}
\begin{document}
hello chuck
\makeatletter

{\catcode`\'=\active
\gdef\doactivequoteinverbatim{\let'\activeapost}}
\newenvironment{myverbatim}
{\verbatim\doactivequoteinverbatim}
{\endverbatim}

\def\activeapost{\afterassignment\absorb\let\next= }

\def\absorb{ \ifx p\next next is a p\else next is not a p\fi\laps}

\def\laps{laps}

\begin{myverbatim}
This is myverbatim' environmemt 'putting this here
\end{myverbatim}


\newwrite\***@out
\def\verbatimwrite#1{%
\@bsphack
\immediate\openout \***@out #1
\let\do\@makeother\dospecials
\catcode`\^^M\active
\def\***@processline{%
\immediate\write\***@out{\the\***@line}}%
\***@start}
\def\endverbatimwrite{\immediate\closeout\***@out\@esphack}


\begin{verbatimwrite}{nancy.tex}
Hello everybody this is a 'famous catchphrase.
\end{verbatimwrite}
some more

\end{document}

David R

Christian Ebert
2007-04-24 23:29:26 UTC
Permalink
Post by Donald Arseneau
Post by Christian Ebert
At least in the example of the OP, not using the environment
saves some typing, as \endverbatim closes the group ;)
So you suggest using a \small declaration inside the verbatim
environment?
No.
Post by Donald Arseneau
I hope not.
Hope is a big word for a small declaration.

I should have written: in my example when answering the OP.

[...]
\usepackage{verbatim}
\newenvironment{code}{\small\verbatim}{\endverbatim}
[...]
I have no hope to improve the elegance of your solution.

c
--
_B A U S T E L L E N_ lesen! --->> <http://www.blacktrash.org/baustellen.html>
David R
2007-04-25 10:47:47 UTC
Permalink
Post by Christian Ebert
[...]
\usepackage{verbatim}
\newenvironment{code}{\small\verbatim}{\endverbatim}
[...]
I think that if you look in verbatim.sty you find
\def\verbatim{...}
and
\def\endverbatim{...}

Somehow this (declarations) is linked with the latex source
so that \begin{verbatim} and \end{verbatim} means the same as the above
My guess is that the { and the } don't do anything if \end{verbatim} is
read, but I am possible wrong about that (perhaps { and } are not
allowed in control sequence names).

How \begin{verbatim} works again is guesswork, but the way I would have
thought to do this is to look at whatever follows \begin{untilthe},
throw away the \begin{ and the } and then \csname untilthe\endcsname\relax

what is actually in verbatim.sty is

\def\verbatim{\begingroup\@verbatim \frenchspacing\@vobeyspaces
\***@start}
and

\def\endverbatim{\endtrivlist\endgroup\@doendpe}

David R
Robin Fairbairns
2007-04-25 14:10:39 UTC
Permalink
Post by David R
Post by Christian Ebert
[...]
\usepackage{verbatim}
\newenvironment{code}{\small\verbatim}{\endverbatim}
[...]
I think that if you look in verbatim.sty you find
\def\verbatim{...}
and
\def\endverbatim{...}
Somehow this (declarations) is linked with the latex source
so that \begin{verbatim} and \end{verbatim} means the same as the above
My guess is that the { and the } don't do anything if \end{verbatim} is
read, but I am possible wrong about that (perhaps { and } are not
allowed in control sequence names).
i thought you had been reading the source of latex; how environments
work is *really* straightforward.

\begin#1->check \csname#1\endcsname exists
\begingroup
remember #1, line number
\csname#1\endcsname

\end#1->\csname end#1\endcsname
check #1=what was remembered above
\endgroup
\csname end#1\endcsname

modulo the odd topping and tailing.
Post by David R
How \begin{verbatim} works again is guesswork, but the way I would have
thought to do this is to look at whatever follows \begin{untilthe},
throw away the \begin{ and the } and then \csname untilthe\endcsname\relax
what is actually in verbatim.sty is
and
all you actually need to know about verbatim.sty is in its
documentation. if you can't bring yourself to read that, perhaps
looking at the definition of \***@start will help.
--
Robin Fairbairns, Cambridge
Robin Fairbairns
2007-04-25 14:51:24 UTC
Permalink
Post by Robin Fairbairns
\end#1->\csname end#1\endcsname
check #1=what was remembered above
\endgroup
\csname end#1\endcsname
of course the last line shouldn't be there ;-)
--
Robin Fairbairns, Cambridge
Donald Arseneau
2007-04-25 12:56:25 UTC
Permalink
Post by Christian Ebert
Post by Donald Arseneau
Post by Christian Ebert
At least in the example of the OP, not using the environment
saves some typing, as \endverbatim closes the group ;)
So you suggest using a \small declaration inside the verbatim
environment?
No.
Oh good!
Post by Christian Ebert
Post by Donald Arseneau
I hope not.
Hope is a big word for a small declaration.
We should live in an environment of hope.
Post by Christian Ebert
\usepackage{verbatim}
\newenvironment{code}{\small\verbatim}{\endverbatim}
Yes, best answer.
--
Donald Arseneau ***@triumf.ca
Rainer Schoepf
2007-04-23 10:53:03 UTC
Permalink
Post by Christian Ebert
Post by iamsolo
I want to create a new environment called 'code' whose content is
smaller than normal text, and is verbatim. Intuitively, I can do
\begin{small}
\begin{verbatim}
code code code...
\end{verbatim}
\end{small}
\small is not an environment.
No, it's a declaration. Every declaration has an environment of the
same name. So the above code is perfectly valid LaTeX input.

Rainer

PS: Don't use verbatim when alltt will do.
Continue reading on narkive:
Loading...