刪除 LaTeX 中的註釋

刪除 LaTeX 中的註釋

我正在努力從 LaTeX 文件中刪除註釋。

我嘗試了兩個主要答案這個問題,但它不起作用。

這是一個測試檔案:

é %comment
  1. 當我運行時latexpand test.tex > stripped.tex,生成的文件的編碼存在問題,它無法編譯為 PDF。stripped.tex如下:
é %
  1. 當我運行時arxiv-latex-cleaner \path\to\folder,出現錯誤,指出arxiv-latex-cleanerPATH 上未定義:我有位於 PATH 的arxiv-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.exe(即)。C:\Users\MYNAME\AppData\Roaming\Python\Python39\Scripts我使用pip卸載並重新安裝arxiv-latex-cleaner,無論是在Windows提示符號下還是在Anaconda提示符下,都無濟於事。我仍然遇到同樣的錯誤。

我透過 Anaconda 安裝了 Windows 10、Python 3.9,並透過 texlive 安裝了 LaTeX。

答案1

(這是第三版)

將此儲存為文件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 在工作庫中的新檔案中產生輸出。請先將要剝離的文件放在同一工作庫中。

有一些限制,如果%用於註釋字元以外的其他用途,則會出現問題(例如,如果有\%.)

理論上,此程式碼會找到所有%' 並修剪其輸入行中跟隨它們的所有內容。

與此答案的最新版本相比,此答案避免了輸​​出檔行以空格字元結尾。 (無論如何這並不重要)。

相關內容