Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
    
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. –  Andrew Swann Mar 10 '13 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 '13 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. –  Andrew Swann Mar 10 '13 at 15:45

1 Answer 1

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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