When are underscores allowed and when are they not allowed?












4














I used to think that the behavior of underscores ('_') was as follows:



IF ('_') {
BUFFER NEXT ITEM
MAKE THE NEXT ITEM A SUBSCRIPT
SEND SUBSCRIPT TO OUT STREAM
}
ELSE IF ('_')
SEND UNDERSCORE CHARACTER TO OUT STREAM


I was wrong.



%% Use underscore for subscript while not in math mode
%% ERROR!
%X_5

% Use underscore in math mode to make a subscript
% No error
$ X_{subscript} $

% Escape Sequence (backslash) tells LaTeX we want underscore char
% instead of '_' means 'make a subscript'
% No error
X _ _ _ Oh look! underscore characters.

% underscore in the label name
% No error
begin{equation}
x = y label{LAB_BY}
end{equation}

%% Do not want subscript functionality of '_'
%% Use backslash to put an underscore into the label name
%% put a space after '_' so that after escape reads only '_' and not '_BY'
%% ERROR
%begin{equation}
% x = y label{LAB_ BY}
%end{equation}

%% Do not want subscript functionality of '_'
%% Use backslash to put an underscore into the label name
%% fail to put a space char after '_'
%% ERROR
%begin{equation}
% x = y label{LAB_BY}
%end{equation}


When are underscores allowed and when are they not allowed?

What does '_' signify if you are not in math mode?










share|improve this question







New contributor




IdleCustard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 4




    As for "underscores in math mode" : your document says : "Use underscore in math mode to make a subscript".
    – marmot
    yesterday
















4














I used to think that the behavior of underscores ('_') was as follows:



IF ('_') {
BUFFER NEXT ITEM
MAKE THE NEXT ITEM A SUBSCRIPT
SEND SUBSCRIPT TO OUT STREAM
}
ELSE IF ('_')
SEND UNDERSCORE CHARACTER TO OUT STREAM


I was wrong.



%% Use underscore for subscript while not in math mode
%% ERROR!
%X_5

% Use underscore in math mode to make a subscript
% No error
$ X_{subscript} $

% Escape Sequence (backslash) tells LaTeX we want underscore char
% instead of '_' means 'make a subscript'
% No error
X _ _ _ Oh look! underscore characters.

% underscore in the label name
% No error
begin{equation}
x = y label{LAB_BY}
end{equation}

%% Do not want subscript functionality of '_'
%% Use backslash to put an underscore into the label name
%% put a space after '_' so that after escape reads only '_' and not '_BY'
%% ERROR
%begin{equation}
% x = y label{LAB_ BY}
%end{equation}

%% Do not want subscript functionality of '_'
%% Use backslash to put an underscore into the label name
%% fail to put a space char after '_'
%% ERROR
%begin{equation}
% x = y label{LAB_BY}
%end{equation}


When are underscores allowed and when are they not allowed?

What does '_' signify if you are not in math mode?










share|improve this question







New contributor




IdleCustard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 4




    As for "underscores in math mode" : your document says : "Use underscore in math mode to make a subscript".
    – marmot
    yesterday














4












4








4







I used to think that the behavior of underscores ('_') was as follows:



IF ('_') {
BUFFER NEXT ITEM
MAKE THE NEXT ITEM A SUBSCRIPT
SEND SUBSCRIPT TO OUT STREAM
}
ELSE IF ('_')
SEND UNDERSCORE CHARACTER TO OUT STREAM


I was wrong.



%% Use underscore for subscript while not in math mode
%% ERROR!
%X_5

% Use underscore in math mode to make a subscript
% No error
$ X_{subscript} $

% Escape Sequence (backslash) tells LaTeX we want underscore char
% instead of '_' means 'make a subscript'
% No error
X _ _ _ Oh look! underscore characters.

% underscore in the label name
% No error
begin{equation}
x = y label{LAB_BY}
end{equation}

%% Do not want subscript functionality of '_'
%% Use backslash to put an underscore into the label name
%% put a space after '_' so that after escape reads only '_' and not '_BY'
%% ERROR
%begin{equation}
% x = y label{LAB_ BY}
%end{equation}

