1/ What you want to do
You want to tell your IDE (i.e., the program you use to write .tex
files) to:
- look for all comments in you text,
- 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:
- is a unknown number of successive characters
- starts with
%
- 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!
).
- 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:
- "protect"
\%
occurrences in your text,
- delete comments
- 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).
texdoc docstrip
at the prompt of a terminal emulator window.