I'm new to LaTeX and I'm just starting to create my own little template. I'm putting comments everywhere to make it easier to remember what command does what.

I'd like to have a seperate file that does not contain those comments though. Is there any easy way I can delete all comments from a .tex file?


  • 6
    Either you do regex-based replace (which every decent editor supports) or you should have a look at the dtx format.
    – TeXnician
    Sep 21, 2018 at 8:42
  • Uff, alright, I gotta add that I'm completely new not only to latex but to everything that's got to do with it. I'm using TeXmaker, can I do what you said with it? Sep 21, 2018 at 8:57
  • Yes, TeXmaker has regex-based find & replace (click the + sign in the replace pop-up). And for editing dtx files you could even use ed (editor does not matter), but you would have to read a bit.
    – TeXnician
    Sep 21, 2018 at 9:02
  • For info another type of solution, type texdoc docstrip at the prompt of a terminal emulator window.
    – GuM
    Sep 21, 2018 at 9:03
  • 1
    On a side note: comments are great and help you structure your code. You may want to prefer writing good comments (advice, here and there too) instead of deleting all of them.
    – ebosi
    Sep 21, 2018 at 10:28

2 Answers 2


I hope you won't mind this suggestion, but it seems to me that when you compile the LaTeX document you will see all of your text without the comments. If that is sufficient for what you want then why not just have the compiled .pdf document plus the source .tex file.

When you edit your .tex document do the comments appear in a different colour? Many (most?) editors will put your comments in a different colour that make it much easier to see which parts of the .tex file are comments and which parts are not. I can see that if your comments are in the same colour as your main text that would be really confusing.

[I would encourage you to keep the comments (even if you just make them smaller/shorter) as I find that comments help me keep track of what LaTeX is doing... .... -- for me it would be even more confusing not to have the comments..... sorry I guess this may not be the answer you want]

  • no that works fine. The comments are in a different color than the rest of the text, the hole .tex document just gets longer and a little more confusing :) Sep 21, 2018 at 9:09
  • @AntonNeundorfer - ok, fair enough, - I would encourage you to keep the comments (even if you just make them smaller/shorter) as I find that comments help me keep track of what LaTeX is doing... .... -- for me it would be even more confusing not to have the comments..... sorry I guess this may not be the answer you want,.. I will probably delete this answer if it is not helpful.
    – tom
    Sep 21, 2018 at 9:15
  • no it's cool, 'll probably end up doing that. Sep 21, 2018 at 9:18
  • It may not be relevant at this early stage but as you progress with commenting you may wish for more control so it may be worth a quick search/review of related issues such as comment-out-lines
    – user170109
    Sep 21, 2018 at 10:40

1/ What you want to do

You want to tell your IDE (i.e., the program you use to write .tex files) to:

  1. look for all comments in you text,
  2. delete them — i.e. replace them with an empty string.

2/ Explaining your IDE what is a comment

The issue is then "how can I tell my IDE what a comment is?". First, let's define what a comment is. A comment:

  1. is a unknown number of successive characters
  2. starts with %
  3. but is not preceded with \ (otherwise, you would delete sentences in which you use the percentage symbol % — e.g., in "I should definitively get a 13% raise!" that is written as following in your code: I should definitively get a 13\% raise!).
  4. ends at the end-of-line.

To tell this to your IDE, you can use regular expressions (regex). Explaining regex is out of topic here, but the "regex code" you are looking for is:


Explanation on the regex:

  • .*: you are looking for an unknown number of any character (implied here: ending at the end of line).
  • %: that should arrive after a % character.
  • (?<!\\): but the % character should not (!) be preceded ((?< )) with a backslash (\\).

3/ Proceed to the deletion

You then "just" need to search for the previous regular expression and replace every matching string with an empty one.

"Just", because it isn't that easy. Indeed, the previous expression uses negative lookbehind, which is an advance regex functionality that is not implemented in every editor. We'll then have a look at a workaround.

(If your IDE is regex-lookaround enabled, you probably don't need me to tell you how to do a search and replace.)

Prior potentially damaging your code, you can test if it works on this text sample:

% this is a full line comment
This line starts with normal text,% and ends with a comment.
This line is 100\% text, without any comment.

4/ Workaround if you use TeXworks, TeXstudio, Atom, or any other IDE that does not support regex lookaround.

Here is a solution that doesn't use regex lookaround. The idea is to:

  1. "protect" \% occurrences in your text,
  2. delete comments
  3. revert \% back to normal.

Ready? Go:

4.1/ Protecting \%

The idea here is to temporarily replace \% with something that: 1/ won't be deleted by the next regex, 2/ can easily be re-converted into \% afterwards without causing any false positive errors. What you need to do is:

  • search for: \% (do not use "regex search")
  • replace with: [this is a protected percentage symbol and will be soon be reverted back to normal] (unless you already have exactly this sentence in your document, obviously).

4.2/ Deleting comments

Now that we are sure all % characters in our documents are start of comments, we can bluntly delete them all:

  • search for: %.* (use "regex search" here)
  • replace with: (nothing, i.e., leave text input void)

4.3/ Reverting percentage symbols back to normal

Now we can undo our step 4.1. For that:

  • search for: [this is a protected percentage symbol and will be soon be reverted back to normal] (or any string you used in step 4.1. Do not use "regex search".)
  • replace with: \%

Et voilà!

5/ How to do a (regex) search and replace in my IDE?

This depends on your IDE. You want to make sure that your "Search and replace" function accepts regular expression (and will not blindly look for (?<!\\)%.* in your text).

  • TeXworks: Go in the Search/Replace menu (or Ctrl+R). To activate the regex-search, tick the "Regular expression" box.

    enter image description here
    enter image description here
    enter image description here

  • TeXstudio: Hit Ctrl+R. To activate regex-search, click on the little Reg button at the end of the "Find" line.

    enter image description here

  • atom: Hit Ctrl+F. To activate regex-search, click on the .* button.

    enter image description here

  • thans for the elaborate answer! Sep 21, 2018 at 14:17
  • Alright, for some reason that crashes my TeXworks... Sep 21, 2018 at 14:18
  • any help with that? Sep 21, 2018 at 16:00
  • alright, now it's working, but instead of only deleting my comments it completely ereases my documents content... Sep 21, 2018 at 16:03
  • @AntonNeundorfer Have you tried with the text sample (see at the end of section 3)? At which step does it crash?
    – ebosi
    Sep 22, 2018 at 8:41

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