乳胶膨胀

乳胶膨胀
I've a LaTeX source.
I'm ready %for submission
%But first I would like
to strip its comments.

So I hope there are
100\% auto
ways to get this done.

\begin{comment}
   Because there are subtle ways to mess it up.
\end{comment}

是否有一个实用程序可以消除所有这些评论?

是的,我可以手动完成,但这样似乎很费力,容易出错,维护起来也很困难。我也可以使用sed,但容易出错。此外,GNU/Linux 的一条公理是,如果你能想到它,那么可能有人已经为它制作了一个实用程序。

答案1

要从 LaTeX 文件中删除所有注释,可以使用arxiv-latex-cleaner。积极维护,1.2k GitHub 星星,Apache-2.0 许可证,用 Python 编写但不需要了解 Python。

例子:

pip install arxiv-latex-cleaner
arxiv_latex_cleaner /path/to/latex_folder 

答案2

乳胶膨胀

(与 TeXLive 一起分发)

latexpand是一个扩展指令的工具include/input,它还可以删除注释。它还可以调整以保留行尾注释,因为有时它们是有意义的。在第二步中,删除仅包含以下内容的行%

$ latexpand --empty-comments mytexfile.tex > mytexfile-stripped.tex

删除仅包含%空格的行:

$ sed -i '/^\s*%/d' mytexfile-stripped.tex

并挤压空白行(cat -s可能会出现窗口行结尾的问题,并且操作系统 -s有不同的含义)

$ cat -s mytexfile-stripped.tex | sponge mytexfile-stripped.tex

(是来自sponge的一部分moreutilshttps://joeyh.name/code/moreutils/

答案3

我不确定该怎么做。所以,我发布了一个新的解决方案。我昨天发布的代码会从环境中删除注释verbatim

这是一个需要清理的新示例文件:

I've a LaTeX source.
I'm ready %for submission
%But first I would like
to strip its comments.

So I hope there are
100\% auto
ways to get this done.

\begin{comment}
   Because there are subtle ways to mess it up.
\end{comment}

\begin{verbatim}
        next two lines should not be lost
        % don't lose this line
% this line should stay too
\end{verbatim}

根据verbatim软件包文档verbatimcomment环境不应嵌套。以下代码(类似于我昨天发布的代码)不会吃掉环境中出现的注释行verbatim

以下是更正后的 Perl 代码:

#!/usr/bin/perl
use strict 'vars';
&MAIN(@ARGV);
sub MAIN {
   my ($filehandle) = @_;

   open FILE, "<$filehandle";
   my @doc = <FILE>;
   close FILE;

   &removeComments(\@doc);

   foreach my $line ( @doc ){
      print $line;
    }

   return 1;
}

sub removeComments {
   my ($docarray) = @_;

   my $isCommentEnvironment  = "no";
   my $isVerbatimEnvironment = "no";

   my @newdoc;

   foreach my $line ( @{$docarray} ){
      $isVerbatimEnvironment = "yes" if ( $line =~ /^\\begin{verbatim}/ );
      $isCommentEnvironment  = "yes" if ( $line =~ /^\\begin{comment}/ );
      if ( ($isVerbatimEnvironment eq "no") && ($isCommentEnvironment eq "no") ){
     next if ($line =~ /^%/);
     ## Temporarily replace "%" that you want to keep with a dummy string
     ## that does not appear elsewhere in your document.  Then, remove remainder
     ## of lines that still contain "%".
     if ( $line =~ /\\%/){
        $line =~ s/\\%/TMP::PERCENT/g;
        $line =~ s/%.*//;
        $line =~ s/TMP::PERCENT/\\%/g;
      } else {
     ## do not remove trailing % marking NO SPACE in LaTeX: $line =~ s/%.*//;
         $line =~ s/\s*%.+//;
       }
     push @newdoc, $line;
       }
      push @newdoc, $line if ( $isVerbatimEnvironment eq "yes" );

      $isVerbatimEnvironment = "no" if ( $line =~ /^\\end{verbatim}/ );
      $isCommentEnvironment  = "no" if ( $line =~ /^\\end{comment}/ );
    }

   @{$docarray} = @newdoc;
   return 1;
 }  

答案4

下面的 Perl 脚本应该可以解决问题:它期望接收您想要删除注释的文件的名称,然后打印以控制台修改后的文档。

#!/usr/bin/perl
use strict 'vars';
&MAIN(@ARGV);
sub MAIN {
   my ($filehandle) = @_;
   open FILE, "<$filehandle";
   my @doc = <FILE>;
   close FILE;
   &removeComments(\@doc);

   foreach my $line ( @doc ){
      print $line;
    }
   return 1;
}
sub removeComments {
   my ($docarray) = @_;
   my $isCommentEnvironment = "no";
   my @newdoc;

   foreach my $line ( @{$docarray} ){
      $isCommentEnvironment = "yes" if ( $line =~ /^\\begin{comment}/ );
      if ( $isCommentEnvironment eq "no" ){
     next if ($line =~ /^%/);
     ## Temporarily replace "%" that you want to keep with a dummy string
     ## that does not appear elsewhere in your document.  Then, remove remainder
     ## of lines that still contain "%".
     if ( $line =~ /\\%/){
        $line =~ s/\\%/TMP::PERCENT/g;
        $line =~ s/%.*//;
        $line =~ s/TMP::PERCENT/\\%/g;
      } else {
         $line =~ s/%.*//;
       }
     push @newdoc, $line;
       }
      $isCommentEnvironment = "no" if ( $line =~ /^\\end{comment}/ );
    }

   @{$docarray} = @newdoc;
   return 1;
 }  

我唯一不确定的是使用\begin{comment}和 时是否有任何限制\end{comment}。例如,我不知道——就像环境一样verbatim——您是否对这些行上可能出现的其他内容有所限制。

相关内容