%% Do not want subscript functionality of '_'
%% Use backslash to put an underscore into the label name
%% fail to put a space char after '_'
%% ERROR
%begin{equation}
% x = y label{LAB_BY}
%end{equation}


When are underscores allowed and when are they not allowed?

What does '_' signify if you are not in math mode?










share|improve this question







New contributor




IdleCustard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I used to think that the behavior of underscores ('_') was as follows:



IF ('_') {
BUFFER NEXT ITEM
MAKE THE NEXT ITEM A SUBSCRIPT
SEND SUBSCRIPT TO OUT STREAM
}
ELSE IF ('_')
SEND UNDERSCORE CHARACTER TO OUT STREAM


I was wrong.



%% Use underscore for subscript while not in math mode
%% ERROR!
%X_5

% Use underscore in math mode to make a subscript
% No error
$ X_{subscript} $

% Escape Sequence (backslash) tells LaTeX we want underscore char
% instead of '_' means 'make a subscript'
% No error
X _ _ _ Oh look! underscore characters.

% underscore in the label name
% No error
begin{equation}
x = y label{LAB_BY}
end{equation}

%% Do not want subscript functionality of '_'
%% Use backslash to put an underscore into the label name
%% put a space after '_' so that after escape reads only '_' and not '_BY'
%% ERROR
%begin{equation}
% x = y label{LAB_ BY}
%end{equation}

%% Do not want subscript functionality of '_'
%% Use backslash to put an underscore into the label name
%% fail to put a space char after '_'
%% ERROR
%begin{equation}
% x = y label{LAB_BY}
%end{equation}


When are underscores allowed and when are they not allowed?

What does '_' signify if you are not in math mode?







underscore






share|improve this question







New contributor




IdleCustard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




IdleCustard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




IdleCustard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









IdleCustardIdleCustard

964




964




New contributor




IdleCustard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





IdleCustard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






IdleCustard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 4




    As for "underscores in math mode" : your document says : "Use underscore in math mode to make a subscript".
    – marmot
    yesterday














  • 4




    As for "underscores in math mode" : your document says : "Use underscore in math mode to make a subscript".
    – marmot
    yesterday








4




4




As for "underscores in math mode" : your document says : "Use underscore in math mode to make a subscript".
– marmot
yesterday




As for "underscores in math mode" : your document says : "Use underscore in math mode to make a subscript".
– marmot
yesterday










2 Answers
2






active

oldest

votes


















11














No special behaviour is assigned to any character in TeX, everything depends on the current catcode regime.



