Generalusing verbatim in the argument of new environment

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
User avatar
Steztric
Posts: 12
Joined: Sat Oct 25, 2008 2:00 pm

using verbatim in the argument of new environment

Post by Steztric »

Hi everyone,

I want to create a new environment which abstracts the following typesetting information;

Code: Select all

{\ttdefault
{\small
  \begin{verbatim}
Some text that is typset to the correct style
  \end{verbatim}
}
}
The problem is, when I define the "begdef" argument of \newenvironment, I cannot use \begin{verbatim}, as it thinks I am trying to write the rest of the argument verbatim. So the only solution I could think of is to use \verb to include it, like this;

Code: Select all

\newenvironment{code}[default]{\verb+{+\ttdefault\verb+{+\small\verb+\begin{verbatim}+}{\verb+\end{verbatim}}}+}
This is obviously wrong, because I get loads of errors.

How would I do this please anyone?

Thanks in advance,

Steve

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

using verbatim in the argument of new environment

Post by Juanjo »

Have a look at the fancyvrb package. It provides facilities to define verbatim environments. Even more, it is possible that some of the already predefined environments may suit your needs.
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
User avatar
Steztric
Posts: 12
Joined: Sat Oct 25, 2008 2:00 pm

using verbatim in the argument of new environment

Post by Steztric »

Great! That helps- but I don't want to have to be typing a lot of arguments every time I want to use the same Verbatim environment. Maybe I will want to change the output style of all the instances of code. Unfortunately with this package, I still can't do this;

Code: Select all

\newenvironment{code}[1]{\begin{Verbatim}[fontsize=\small]}{\end{Verbatim}}
Because when compiling my document, the Verbatim package thinks I am trying to define parts of the \newenvironment instruction as Verbatim...

Any other ideas?

Thanks,

Steve
phi
Posts: 577
Joined: Tue Oct 21, 2008 8:10 pm

Re: using verbatim in the argument of new environment

Post by phi »

You cannot use \begin{verbatim} and the likes inside an environment definition. Look at section 4.2.4 (Personalized environments) of the fancyvrb documentation.

If you want to typeset source code, you can also use the listings package.
User avatar
Steztric
Posts: 12
Joined: Sat Oct 25, 2008 2:00 pm

using verbatim in the argument of new environment

Post by Steztric »

Thanks phi- that gave me the information I needed.

If anyone else wants to know, I can define the new environment like this;

Code: Select all

\DefineVerbatimEnvironment{code}{Verbatim}{fontsize=\small}
\begin{code}
Some code
\end{code}
Great stuff.
fasol8
Posts: 1
Joined: Wed Oct 17, 2012 12:44 am

using verbatim in the argument of new environment

Post by fasol8 »

Simplest way to start with \VerbatimEnvironment

Code: Select all

\newenvironment{newenv}
  {\VerbatimEnvironment *beginingcommands* \begin{Verbatim}}
  {\end{Verbatim} *endingcommand*}
This solution i find in guide for nice package "fansybox", for example if you need boxed source code you can try this

Code: Select all

\usepackages{fansybox}
...
\newenvironment{FramedVerb}
  {\VerbatimEnvironment \vspace{4pt} \noindent \begin{Sbox}
    \begin{minipage}{.94\textwidth} \begin{small}\begin{Verbatim}}
  {\end{Verbatim}\end{small}\end{minipage}\end{Sbox}
    \setlength{\fboxsep}{8pt}\fbox{\TheSbox} \vspace{4pt}}
...
\begin{FramedVerb}
> airquality
> shapiro.test(airquality$Ozone)
\end{FramedVerb} %$
Post Reply