I see that the code in many packages and examples contains percent signs % at the end of (many) lines. What are they used for? Do they affect the parsing of those lines?
- 88,848
- 10,294
-
2Illustration of a potential problem that could arise if one is not careful: Tex Capacity Exceeded (if remove % after use of macro). – Peter Grill Nov 21 '12 at 20:42
-
3After seeing comments that this question is not easily found, resulting in many questions for which it has been listed as duplicate, I have added an "alternate" question intended to make finding it easier. – barbara beeton May 19 '20 at 13:50
-
@barbarabeeton Sounds like a good change. You can even swap the two titles to have "why is my macro creating extra space" in front (and reference it in the text), I won't object to that. – Federico Poloni May 24 '20 at 09:01
-
@FedericoPoloni -- Thanks for the confirmation. I think I'll stick with the "minimal" change; this is really your question, and it's a good one, clearly recognized by many as a puzzling circumstance. The addition is actually sort of an answer, so I'd rather not "pollute" your nice clean statement. (And it's nice to see you still around after such a long time.) – barbara beeton May 24 '20 at 13:01
-
See also: macros - When is it harmful to add percent character at end of lines in a \newcommand, or similar - TeX - LaTeX Stack Exchange – user202729 Dec 30 '21 at 09:02
6 Answers
The short answer is what others have said, % starts a comment that goes to the end of the line. The normal effect is that it doesn't insert the space (or a \par) from the newline.
The longer answer is that as TeX parses its input, it reads the input file line by line. It strips off tailing whitespace (including any carriage return and newline) and then appends a character corresponding to the number in the \endlinechar register (provided it isn't -1 or greater than 255). By default, the value is 13 which corresponds to an ASCII carriage return. (TeX denotes this by ^^M.) You can change this register to be any value.
Unless the category codes have been changed (e.g., by the \obeylines macro), ^^M has category code 5, or end of line. When TeX reaches an end of line character, it discards the rest of the line and does one of three things: nothing, insert a space token, or insert a \par token, depending on what state TeX was in (S, M, or N, respectively).
So what does this have to do with %? Well, since the comment character causes TeX to ignore the rest of the input line, the end of line character that got appended gets ignored too.
This can frequently be important when playing around with category codes of ^^M (again, using \obeylines or similar).
The long answer is contained in Chapter 8 of The TeXbook.
One final use that no one has mentioned is that it is sometimes necessary for the line to end with a space character and not a end of line character. One example is that a backslash followed by a space is different than a backslash followed by a newline:
\show\
\show\ %
In the first line, there's a space following the \, but it'll get stripped off as described so what you get instead \^^M as you can see by what TeX prints out.
> \^^M=macro:
->\ .
That is, \^^M is a macro that expands to a control space: \ . In the second case, the comment prevents the space from being stripped off and the end of line char is ignored. TeX prints out the following.
> \ =\ .
That is, \ is a TeX primitive (see control space in either The TeXbook or TeX by Topic).
The usual reason for % is suppressing spaces in macro definitions. Consider the macros \nopercents and \percents below.
\documentclass{minimal}
\newcommand*\bracket[1]{[#1]}%
\newcommand*\nopercents[1]{
\bracket{#1}
}
\newcommand*\percents[1]{%
\bracket{#1}%
}
\begin{document}
X\nopercents{blah}X
X\percents{blah}X
\end{document}
Superficially, they appear to do the same thing: pass their input to \bracket. The difference is that the newlines becomes space tokens in \nopercents but are ignored in \percents due to the %. So X\nopercents{blah}X expands to X [blah] X whereas X\percents{blah}X expands to X[blah]X.
Addendum regarding spaces at the beginning of a line.
A % only swallows whatever follows it on a line.
It does not have any effect on white spaces that begin the next line.
Under most circumstances, spaces at the beginning of a line are ignored
by TeX itself. There are a couple of exceptions:
When the line is otherwise entirely blank, it is interpreted as a paragraph break.
When
\obeyspacesis in effect, every space is carried into the output; this is true withinverbatimmode and can also be requested explicitly.
If \obeyspaces is in effect while a command or environment is being defined, if the definition occupies more than one line, any spaces at the beginning of a continuation line will be preserved in the definition.
Indentation is often used in defining commands or environments to make the code easier to understand (usually a good thing), but while \obeyspaces is in effect, this has an unwelcome result and should be avoided.
\obeyspaces should normally be used only within a delimited scope ({ } or \begingroup ... \endgroup) to avoid unwanted results.
- 88,848
- 62,639
-
- I thought that a percent sign also swallows all initial whitespace from the next line? 2. When writing macros, in which situations would one (broadly speaking) want to end lines with a percent sign? (I know someone might be tempted to answer "whenever you don't want a whitespace there", but I'm hoping for a list or a pointer to a list of common pitfalls and traps.)
– Lover of Structure Sep 12 '12 at 18:56 -
10@user14996 -- a % only swallows the end-of-line character. tex itself in most cases is what swallows (rather ignores) spaces at the beginning of a line; it's possible to turn this off, with, e.g.,
\obeyspaces, and this is very useful when quoting blocks of code, to preserve meaningful indentation. – barbara beeton Sep 28 '12 at 18:40 -
@barbarabeeton Thanks for this piece of info, because for a long time I have believed otherwise, possibly because of the following statement on Wikibooks: "When LaTeX encounters a % character while processing an input file, it ignores the rest of the current line, the line break, and all whitespace at the beginning of the next line." (emph. added) As I am not an expert, I won't correct this, but perhaps one of you might clarify the intricacies of this either there or (better) here? (And, did you mean
\obeywhitespace?) – Lover of Structure Sep 29 '12 at 16:30 -
-
8@user14996 -- it's true that a
%causes (la)tex to ignore whatever comes after on that line, including, of course, the line break. but it's tex itself that ignores spaces at the beginning of a line, and a totally blank line (no%allowed, so that the line break can be seen) will be recognized as a paragraph break, equivalent to\par. the sources you cite are mistaken. and regarding\obeyspaces, that's defined inplain.texand in the texbook, p.352. i'm not familiar with\obeywhitespace, and don't find it in my trusted collection of reference books. – barbara beeton Sep 29 '12 at 19:06 -
@barbarabeeton Are there any scenarios where I shouldn't use line-initial whitespace? – Lover of Structure Oct 06 '12 at 22:20
-
2@user14996 -- line-initial whitespace is preserved by (la)tex within most "verbatim" environments. when a new command is being defined within such an environment, spaces must not be input at the beginnings of lines to make the definition easier for someone to read (usually a polite thing to do, as well as being helpful when one has to diagnose problems); such spaces will be carried into the output. – barbara beeton Oct 07 '12 at 11:22
-
@barbarabeeton Can you give 1 or 2 examples of when something is defined "within a 'verbatim' environment"? – Lover of Structure Oct 07 '12 at 17:56
-
@user14996 -- these two files (in tex live or ctan) have such definitions:
eplain.texstarting with the line\gdef\obeywhitespace{%, andtugboat.styin the section% Definitions of spaces and ^^M. there's also a block inlatex.ltxjust after the line\message{verbatim,}. this code is rather arcane, but if there were spaces at the beginnings of the affected lines, it would wreak havoc. – barbara beeton Oct 08 '12 at 13:59 -
@barbarabeeton Many thanks for your examples. So I think that in nearly all "user" contexts of LaTeX including those of the user occasionally defining his own commands and doing macro tricks, line-initial whitespace can be liberally used. Please contradict me if this is not correct. – Lover of Structure Oct 08 '12 at 23:08
-
3@user14996 -- most "ordinary" users should feel free to use line-initial whitespace to make input easier to comprehend. the only time it's really necessary to "be careful" is in verbatim-type environments, especially when defining commands to be used in such environments. with this one important exception, line-initial whatespace, used consistently, is generally a good thing. – barbara beeton Oct 09 '12 at 17:46
-
so is it considdered a good practice to add % after each line (when/where possible) in a macro/class definition? – Pieter Stroobants Nov 11 '14 at 04:47
-
3@PieterStroobants If the line ends with a control word (a control sequence consisting of catcode 11 characters)—but not a control word (a control sequence consisting of 1 character with catcode different from 11) nor a control space—spaces are ignored so there's no need for
%. If the line ends with another token, the end of line will become a space. If you don't want the space, use%. Sometimes you want the space, e.g., after number or glue specifications:\count@=37you wouldn't use%so TeX inserts the space and thus ends the number. See TeXbyTopic, section 2.10.2 for details. – TH. May 11 '16 at 20:12 -
@TH. -- there are a couple of new comments on the answer by Kit, below, that make me think it might be useful to add an addendum to your answer regarding spaces at the beginning of a line. i'd be happy to condense my comments here into such an addendum, but don't want to do that without your permission. – barbara beeton May 21 '17 at 20:48
-
This answer gives a nice description of how the parser treats %. But as for why you would do it, this answer only mentions that it's useful (1) when playing around with category codes of
^^M, and (2) when you need your line to end with a space. Since neither of these is usually the case, how do you explain the rampant usage of %? Surely it has to do with the annoying too-much-space-in-the-document phenomenon when a % is "forgotten" in a macro -- could this please be explained? Specifically, why is a newline from inside a macro any different from a newline next to where the macro is used? – Matt May 21 '17 at 23:08 -
-
@Matt, I guess the second sentence hints at it, "The normal effect is that it doesn't insert the space…" Maybe I can make that more explicit. – TH. May 22 '17 at 01:54
-
@TH: That does not explain the standard confusion that people have about this. A macro is expected to simply substitute its contents into the place where it is used. So if a macro contains some whitespace, then that whitespace would get "plugged into" the text where the macro was used. But extra spaces and newlines are no problem in the text! (Although a double newline would start a paragraph.) So why is it a problem when extra spaces and/or newlines come from a macro? Why do we see extra-wide spaces between words in that case? – Matt May 22 '17 at 10:14
-
@Matt, I'm not sure I follow. Macro expansion replaces the macro token (and any argument tokens) with the replacement tokens (it gets called the replacement text, but it's really tokens because it has already been tokenized). It's not a textual replacement, but TeX's parsing rules don't change. Maybe you're thinking of something like vertical mode where space tokens and
\parcommands are ignored. For macros designed to be used in vertical mode, extra spaces in the macro definitions waste memory but are otherwise harmless. – TH. May 22 '17 at 16:18 -
@Matt, maybe this?
\def\foo#1{ #1 }Then,X \foo{bar} Xexpands toX bar X(with four space tokens) whereas had I writtenX bar X(with four space characters), TeX would produceX bar X(with two space tokens). The difference comes from when the replacement text of\foois tokenized, it contains a leading and trailing space token. SoX \foo{bar} Xyields anXtoken, a space token,\foo{bar}is expanded to a space token,bar, and another space token. Then, there's a space token and the finalXtoken. https://texhacks.blogspot.com/2009/10/spaces-in-tex.html may help. – TH. May 22 '17 at 16:30 -
StackExchange ate my extra spaces. Everywhere I wrote (with four space characters) should really have two sets of double spaces. – TH. May 22 '17 at 16:32
-
@TH: Yes, your comments here are exactly on target. I love your new section, "The usual reason for %..." And the main reason you need to use them, if I understand you, is that whitespace regions in the source are only reduced to a single space before macro substitutions are done. (I'm sure this is so obvious to you that it needs no mention, but it is not obvious before you learn it!) So actually I would update your new section to use the four-space-character example as in your comment, since I think that illustrates this issue nicely. – Matt May 23 '17 at 12:27
-
You already got lots of answers. You can also just experiment yourself:
\documentclass{article}
\begin{document}
Hello%
world!
\end{document}
Try compiling this with and without the %. Then you see yourself that the % makes the space produced by the newline disappear. (Note that you'll still get the space if you write Hello % with a space before the % – try it out!) All the details are given in TH's great answer.
- 37,935
A percent sign, %, allows to end a line without generating a space character -- very useful when writing macros.
- 8,890
When I was starting out with TeX, I have read many times that the percent sign "swallows" all the whitespace after it. Whitespace includes spaces, tabs, and linebreaks.
- 16,430
-
15@Kit -- actually, a percent sign swallows everything that follows it, not just whitespace. so it can also be used as a really convenient mechanism to insert by-line comments in macro code (and other input). – barbara beeton Oct 12 '12 at 20:39
-
1@barbarabeeton -- I think this answer is referring to the fact that it swallows all the initial whitespace on the next line. Since a common use of % is to control presence/absence of whitespace, this is good to know, and none of the other answers have mentioned it. It means that even when you are formatting your lines via indentation, using % will still work to eliminate whitespace between lines. – Matt May 21 '17 at 16:06
-
@barbarabeeton -- Ah, I see now that this is discussed in the comments under the first answer. – Matt May 21 '17 at 16:18
When % is not the answer
In the middle of a control sequence.
\ta%
u
will not be reconstituted into \tau. A control sequence must be input as an unbroken unit.
- After a number or <dimen> that is being used in an assignment. TeX will continue reading until the next token is definitely not numeric, or in the case of a <dimen> not
plusorminus. Use\relaxinstead.
These exceptions are already covered, but "buried", so a more prominent reminder is warranted.
Another possibility, not as easily identified and often not mentioned in user documentation, is insertion of "padding" by a package, as in this example.
\documentclass{article}
\usepackage{tcolorbox}
\newcommand{\pink}[1]{\colorbox{red!20}{#1}}
\newcommand{\Pink}[1]{{\fboxsep=0pt\colorbox{red!20}{#1}}}
\begin{document}
compress \par
com\pink{p}ress \par
com\Pink{p}ress
\end{document}
This isn't usual, but action by a package should be considered when more likely possibilities fail to cure the problem.
- 88,848
The question of the use of the symbol % is dealt with in numerous TeX.SE questions (see a non-exhaustive list at the end), so this answer aims to make a summary of the practical recommendations concerning the use of the percent symbol.
Outline
- How
TeX"deals with spaces"? - Practical recommendations concerning the use of the percent symbol
- Related posts
- Other resources
1. How TeX "deals with spaces"?
The rules applied to spaces (except inside verbatim/math mode or when \obeyspaces is in effect) are listed in this Martin Scharrer's answer.
For deeper explanations and "traps" regarding how TeX "deals with spaces":
- Mico's answer (for beginners this answer is quite complete, very pedagogical and plenty of practical examples).
- TH.'s answer here.
- egreg's answer (introducing also the three TeX modes: horizontal, vertical or math).
- another egreg's answer introducing the use of
\relax,\ignorespacesand also the issues related to spaces after a constant number (and much more!). - This blog introducing the different states of TeX when reading input.
- Chapter 8 of The TeXbook.
2. Practical recommendations concerning the use of the percent symbol
Use a
%when you wantTeX"to exclude everything that's on the remainder of the current input line from further processing" (including the "invisible" end-of-line character), useful for writing a comment for e.g.No need to use
%sign in preamble (except in macro definition, see below). So NO need of%after the closing}of a macro definition.No need to use
%sign between two consecutive undelimited arguments (details here): Then\parbox {3cm} {text}is equivalent to\parbox {3cm} {text}In macro definition [details here]:
A%will prevent from possible extra spaces before AND after the wordbarin the following example (unless the macro is used in math mode):\newcommand{\foo}{% bar% }Which is equivalent to
\newcommand{\foo}{bar}OR:\newcommand{\foo}{% bar}But it is not optimal because in some cases (depending on the context at the macro "call"), there will be no spurious spaces "added" before and/or after the word
barin the output, then the%signs will be unnecessary (but not problematic).So, if you really wants to optimise/minimise the use of the
%sign in your macro definition, then take a look at this egreg's answer and see also ShreevatsaR's answer for good practice regarding macro writing.Never put a
%sign just after a constant number but always PUT a space or a new line or a\relaxinstead (for e.g. - hereafter the_symbol stands as a space character - do\ifnum\mycounter<13_or\mycount=123_or\mycount=123\relaxbut NOT\mycount=123%), details here and here.- About the use of
\relax(especially in "incomplete glue assignment") an possible issues see this answer and also this one.
- About the use of
Put a
%between two consecutives\end{subfigure}and\begin{subfigure}or\end{minipage}and\begin{minipage}if you want to avoid undesired space (which could induce non alignedsubfigureorminipage, e.g. and detailed explanation here) BUT an\hfillwill do the trick (even "better/cleaner") also (see here).No need to put a
%after a control sequence (e.g.\foo) because the space is already ignored.In LaTeX3 syntax, spaces are ignored in macro definition unless you explicitly specify the space with a
~character (see Leo Liu's answer).
3. Related posts
Legend : date in mm/dd/yyyy format.
| # | Question | Answer/comment | Insight |
|---|---|---|---|
| 1 | Where are the necessary places to be appended with % to remove unwanted spaces? [duplicate] 06/05/2011 | Leo Liu's answer | Overview of different way to "deal" with space in macro def |
| 2 | // | egreg's answer (cited in §1) | Introducing the use of \relax, \ignorespaces and also the use of spaces after a constant number and undelimited arguments |
| 3 | Tex Capacity Exceeded (if remove % after use of macro) 06/13/2011 | Ryan Reich's answer | Illustrative problem with spurious spaces into foreach loop, main rules applied to spaces presented + some maro definition examples |
| 4 | When is it harmful to add percent character at end of lines in a \newcommand, or similar 11/14/2011 | egreg's answer (cited in §1) | Introducing the three TeX modes (horizontal, vertical or math) and related effect on space tokenization, the use of spaces after a constant number and exercises |
| 5 | // | Martin Scharrer's answer (cited in §1) | Listing the rules applied to spaces |
| 7 | Why the end-of-line % in macro definitions? [duplicate] 01/13/2012 | Martin Scharrer's answer | Differentiating macro with and without argument and the use of braces {} AND notice that the "(La)TeX rules can be dynamically changed" |
| 8 | Unwanted spaces [duplicate] - 04/18/2012 | egreg's answer | Spurious spaces caused by missing % in macro def |
| 9 | Do assignments need to be appended by % to remove the trailing white spaces? 04/09/2013 | egreg's answer (cited in §2) | Focus on the use of % sign after register, parameter, constant number, and focus on \relax |
| 10 | // | Ryan Reich's answer | Explanation of how TeX scan "a dimension" or "a register" |
| 11 | // | Joseph Wright's answer | A bit of details about tokenization of macros |
| 12 | Does the comment symbol % remove the carriage return at the end of the line? | egreg's answer (cited in §2) | Description of the tokenization process and examples where % after a control sequence can be harmful. |
| 13 | Unwanted spaces in user defined structures and the uses of % [duplicate] 03/16/2018 | Steven B. Segletes' answer | Details about the use of \relax after constant number and possible issues AND "indentation and vertical mode" AND "brackets [ ] and Optional Arguments" |
| 14 | // | ShreevatsaR's answer | Good practice regarding macro writing in order to avoid spurious spaces |
| 15 | How to compare number from \newenvironment argument 11/14/2018 | Phelype Oleinik's answer | Practical example on the usage of % and ifnum |
| 16 | (La)TeX -- What does the '%' character do? [duplicate] 10/13/2020 | Mico's answer (cited in §1) | Interesting answer for beginners: presentations of the main rules regarding spaces, % usage in macro and after constant number, illustrate by practical examples |
4. Other resources
| # | Resource | Remark | Source |
|---|---|---|---|
| 1 | The TeXbook, Chapter 20 | "For general knowledge of macro definitions" | Yan Zhou's answer |
| 2 | TeX by Topic "distributed with TeXLive" | "Good introduction to how TeX read in your input" | Yan Zhou's answer |
| 3 | egreg's article, "Recollections of a spurious space catcher", TUGboat, Volume 36 (2015), No. 2), p.149-161 | ShreevatsaR's answer | |
| 4 | egreg's video, "Recollections of a spurious space catcher" | ShreevatsaR's answer |
- 191
- 779
-
This answer has been set to Community wiki in order to be easily editable. So, please don't hesitate to improve it! – zetyty Jun 05 '23 at 12:23
