3

I want to define a new environment (here testenv), which is going to include text in verbatim mode. So, I have the code:

\documentclass{article}

\usepackage[english,greek]{babel}
\usepackage[utf8]{inputenc}
\usepackage{fancyvrb}

\newenvironment{testenv}
{
\selectlanguage{english}
\fontsize{8}{10}\selectfont
\Verbatim
}
{
\endVerbatim
\normalsize
\selectlanguage{greek}
}

\begin{document}

\begin{testenv}
This is a test. 
\end{testenv}

\end{document}

However, I need some optional arguments in the verbatim environment. How should I pass these options e.g. commandchars, commentchar ?

P.S. I make use of the fancyvrb package.

3
  • There are some verbatim related packages that could do what you want ...
    – juanuni
    Jun 11, 2015 at 1:30
  • @juanuni I already use fancyvrb package. Isn't this enough? Jun 11, 2015 at 1:34
  • There are anothers packages, verbatim package an others, sorry I don't remember the others ...
    – juanuni
    Jun 11, 2015 at 1:43

1 Answer 1

3

You can redefine the way Verbatim works using \RecustomVerbatimEnvironment, adding the formatcom key to execute formatting commands at the start of the Verbatim environment. Since the environment scope limits the extent of what happens with the content of formatcom, there is no need to reset it at the end:

enter image description here

\documentclass{article}

\usepackage[english,greek]{babel}
\usepackage[utf8]{inputenc}
\usepackage{fancyvrb}
\usepackage{xcolor}

\RecustomVerbatimEnvironment{Verbatim}{Verbatim}
  {commandchars = \\\{\},
   commentchar = !,
   formatcom = {\selectlanguage{english}
                \fontsize{8}{10}\selectfont}}

\begin{document}

\selectlanguage{greek}

\begin{Verbatim}
This is \textcolor{red}{red}. 
!This is a comment.
\end{Verbatim}

Some text.

\end{document}

This provides for a clean interface.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .