2015-06-19 74 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。