5

I would like to combine the power of \DefineVerbatimEnvironment with \UseVerbatim within a combined beamer presentation and handout scenario. I have already defined the environment coding, and now I can typeset my code using

\begin{coding}
  foo->bar( ).
\end{coding}

I can also define and re-use verbatim snippets in their stand-alone version:

\begin{SaveVerbatim}{Foo}
  foo->bar( ).
\end{SaveVerbatim}

\UseVerbatim{Foo}

The problem is that the latter is typeset using the default fancyvrb options. How can I combine the two so that the saved text is displayed using my custom environment?


\documentclass{article}
\usepackage{fancyvrb}

\DefineVerbatimEnvironment{coding}{Verbatim}{numbers=left,fontsize=\small}

\begin{document}

\begin{coding}
  foo->bar( ).
\end{coding}

Yup. This works.

\begin{SaveVerbatim}{Foo}
  foo->bar( ).
  boo->baz( ).
  cat->code( ).
\end{SaveVerbatim}

\UseVerbatim{Foo}
\UseVerbatim{Foo}
\UseVerbatim{Foo}

Yup, this works as well -- but it uses the default format, not my custom environment: larger font, no line numbers.

\end{document}
3
  • It would be best if you posted a complete minimal working example (MWE). In lieu of that you can pass the same options to SaveVerbatim as you used to create the coding environment. Mar 10, 2013 at 14:50
  • @AndrewSwann: I've added the MNWE. Passing the options to SaveVerbatim won't work in my case, because I need slightly different environment definitions for the slides and the handouts (different font sizes).
    – vwegert
    Mar 10, 2013 at 15:02
  • The fontsize can be passed to the \UseVerbatim command as an optional argument, e.g. \UseVerbatim[fontsize=\small]{Foo}. However, line numbering doesn't seem to work in this set-up. Mar 10, 2013 at 15:45

1 Answer 1

3

Taking a look inside fancyvrb, there is a mechanism to extend existing or create new verbatim commands in quite a general way. The key is \FV@Command, which takes a keyval list of settings and an 'internal' command to execute. You'll find that \UseVerbatim is defined as

\def\UseVerbatim{\FV@Command{}{UseVerbatim}}

This suggests a couple of fixes. First, you could simply redefine \UseVerbatim to use your settings:

\def\UseVerbatim{\FV@Command{numbers=left,fontsize=\small}{UseVerbatim}}

Alternatively, you could create a related command, say \UseCoding, and leave \UseVerabtim alone:

\documentclass{article}
\usepackage{fancyvrb}

\DefineVerbatimEnvironment{coding}{Verbatim}{numbers=left,fontsize=\small}
\makeatletter
\def\UseCoding{\FV@Command{numbers=left,fontsize=\small}{UseVerbatim}}
\makeatother
\begin{document}

\begin{coding}
  foo->bar( ).
\end{coding}

Yup. This works.

\begin{SaveVerbatim}{Foo}
  foo->bar( ).
  boo->baz( ).
  cat->code( ).
\end{SaveVerbatim}

\UseCoding{Foo}

\end{document}

If you want to be really systematic, it would also be possible to create a SaveCoding environment, although that's probably overkill.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.