What is the use of percent signs (%) at the end of lines?












286















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?










share|improve this question




















  • 2





    Illustration 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
















286















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?










share|improve this question




















  • 2





    Illustration 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














286












286








286


89






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?










share|improve this question
















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?







macros symbols sourcecode syntax comments






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '12 at 19:18









doncherry

35k23135208




35k23135208










asked Dec 20 '10 at 14:30









Federico PoloniFederico Poloni

5,04743348




5,04743348








  • 2





    Illustration 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














  • 2





    Illustration 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








2




2





Illustration 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





Illustration 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










4 Answers
4






active

oldest

votes


















239














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}
Xnopercents{blah}X

Xpercents{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 Xnopercents{blah}X expands to X [blah] X whereas Xpercents{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 obeyspaces is in effect, every space is carried into the output; this is true within verbatim mode 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.






share|improve this answer





















  • 9





    @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






  • 6





    @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 in plain.tex and 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






  • 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 7 '12 at 11:22






  • 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 9 '12 at 17:46






  • 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@=37 you 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





















52














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.






share|improve this answer

































    41














    A percent sign, %, allows to end a line without generating a space character -- very useful when writing macros.






    share|improve this answer































      22














      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.






      share|improve this answer





















      • 10





        @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











      • @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










      protected by Joseph Wright Mar 2 '17 at 15:21



      Thank you for your interest in this question.
      Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



      Would you like to answer one of these unanswered questions instead?














      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      239














      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}
      Xnopercents{blah}X

      Xpercents{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 Xnopercents{blah}X expands to X [blah] X whereas Xpercents{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 obeyspaces is in effect, every space is carried into the output; this is true within verbatim mode 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.






      share|improve this answer





















      • 9





        @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






      • 6





        @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 in plain.tex and 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






      • 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 7 '12 at 11:22






      • 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 9 '12 at 17:46






      • 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@=37 you 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


















      239














      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}
      Xnopercents{blah}X

      Xpercents{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 Xnopercents{blah}X expands to X [blah] X whereas Xpercents{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 obeyspaces is in effect, every space is carried into the output; this is true within verbatim mode 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.






      share|improve this answer





















      • 9





        @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






      • 6





        @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 in plain.tex and 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






      • 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 7 '12 at 11:22






      • 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 9 '12 at 17:46






      • 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@=37 you 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
















      239












      239








      239







      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}
      Xnopercents{blah}X

      Xpercents{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 Xnopercents{blah}X expands to X [blah] X whereas Xpercents{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 obeyspaces is in effect, every space is carried into the output; this is true within verbatim mode 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.






      share|improve this answer















      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}
      Xnopercents{blah}X

      Xpercents{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 Xnopercents{blah}X expands to X [blah] X whereas Xpercents{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 obeyspaces is in effect, every space is carried into the output; this is true within verbatim mode 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.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited May 22 '17 at 16:42









      barbara beeton

      69.5k9157371




      69.5k9157371










      answered Dec 20 '10 at 15:49









      TH.TH.

      47.6k10130197




      47.6k10130197








      • 9





        @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






      • 6





        @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 in plain.tex and 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






      • 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 7 '12 at 11:22






      • 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 9 '12 at 17:46






      • 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@=37 you 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
















      • 9





        @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






      • 6





        @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 in plain.tex and 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






      • 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 7 '12 at 11:22






      • 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 9 '12 at 17:46






      • 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@=37 you 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










      9




      9





      @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





      @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




      6




      6





      @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 in plain.tex and 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





      @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 in plain.tex and 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




      2




      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 7 '12 at 11:22





      @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 7 '12 at 11:22




      3




      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 9 '12 at 17:46





      @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 9 '12 at 17:46




      3




      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@=37 you 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







      @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@=37 you 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













      52














      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.






      share|improve this answer






























        52














        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.






        share|improve this answer




























          52












          52








          52







          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.






          share|improve this answer















          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.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited May 16 '13 at 10:17

























          answered Dec 20 '10 at 16:06









          Hendrik VogtHendrik Vogt

          28.9k4107190




          28.9k4107190























              41














              A percent sign, %, allows to end a line without generating a space character -- very useful when writing macros.






              share|improve this answer




























                41














                A percent sign, %, allows to end a line without generating a space character -- very useful when writing macros.






                share|improve this answer


























                  41












                  41








                  41







                  A percent sign, %, allows to end a line without generating a space character -- very useful when writing macros.






                  share|improve this answer













                  A percent sign, %, allows to end a line without generating a space character -- very useful when writing macros.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 20 '10 at 14:38









                  chlchl

                  6,03042845




                  6,03042845























                      22














                      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.






                      share|improve this answer





















                      • 10





                        @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











                      • @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
















                      22














                      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.






                      share|improve this answer





















                      • 10





                        @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











                      • @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














                      22












                      22








                      22







                      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.






                      share|improve this answer















                      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.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Dec 20 '10 at 15:13

























                      answered Dec 20 '10 at 14:39









                      KitKit

                      7,5651958104




                      7,5651958104








                      • 10





                        @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











                      • @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














                      • 10





                        @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











                      • @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








                      10




                      10





                      @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





                      @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













                      @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 -- 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





                      @barbarabeeton -- Ah, I see now that this is discussed in the comments under the first answer.

                      – Matt
                      May 21 '17 at 16:18





                      protected by Joseph Wright Mar 2 '17 at 15:21



                      Thank you for your interest in this question.
                      Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                      Would you like to answer one of these unanswered questions instead?



                      Popular posts from this blog

                      How to send String Array data to Server using php in android

                      Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

                      Is anime1.com a legal site for watching anime?