TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am working with other people LaTeX files and sometimes they look very messed up. I am looking for a way to delete all comments without changing the code. The problem is that I want to preserve all spaces and exclude percent signs \%. I am feeling bit insecure in doing it by myself and I am new to Emacs. Maybe there is something already written? However if not maybe You can help me out by pointing out risky situations that I should include in my elisp code.

P.S. Also any suggestions on how to make other people code more readable would be very appreciated :).

share|improve this question
See also the Python script handling several subtleties in tex.stackexchange.com/questions/83663/… – jrouquie Jan 18 at 11:01
up vote 14 down vote accepted

If you do not need to worry about verbatim or verb usage then

(query-replace-regexp "\\(^\\| *[^\\\\]\\)%.*" "" nil nil)

is probably safe (and it does query replace so you get to say yes or no anyway).

Note this removes the entire line if the comment was at the start of the line (as leaving a blank line would make a paragraph). However it does not remove line ends if the comment was not the whole line, this means it has the potential to introduce white space so it is probably safe on documents but not in macro code.

That is

blah blah
%not this line
blah blah


blah blah
blah blah


abc% a comment here



A more correct translation (which could relatively easily be done) would be to


But that would have a tendency to make the entire file one long line, and you'd have to be careful about leaving a space after command names.

LaTeX is not a regular language so if you parse it with regular expression then it will mess up some constructs. That's why they are called regular expressions. The alternative is to do a full latex parser but that is hard. Consider something like xii.tex there are some examples of that on this site. Trying to locate the comments in there would be tricky (there are none, but emacs doesn't know that).

It would be relatively easy to remove a preceding linebreak when removing the percent if that's what you want. for verb and verbatim I'd probably do a pre-pass changing % to [[[PERCENTWASHERE]] and then change it back again after you have removed the %

Perhaps not unlike this (defines an interactive command you can run with M-x xxx once the definition has been evaluated) It handles 5 in verbatim and \verb| ... % | if you use other characters as \verb delimiters it would need modifying a bit.

(defun xxx ()

(goto-char (point-min))
(while (re-search-forward "\\\\begin{verbatim}" nil 1)
   (replace-regexp "%" "@@@@@PERCENT@@@@@" nil (point)
             (progn (re-search-forward "\\\\end{verbatim}" nil 1) (point))))))

(goto-char (point-min))
(while (re-search-forward "\\\\verb|" nil 1)
   (replace-regexp "%" "@@@@@PERCENT@@@@@" nil (point)
             (progn (re-search-forward "|" nil 1) (point))))))

(query-replace-regexp  "\\(^\\| *[^\\\\]\\)%.*" "" nil nil)

(goto-char (point-min))
 (replace-regexp  "@@@@@PERCENT@@@@@" "%"  nil nil)
share|improve this answer
Thanks for your reply. However I need also to worry about verbatim environments. How can I exclude some environments from cleaning them up? And is it possible in Auctex to wrap up whole text after deletion of comments in line ends? – user21710 Nov 16 '12 at 13:24
too long for comment: I'll edit the answer – David Carlisle Nov 16 '12 at 13:35

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.