# How to “clean” article source code before submitting to ArXiv?

ArXiv requires uploading TeX sources. The justifications they give for this are rather weak IMHO, but that is beside the point.

What is the easiest way to clean up an article for submission? ArXiv suggests to automatically delete all lines starting with %, but of course this vastly understates the possibilities of LaTex: Many of my files contain complex \if statements, \iffalse-\fi blocks, not all % symbols mark comments, etc.

Is there a good automatic way to prepare a file for submission?

During the compilation, clearly at some point LaTeX must decide which parts to ignore and which to compile, what to do with the conditionals etc. Is there a way to send the result of this into a file?

Or are there any other good ideas of how to approach this problem?

• I assume 'cleaning up' just implies that the source is readable and does not contain unnecessary packages etc. It does not mean to make it as simplified as possible. Things to avoid are dependencies on external tools, use pdflatex only (so no XeLaTeX or LuaLaTeX or ConTeXt), precompile bbl and ind files, precompile minted listings, pythontex, gnuplottex etc. But considering standard TeX/LaTeX syntax such as if-statements there should not be a problem at all for ArXiv to compile it. – Marijn Jul 16 '19 at 11:01
• No, that is not what I mean by cleaning up. I mean removing comments, outcommented old content, outcommented content cut from the article that is meant to be published in the future, etc. What should remain is exactly as much source code as is needed to generate the article as it looks as PDF. – Rupert Hölzl Jul 16 '19 at 11:57
• That is what I mean by 'making the source readable', i.e., removing commented content et cetera. That was maybe not clear, sorry. However, for ArXiv I don't think even this kind of cleaning up is very important, they just want to make sure that the document can be compiled with their setup. As long as you provide a source that does this then it's ok. Alternatively, if your reason for cleaning up is more personal (i.e., you want the file you submit to be as clean as possible) then you are also the person that decides what should be cleaned and what shouldn't. – Marijn Jul 16 '19 at 12:28
• in answer to your specific question about whether you can output the intermediate latex expansion then no, latex interleaves parsing the source file and execution, it just reads the source file character by character stripping comments and evaluating conditionals as it goes, so it never constructs (or is able to output) an equivalent file with such constructs removed. – David Carlisle Jul 16 '19 at 13:01
• @rmh So it's nothing to do with ArXiv requirements but that you have included stuff in your code that you don't want to be made public. You remove your private code. – Peter Wilson Jul 17 '19 at 16:57

Google provides a neat tool to clean up the source code for arxiv submission - https://github.com/google-research/arxiv-latex-cleaner.

It removes auxiliary files, comments from your code and has some other nifty features.

• Thank you! This looks pretty much like what I wanted. Will try out next time the problem comes up! – Rupert Hölzl Apr 8 at 8:35

Pandoc has an option to clean the source code:

  pandoc withcomments.tex -o withoutcomments.tex

• This left my computer hanging on a moderate .tex file. Not sure why… – Clément May 4 at 14:45

latexindent.pl can help with this, using its -rr switch.

% my comment
\documentclass{article} % trailing comment

%comment
\begin{document}
$$x^2$$ % more comments
\end{document}


and the following settings in rmh.yaml

replacements:
-
substitution: s/^%.*\R//mg


and run the command:

latexindent.pl -rr -l=rmh.yaml myfile.tex


\documentclass{article} % trailing comment

\begin{document}
$$x^2$$ % more comments
\end{document}


The above has left in trailing comments. If you want these removed as well, then you can explore

replacements:
-
substitution: s/%.*\R//mg


If you'd like more subtle replacements, we'll have to see a before and after in your question :)

• As I said, I also want the content of any false conditionals automatically stripped. But apparently that is hopeless, so I will just have to do it manually. – Rupert Hölzl Jul 17 '19 at 9:19
• If you can provide a complete example of before and after, I might be able to help – cmhughes Jul 17 '19 at 11:36