Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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
add comment

1 Answer

up vote 11 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

becomes

blah blah
blah blah

But

abc% a comment here
xyz

becomes

abc
xyz

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

abcxyz

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 ()
(interactive)

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

(goto-char (point-min))
(while (re-search-forward "\\\\verb|" nil 1)
  (progn
   (replace-regexp "%" "@@@@@PERCENT@@@@@" nil (point)
           (save-excursion
             (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
    
Very nice! This is going straight to my .emacs file. –  Svend Tveskæg Nov 16 '12 at 16:04
    
Than You, it is very helpful. I appreciate your assistance. –  user21710 Nov 16 '12 at 20:08
add comment

Your Answer

 
discard

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.