2

I am using the fancyvrb package. I'd like to create a verbatim-like environment that does the following two things:

1) Append the verbatim material as-is to a file that has already been opened. 2) Output the verbatim material like UseVerbatim would do.

I have tried to re-use the SaveVerbatim environment and to output the material with \immediate\write\FILE{\UseVerbatim{VERB}} but this doesn't as \UseVerbatim needs more than expansion. Adding \protect in front of \UseVerbatim didn't help either.

A non-working example follows:

\documentclass{article}
\usepackage{fancyvrb}
\newwrite\FILE
\immediate\openout\FILE=\jobname.out

\newenvironment{chunk}{%
    \VerbatimEnvironment{chunk}\begin{SaveVerbatim}[gobble=2,commentchar=\%,commandchars=\\\{\}]{VERB}%
}{%
  \end{SaveVerbatim}%
  \UseVerbatim{VERB}%
  \immediate\write\FILE{\UseVerbatim{VERB}}%
}

\begin{document}

\begin{chunk}
  First line.
  \texttt{Second} line.
\end{chunk}

\end{document}
6
  • afaik, this isn't possible; it would require a rewrite of tex engine's file handling, and would also require some new macro-level facility to access the new stuff; an alternative would be to write a shell script (or .bat file on widnoze), to be executed after the latex run. Commented Dec 26, 2014 at 13:17
  • What exactly, do you think, is not possible? I have the feeling I could get what I want but reimplementing much of fancyvrb but I don't think it is a good idea to reinvent the wheel. Commented Dec 26, 2014 at 13:28
  • @MarcNieper-Wißkirchen: Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}.
    – user31729
    Commented Dec 26, 2014 at 14:03
  • Added a MNWE :-) Commented Dec 26, 2014 at 14:18
  • Does this help? tex.stackexchange.com/questions/37792/… Commented Dec 26, 2014 at 14:34

1 Answer 1

1

Are you doing this for literate programming? If so, then maybe you want a specialized literate programming tool?

Yes, you could obviously do this by reimplementing fancyvrb.sty. Just tweak the lines containing \openout and \closeout, or even add the ability to write to an arbitrary handle as opposed to \FV@OutFile. Ain't the easiest or most robust solution though.

Instead, I've a Perl script called cat_latex_env that uses the Perl flip-flop operator .. to do exactly this :

#!/usr/bin/perl
use strict;
use warnings;

sub usage { die "Usage: cat_latex_env enviroment_name [filename]\n"; }
usage if ($#ARGV < 0);

my $env = shift;
my $begin = quotemeta "\\begin{$env}";
my $end = quotemeta "\\end{$env}";
while (<>) {
        if (/$begin/../$end/) {
                next if /$begin/ || /$end/;
                print;
        }
}

Just run cat_latex_env chunk file1.tex > file2.tex in your Makefile.

You must log in to answer this question.

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