If catcode95=11 (often written as catcode`_=11 then _ is a letter and you can use it anywhere you can use x so



catcode`_=11

a_b defone_two_three{four} one_two_three


is all good and would typeset a_b four.



But normally _ has catcode 8 which means it has a subscript meaning if encountered in math mode, and an error if the character token would otherwise be typeset directly in text mode.



However other uses of the token, it is just a character token so for example



newcommandfoo{a___jd_ _ }


is legal and defines foo to be that sequence of tokens (it may possibly generate an error if used, but not necessarily, depending on context).



Similarly in a write or csname (both constructs used by LaTeX's label macro) any non active legal token just acts as itself so csname one_two_threeendcsname constructs the control sequence with name one_two_three which is the same as the one_two_three accessed above by use of catcode changes.



Note that _ is just the control sequence with name _ it is not forced to produce an underscore. It does by default in latex, but just as \ doesn't produce a backslash you could define _ to do anything:



def_{zzzzz} _


would produce zzzzz for example.



_ is not predefined by TeX, laTeX defines it to be the macro:



DeclareRobustCommand{_}{%
ifmmodenfss@text{textunderscore}elsetextunderscorefi}





share|improve this answer































    3














    I'll deal with the standard LaTeX settings.



    What's _?



    The character _ is special; if found in math mode it signals that a subscript is wanted and will basically turn into a subscript the token or braced group that follows. Using _{1} is recommended even if the subscript is just a single character.



    If _ is found outside math mode and TeX is typesetting, an error will be raised. On the other hand, TeX is not typesetting all the time. In some cases it absorbs tokens for processing them in different ways than printing something.



    This is the case, for instance, when reading the argument to label, ref or cite where _ can be used without worries.1. This is because here _ is used as a character for forming an internal command that is used for generating the required object (a label, a reference or a citation).



    What's _?



    The command _ is defined to print an underscore. While this might seem not a big deal, there are some complications due mainly to history: fonts need not have an underscore at position 95 (the ASCII decimal code for the underscore), so just telling TeX “please, typeset the character at position 95 in the current font” is not a good set of instruction in order to actually get an underscore in the printout.



    In any case, when you want an underscore in a label, you don't want to print it, but just using it as a marker.2 So _ is out of the question here.





    Footnotes



    1 Documents using not carefully written macros might break on label{LAB_BY}, if they set _ as an active character. A prominent case is the syntax package.



    2 See What are the valid names as labels? for what are the “legal” characters in a label.






    share|improve this answer





















      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "85"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });






      IdleCustard is a new contributor. Be nice, and check out our Code of Conduct.










      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f469274%2fwhen-are-underscores-allowed-and-when-are-they-not-allowed%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      11














      No special behaviour is assigned to any character in TeX, everything depends on the current catcode regime.



      If catcode95=11 (often written as catcode`_=11 then _ is a letter and you can use it anywhere you can use x so



      catcode`_=11

      a_b defone_two_three{four} one_two_three


      is all good and would typeset a_b four.



      But normally _ has catcode 8 which means it has a subscript meaning if encountered in math mode, and an error if the character token would otherwise be typeset directly in text mode.



      However other uses of the token, it is just a character token so for example



      newcommandfoo{a___jd_ _ }


      is legal and defines foo to be that sequence of tokens (it may possibly generate an error if used, but not necessarily, depending on context).



      Similarly in a write or csname (both constructs used by LaTeX's label macro) any non active legal token just acts as itself so csname one_two_threeendcsname constructs the control sequence with name one_two_three which is the same as the one_two_three accessed above by use of catcode changes.



      Note that _ is just the control sequence with name _ it is not forced to produce an underscore. It does by default in latex, but just as \ doesn't produce a backslash you could define _ to do anything:



      def_{zzzzz} _


      would produce zzzzz for example.



      _ is not predefined by TeX, laTeX defines it to be the macro:



      DeclareRobustCommand{_}{%
      ifmmodenfss@text{textunderscore}elsetextunderscorefi}





      share|improve this answer




























        11














        No special behaviour is assigned to any character in TeX, everything depends on the current catcode regime.



        If catcode95=11 (often written as catcode`_=11 then _ is a letter and you can use it anywhere you can use x so



        catcode`_=11

        a_b defone_two_three{four} one_two_three


        is all good and would typeset a_b four.



        But normally _ has catcode 8 which means it has a subscript meaning if encountered in math mode, and an error if the character token would otherwise be typeset directly in text mode.



        However other uses of the token, it is just a character token so for example



        newcommandfoo{a___jd_ _ }


        is legal and defines foo to be that sequence of tokens (it may possibly generate an error if used, but not necessarily, depending on context).



        Similarly in a write or csname (both constructs used by LaTeX's label macro) any non active legal token just acts as itself so csname one_two_threeendcsname constructs the control sequence with name one_two_three which is the same as the one_two_three accessed above by use of catcode changes.



        Note that _ is just the control sequence with name _ it is not forced to produce an underscore. It does by default in latex, but just as \ doesn't produce a backslash you could define _ to do anything:



        def_{zzzzz} _


        would produce zzzzz for example.



        _ is not predefined by TeX, laTeX defines it to be the macro:



        DeclareRobustCommand{_}{%
        ifmmodenfss@text{textunderscore}elsetextunderscorefi}





        share|improve this answer


























          11












          11








          11






          No special behaviour is assigned to any character in TeX, everything depends on the current catcode regime.



          If catcode95=11 (often written as catcode`_=11 then _ is a letter and you can use it anywhere you can use x so



          catcode`_=11

          a_b defone_two_three{four} one_two_three


          is all good and would typeset a_b four.



          But normally _ has catcode 8 which means it has a subscript meaning if encountered in math mode, and an error if the character token would otherwise be typeset directly in text mode.



          However other uses of the token, it is just a character token so for example



          newcommandfoo{a___jd_ _ }


          is legal and defines foo to be that sequence of tokens (it may possibly generate an error if used, but not necessarily, depending on context).



          Similarly in a write or csname (both constructs used by LaTeX's label macro) any non active legal token just acts as itself so csname one_two_threeendcsname constructs the control sequence with name one_two_three which is the same as the one_two_three accessed above by use of catcode changes.



          Note that _ is just the control sequence with name _ it is not forced to produce an underscore. It does by default in latex, but just as \ doesn't produce a backslash you could define _ to do anything:



          def_{zzzzz} _


          would produce zzzzz for example.



          _ is not predefined by TeX, laTeX defines it to be the macro:



          DeclareRobustCommand{_}{%
          ifmmodenfss@text{textunderscore}elsetextunderscorefi}





          share|improve this answer














          No special behaviour is assigned to any character in TeX, everything depends on the current catcode regime.



          If catcode95=11 (often written as catcode`_=11 then _ is a letter and you can use it anywhere you can use x so



          catcode`_=11

          a_b defone_two_three{four} one_two_three


          is all good and would typeset a_b four.



          But normally _ has catcode 8 which means it has a subscript meaning if encountered in math mode, and an error if the character token would otherwise be typeset directly in text mode.



          However other uses of the token, it is just a character token so for example



          newcommandfoo{a___jd_ _ }


          is legal and defines foo to be that sequence of tokens (it may possibly generate an error if used, but not necessarily, depending on context).



          Similarly in a write or csname (both constructs used by LaTeX's label macro) any non active legal token just acts as itself so csname one_two_threeendcsname constructs the control sequence with name one_two_three which is the same as the one_two_three accessed above by use of catcode changes.



          Note that _ is just the control sequence with name _ it is not forced to produce an underscore. It does by default in latex, but just as \ doesn't produce a backslash you could define _ to do anything:



          def_{zzzzz} _


          would produce zzzzz for example.



          _ is not predefined by TeX, laTeX defines it to be the macro:



          DeclareRobustCommand{_}{%
          ifmmodenfss@text{textunderscore}elsetextunderscorefi}






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited yesterday

























          answered yesterday









          David CarlisleDavid Carlisle

          484k4011171859




          484k4011171859























              3














              I'll deal with the standard LaTeX settings.



              What's _?



              The character _ is special; if found in math mode it signals that a subscript is wanted and will basically turn into a subscript the token or braced group that follows. Using _{1} is recommended even if the subscript is just a single character.



              If _ is found outside math mode and TeX is typesetting, an error will be raised. On the other hand, TeX is not typesetting all the time. In some cases it absorbs tokens for processing them in different ways than printing something.



              This is the case, for instance, when reading the argument to label, ref or cite where _ can be used without worries.1. This is because here _ is used as a character for forming an internal command that is used for generating the required object (a label, a reference or a citation).



              What's _?



              The command _ is defined to print an underscore. While this might seem not a big deal, there are some complications due mainly to history: fonts need not have an underscore at position 95 (the ASCII decimal code for the underscore), so just telling TeX “please, typeset the character at position 95 in the current font” is not a good set of instruction in order to actually get an underscore in the printout.



              In any case, when you want an underscore in a label, you don't want to print it, but just using it as a marker.2 So _ is out of the question here.





              Footnotes



              1 Documents using not carefully written macros might break on label{LAB_BY}, if they set _ as an active character. A prominent case is the syntax package.



              2 See What are the valid names as labels? for what are the “legal” characters in a label.






              share|improve this answer


























                3














                I'll deal with the standard LaTeX settings.



                What's _?



                The character _ is special; if found in math mode it signals that a subscript is wanted and will basically turn into a subscript the token or braced group that follows. Using _{1} is recommended even if the subscript is just a single character.



                If _ is found outside math mode and TeX is typesetting, an error will be raised. On the other hand, TeX is not typesetting all the time. In some cases it absorbs tokens for processing them in different ways than printing something.



                This is the case, for instance, when reading the argument to label, ref or cite where _ can be used without worries.1. This is because here _ is used as a character for forming an internal command that is used for generating the required object (a label, a reference or a citation).



                What's _?



                The command _ is defined to print an underscore. While this might seem not a big deal, there are some complications due mainly to history: fonts need not have an underscore at position 95 (the ASCII decimal code for the underscore), so just telling TeX “please, typeset the character at position 95 in the current font” is not a good set of instruction in order to actually get an underscore in the printout.



                In any case, when you want an underscore in a label, you don't want to print it, but just using it as a marker.2 So _ is out of the question here.





                Footnotes



                1 Documents using not carefully written macros might break on label{LAB_BY}, if they set _ as an active character. A prominent case is the syntax package.



                2 See What are the valid names as labels? for what are the “legal” characters in a label.






                share|improve this answer
























                  3












                  3








                  3






                  I'll deal with the standard LaTeX settings.



                  What's _?



                  The character _ is special; if found in math mode it signals that a subscript is wanted and will basically turn into a subscript the token or braced group that follows. Using _{1} is recommended even if the subscript is just a single character.



                  If _ is found outside math mode and TeX is typesetting, an error will be raised. On the other hand, TeX is not typesetting all the time. In some cases it absorbs tokens for processing them in different ways than printing something.



                  This is the case, for instance, when reading the argument to label, ref or cite where _ can be used without worries.1. This is because here _ is used as a character for forming an internal command that is used for generating the required object (a label, a reference or a citation).



                  What's _?



                  The command _ is defined to print an underscore. While this might seem not a big deal, there are some complications due mainly to history: fonts need not have an underscore at position 95 (the ASCII decimal code for the underscore), so just telling TeX “please, typeset the character at position 95 in the current font” is not a good set of instruction in order to actually get an underscore in the printout.



                  In any case, when you want an underscore in a label, you don't want to print it, but just using it as a marker.2 So _ is out of the question here.





                  Footnotes



                  1 Documents using not carefully written macros might break on label{LAB_BY}, if they set _ as an active character. A prominent case is the syntax package.



                  2 See What are the valid names as labels? for what are the “legal” characters in a label.






                  share|improve this answer












                  I'll deal with the standard LaTeX settings.



                  What's _?



                  The character _ is special; if found in math mode it signals that a subscript is wanted and will basically turn into a subscript the token or braced group that follows. Using _{1} is recommended even if the subscript is just a single character.



                  If _ is found outside math mode and TeX is typesetting, an error will be raised. On the other hand, TeX is not typesetting all the time. In some cases it absorbs tokens for processing them in different ways than printing something.



                  This is the case, for instance, when reading the argument to label, ref or cite where _ can be used without worries.1. This is because here _ is used as a character for forming an internal command that is used for generating the required object (a label, a reference or a citation).



                  What's _?



                  The command _ is defined to print an underscore. While this might seem not a big deal, there are some complications due mainly to history: fonts need not have an underscore at position 95 (the ASCII decimal code for the underscore), so just telling TeX “please, typeset the character at position 95 in the current font” is not a good set of instruction in order to actually get an underscore in the printout.



                  In any case, when you want an underscore in a label, you don't want to print it, but just using it as a marker.2 So _ is out of the question here.





                  Footnotes



                  1 Documents using not carefully written macros might break on label{LAB_BY}, if they set _ as an active character. A prominent case is the syntax package.



                  2 See What are the valid names as labels? for what are the “legal” characters in a label.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 17 hours ago









                  egregegreg

                  711k8618913174




                  711k8618913174






















                      IdleCustard is a new contributor. Be nice, and check out our Code of Conduct.










                      draft saved

                      draft discarded


















                      IdleCustard is a new contributor. Be nice, and check out our Code of Conduct.













                      IdleCustard is a new contributor. Be nice, and check out our Code of Conduct.












                      IdleCustard is a new contributor. Be nice, and check out our Code of Conduct.
















                      Thanks for contributing an answer to TeX - LaTeX Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f469274%2fwhen-are-underscores-allowed-and-when-are-they-not-allowed%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      How to make a Squid Proxy server?

                      Is this a new Fibonacci Identity?

                      19世紀