Quantcast
Channel: Defining a new environment extending a verbatim environment - TeX - LaTeX Stack Exchange
Viewing all articles
Browse latest Browse all 5

Defining a new environment extending a verbatim environment

$
0
0

Why is it not possible to define a new environment that extends a verbatim environment?Consider, e.g.:

\documentclass{article}\newenvironment{myverbatim}{\tiny\begin{verbatim}}{\end{verbatim}}\begin{document}\begin{myverbatim}  test\end{myverbatim}\end{document}

Compiling with pdflatex gives the error:

Runaway argument?^^M  test^^M\end{myverbatim}^^M\end{document}^^M^^M! File ended while scanning use of \@xverbatim.<inserted text>                 \par

(A motivation for this question can be found here: "Format a verbatim paragraph". From which it should be clear that I cannot use the listings or fancyvrb packages)

It would be nice with a basic and perhaps more detailed explanation (for this particular case and the reason for the error message) than what is given in, for instance, this discussion: "Why doesn’t verbatim work within...", and if there could be workarounds for simple cases like the one in this example..

Edit1:

Based on the answers so far, I feel that I need to be more precise about my case: I am trying to get the following code to work somehow (Confer with "Format a verbatim paragraph" for more details)

\documentclass{article}\makeatletter\begingroup  \catcode`|=0  \catcode`[=1  \catcode`]=2  \catcode`\{=12  \catcode`\}=12  \catcode`\\=12  |gdef|myvr@xverbatim#1\end{myverbA}[#1|end[myverbA]]|endgroup\newenvironment{myverbA}{%  \let\@xverbatim=\myvr@xverbatim  \verbatim}{%}\makeatother\newenvironment{myverbB}{\tiny\begin{myverbA}}{\end{myverbA}}\begin{document}\begin{myverbB}  test\end{myverbB}\end{document}

Edit2:

Based on egreg's answer, I tried the following:

\documentclass{article}\usepackage{etoolbox}\makeatletter\begingroup  \catcode`|=0  \catcode`[=1  \catcode`]=2  \catcode`\{=12  \catcode`\}=12  \catcode`\\=12  |gdef|myvr@xverbatim#1\end{myverbA}[#1|end[myverbA]]|endgroup\newenvironment{myverbA}{%  \let\@xverbatim=\myvr@xverbatim  \verbatim}{%}\newenvironment{myverbB}  {\preto\verbatim@font{\tiny}   \myverbA}  {\endmyverbA}\makeatother\begin{document}\begin{myverbB}  test\end{myverbB}\end{document}

which produces the error:

Runaway argument?^^M  test^^M\end{myverbB}^^M\end{document}^^M^^M! File ended while scanning use of \@xverbatim.<inserted text>                 \par 

Viewing all articles
Browse latest Browse all 5

Latest Images

Trending Articles





Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>
<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596344.js" async> </script>