LaTeX でのコメントの削除

LaTeX でのコメントの削除

LaTeX ファイルからコメントを削除するのに苦労しています。

私は2つの主な答えを試しましたこの質問ですが、動作しません。

以下はテストファイルです:

é %comment
  1. を実行するとlatexpand test.tex > stripped.tex、結果のファイルのエンコーディングに問題があり、PDF にコンパイルされません。stripped.texは次のとおりです。
é %
  1. を実行すると、が PATH で定義されていないarxiv-latex-cleaner \path\to\folderというエラーが表示されます。 が配置されている フォルダ(つまり) を PATH に追加しました。Windows プロンプトと Anaconda プロンプトの両方で、pip を使用してアンインストールおよび再インストールしましたが、効果はありませんでした。引き続き同じエラーが発生します。arxiv-latex-cleanerarxiv-latex-cleaner : The term 'arxiv-latex-cleaner' is not recognized as the name of a cmdlet, function, script file, or operable program.arxiv_latex_cleaner.exeC:\Users\MYNAME\AppData\Roaming\Python\Python39\Scriptsarxiv-latex-cleaner

Windows 10、Anaconda 経由で Python 3.9 をインストールし、texlive 経由で LaTeX をインストールしました。

答え1

(これは第3バージョンです)

これをファイルとして保存stripper.tex

% macro to sanitize  catcodes, making space and % active
\def\stringregime{%
    \catcode`\\ 12
    \catcode`\{ 12
    \catcode`\} 12
    \catcode`\# 12
    \catcode32 \active
    \catcode`\~ 12
    \catcode`\% \active
}%
% active space gives normal space
\begingroup
\catcode32\active\xdef {\string }%
\endgroup
\begingroup
% the \empty delimiter will be added manually by \handlelines
\catcode`\%\active
\xdef%#1\empty{\string%}
\endgroup

\def\removeEOLspacechar#1 {\def\fileline{#1}}
\def\bracedpar{\par}

% main loop macro
\def\handlelines{%
  \ifeof\infile 
  \else
  \read\infile to \fileline
  % remove the EOL space from \fileline before outputting it
  % If TeX replaces an empty line by \par in input there is no space
  % token only the \par token.
  \ifx\fileline\bracedpar\def\fileline{}\else
      \expandafter\removeEOLspacechar\fileline
  \fi
  \immediate\write\outfile{\fileline\empty}% delimiter for active %
    \expandafter\handlelines
  \fi
}

% loop launch
\def\DoIt{%
  \begingroup
  \stringregime
  \handlelines
  \endgroup
}

% file auxiliaries
\newread\infile
\newwrite\outfile
\immediate\write128{}
\message{filename to strip of comments: }\read-1to\theinfile
% clean up trailing space
\def\cleanuptrailingspace#1 \par{\def\theinfile{#1}}
\expandafter\cleanuptrailingspace\theinfile\par
%
  \openin\infile\theinfile\relax
  \immediate\openout\outfile\theinfile-out

\DoIt

\immediate\write128{output written to \theinfile-out USE AT OWN RISK}
  \immediate\closein\infile
  \immediate\closeout\outfile
\bye

使用方法:etex stripperコマンドラインで発行すると、ファイル名の入力を求められます。ファイル名( で終わる<return>)を入力すると、eTeX は作業レパートリー内の新しいファイルに出力を生成します。最初に、ストリップするファイルを同じ作業レパートリー内に用意してください。

いくつかの制限があり、%がコメント文字以外の目的で使用される場合 (例: \%. がある場合)に問題が発生します。

理論的には、このコードはすべての を見つけて%、入力行でそれに続くものをすべてトリミングします。

この回答の最新バージョンと比較すると、この回答では出力ファイルの行がスペース文字で終わるのを回避します。(いずれにしても、これは問題ではありません)。

関連情報