2015-06-19 76 views
0

我正在尝试在latex中创建一个环境,它在TeX文件中写入\ begin {environment}和\ end {environment} verbatim之间的行。我试过fancyvrb软件包,它的工作原理,但如果我在我的源文件中指定了几个\ begin {environment},只有最后一行被写入outfile(我猜测VerbatimOut每次都会重新创建outfile,并没有附加到它)。LaTeX:将逐字行添加到输出文件

有没有人对此有所领导?谢谢!

回答

0

这是一个稍微间接的答案,但Victor Eijkhout的comment包做了类似的事情,就像它处理'逐字'块一样,LaTeX也是这样。如果不这样做,那么它的实现就会建议如何手工完成这个工作(也就是说,这个包是我自己亲手做的时候拷贝的)。

如果不这样做,您可能需要询问TeX Stackexchange site

1

我遇到了同样的问题,并解决它如下。

文件verbatimappend.tex(注意文件的LaTeX写入不再是一个说法,因为它是在verbatimwrite环境,但在\ verbatimFile定义):

\documentclass{article} 
\usepackage[utf8]{inputenc} 
\usepackage[T1]{fontenc} 
\usepackage{moreverb} 

\makeatletter 
\def\verbatimappend{% inspired by moreverb.sty (verbatimwrite) 
    \@bsphack 
    \let\do\@makeother\dospecials 
    \catcode`\^^M\active \catcode`\^^I=12 
    \def\[email protected]{% 
    \immediate\write\verbatimFile% 
     {\the\[email protected]}}% 
    \[email protected]} 
\def\endverbatimappend{% 
    \@esphack% 
} 
\makeatother 

\begin{document} 

\newwrite\verbatimFile 
\immediate\openout\verbatimFile=verbatimFile.txt\relax% 

\begin{verbatimappend} 
Hello, world! 
\end{verbatimappend} 

\input{random_chars.tex} 

\begin{verbatimappend} 
Bye, world! 
\end{verbatimappend} 

\immediate\closeout\verbatimFile 

\end{document} 

其中我压力测试如下。 文件random_chars.pl:

#! /usr/bin/perl 
use warnings; 
use strict; 
binmode STDOUT, ":utf8"; 
binmode STDERR, ":utf8"; 

my @ords = (32..126, 160..255); # usable latin-1/latin-9 codepoints 
my $N = scalar @ords; 
my @lines = (); 

sub choose_random_char { 
    my $ord = int(rand($N)); 
    return chr($ords[$ord]); 
} 

while ((scalar @lines) < 10000) { 
    my $line = join('', map { choose_random_char() } (1..78)); 
    next if $line =~ m/\\end{verbatimappend}/sx; # probably very unlikely! 
    next if $line =~ m/\s+$/sx; # final spaces do not get output -> false positive 
    push @lines, $line; 
} 

print join("\n", @lines, ''); 
print STDERR join("\n\n", 
    (map { "Paragraph\n\n\\begin{verbatimappend}\n$_\n\\end{verbatimappend}" } @lines), ''); 

使用它们:

$ perl random_chars.pl > random_chars.txt 2> random_chars.tex 
$ latex verbatimappend.tex 
$ diff random_chars.txt verbatimFile.txt 

注意排除random_chars.pl的具体情况:

next if $line =~ m/\\end{verbatimappend}/sx; # probably very unlikely! 
    next if $line =~ m/\s+$/sx; # final spaces do not get output -> false positive 

不知道如何/这是否可以/应该被发送到包装作者 https://www.ctan.org/pkg/moreverb ,因为包装似乎没有维护。

HTH。