10

I was looking through some tutorials recently and some of them used % and others used %%, both seemed to behave like comments though. I just wanted to know if there was a difference between the two and if there is, when to use % and when to use %%.

user77039
  • 101
  • Welcome to TeX.SX! I am sure your question has been asked already here –  Apr 26 '15 at 19:08
  • 6
    @user77039: As far as the compiler is concerned, there is no difference. The difference is relevant only to the docstrip program in that comments preceded by a single % are removed, while those beginning with %% are passed on to the file being unpackaged. – GuM Apr 26 '15 at 19:10
  • 2
    There is no difference. It is personal style. I often use %% where a single % would suffice. I find it easier to see. Also, I've set up my editor to allow me to highlight a single % differently from a double %%. This added visibility makes it easier to find bugs when unwanted spaces creep into the final document. – A.Ellett Apr 26 '15 at 19:11
  • @A.Ellett if it is \foo%%%% then yes but if it is %% Copyright.... then Gustavo's comment is probably the right guess. – David Carlisle Apr 26 '15 at 20:42
  • You've got the answer, but just a little aside: You might also see multiple %s used to provide some visual contrast, often - for example - to mark a kind of heading. I frequently do things like %%Heading (line break) %foo bar baz – Au101 Aug 03 '15 at 16:06

1 Answers1

11

As far as the TeX parser (more precisely, TeX’s “eyes” and “mouth”) is concerned, there is no difference at all between

% A comment starting with a single "%" sign.

and

%% A comment starting with a double "%" sign.

Indeed, as soon as the first % is seen, everything that follows, up to the end of the input line, is ignored, so that TeX doesn’t even notice if a second % comes next or not.

However, there is a significant difference in the way the two above comments are treated in by the docstrip program. Allow me a brief digression here.

Programmers who write code for (La)TeX applications usually employ the facilities offered by the doc package (and other packages, depending on the needs) to combine the actual code and some explicative comments in a single file, which can be formatted with LaTeX to produce a document that combines the TeX code with its documentation, as in Knuth’s “literate programming”. The docstrip program can be used (among other things) to strip off from such files the documentation part, producing a file that contains only the TeX code, almost without comments, and can therefore be input at higher speed (this was much more a concern twenty years ago than today).

More precisely, the docstrip program removes any line that begins with a single % character. Sometimes, however, one wants that a comment be preserved in the “code” file too, for example because it contains a critical warning that users must absolutely see. For this, the docstrip program introduces the concept of “meta-comment”: a line that begins with the string contained in the \MetaPrefix macro is passed on to the “code” file, even if it is a comment. The default value of \MetaPrefix is \DoubleperCent (sic), which, in turn, is defined as %%; in practice, this means that, with the default setting, comments beginning with %% in columns 1 and 2 are left with the code, and not removed like the remaining ones.

Moreover, for consistency, the docstrip program inserts the \MetaPrefix at the beginning of each line of the preamble and of the postamble that it automatically adds to the code files. Thus, it is customary to see package or class files that begin with

%%
%% This is file `somepack.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%

etcetera, and end with

%%
%% End of file `somepack.sty'.

Of course, it is also possible that a programmer—perhaps after seeing examples of comments generated by the docstrip program—decides to use two, or even more, percent sign just to make a comment more visible.

GuM
  • 21,558
  • 1
    emacs uses lines starting with %%% to mark the file as LaTeX, and what it's master file (the one ultimately including this one) is. – vonbrand Aug 03 '15 at 18:20