When are underscores allowed and when are they not allowed?












2














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.
















  • 2




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
















2














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.
















  • 2




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














2












2








2







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 2 hours ago









IdleCustardIdleCustard

764




764




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.








  • 2




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














  • 2




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








2




2




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




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










2 Answers
2






active

oldest

votes


















5














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































    1














    By default, the _ (underscore) character is "special" in TeX and LaTeX: it serves to initiate subscript material. Assuming the default settings are in force, the only times _ does not have to be escaped (by writing "_") outside of math mode occur if the character is in the argument of macros such as verb, detokenize, and url. What these macros have in common is that they strip characters in their arguments of "special" properties. (More precisely, these macros assign cat code 11 ("other") to all characters in their argument, and "other"-code characters can be printed without escaping them.)



    And, as you've discovered, _ is also allowed in the arguments of label and ref. That's because these two macros remove the "special" nature of some, but not all, "special" characters. Conversely, label and ref do not accept macros inside their arguments. That's why label{LAB_ BY} fails, since _ is a macro.






    share|improve this answer





















    • I think that's a bit misleading, macros work fine in label, def_{x}section{hhh}label{LAB_BY} works and is equivalent to label{LABxBy} it doesn't fail because it's a macro, it fails because it doesn't expand to character tokens.
      – David Carlisle
      2 hours ago











    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









    5














    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




























      5














      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


























        5












        5








        5






        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 2 hours ago

























        answered 2 hours ago









        David CarlisleDavid Carlisle

        484k4011171858




        484k4011171858























            1














            By default, the _ (underscore) character is "special" in TeX and LaTeX: it serves to initiate subscript material. Assuming the default settings are in force, the only times _ does not have to be escaped (by writing "_") outside of math mode occur if the character is in the argument of macros such as verb, detokenize, and url. What these macros have in common is that they strip characters in their arguments of "special" properties. (More precisely, these macros assign cat code 11 ("other") to all characters in their argument, and "other"-code characters can be printed without escaping them.)



            And, as you've discovered, _ is also allowed in the arguments of label and ref. That's because these two macros remove the "special" nature of some, but not all, "special" characters. Conversely, label and ref do not accept macros inside their arguments. That's why label{LAB_ BY} fails, since _ is a macro.






            share|improve this answer





















            • I think that's a bit misleading, macros work fine in label, def_{x}section{hhh}label{LAB_BY} works and is equivalent to label{LABxBy} it doesn't fail because it's a macro, it fails because it doesn't expand to character tokens.
              – David Carlisle
              2 hours ago
















            1














            By default, the _ (underscore) character is "special" in TeX and LaTeX: it serves to initiate subscript material. Assuming the default settings are in force, the only times _ does not have to be escaped (by writing "_") outside of math mode occur if the character is in the argument of macros such as verb, detokenize, and url. What these macros have in common is that they strip characters in their arguments of "special" properties. (More precisely, these macros assign cat code 11 ("other") to all characters in their argument, and "other"-code characters can be printed without escaping them.)



            And, as you've discovered, _ is also allowed in the arguments of label and ref. That's because these two macros remove the "special" nature of some, but not all, "special" characters. Conversely, label and ref do not accept macros inside their arguments. That's why label{LAB_ BY} fails, since _ is a macro.






            share|improve this answer





















            • I think that's a bit misleading, macros work fine in label, def_{x}section{hhh}label{LAB_BY} works and is equivalent to label{LABxBy} it doesn't fail because it's a macro, it fails because it doesn't expand to character tokens.
              – David Carlisle
              2 hours ago














            1












            1








            1






            By default, the _ (underscore) character is "special" in TeX and LaTeX: it serves to initiate subscript material. Assuming the default settings are in force, the only times _ does not have to be escaped (by writing "_") outside of math mode occur if the character is in the argument of macros such as verb, detokenize, and url. What these macros have in common is that they strip characters in their arguments of "special" properties. (More precisely, these macros assign cat code 11 ("other") to all characters in their argument, and "other"-code characters can be printed without escaping them.)



            And, as you've discovered, _ is also allowed in the arguments of label and ref. That's because these two macros remove the "special" nature of some, but not all, "special" characters. Conversely, label and ref do not accept macros inside their arguments. That's why label{LAB_ BY} fails, since _ is a macro.






            share|improve this answer












            By default, the _ (underscore) character is "special" in TeX and LaTeX: it serves to initiate subscript material. Assuming the default settings are in force, the only times _ does not have to be escaped (by writing "_") outside of math mode occur if the character is in the argument of macros such as verb, detokenize, and url. What these macros have in common is that they strip characters in their arguments of "special" properties. (More precisely, these macros assign cat code 11 ("other") to all characters in their argument, and "other"-code characters can be printed without escaping them.)



            And, as you've discovered, _ is also allowed in the arguments of label and ref. That's because these two macros remove the "special" nature of some, but not all, "special" characters. Conversely, label and ref do not accept macros inside their arguments. That's why label{LAB_ BY} fails, since _ is a macro.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 2 hours ago









            MicoMico

            274k30371758




            274k30371758












            • I think that's a bit misleading, macros work fine in label, def_{x}section{hhh}label{LAB_BY} works and is equivalent to label{LABxBy} it doesn't fail because it's a macro, it fails because it doesn't expand to character tokens.
              – David Carlisle
              2 hours ago


















            • I think that's a bit misleading, macros work fine in label, def_{x}section{hhh}label{LAB_BY} works and is equivalent to label{LABxBy} it doesn't fail because it's a macro, it fails because it doesn't expand to character tokens.
              – David Carlisle
              2 hours ago
















            I think that's a bit misleading, macros work fine in label, def_{x}section{hhh}label{LAB_BY} works and is equivalent to label{LABxBy} it doesn't fail because it's a macro, it fails because it doesn't expand to character tokens.
            – David Carlisle
            2 hours ago




            I think that's a bit misleading, macros work fine in label, def_{x}section{hhh}label{LAB_BY} works and is equivalent to label{LABxBy} it doesn't fail because it's a macro, it fails because it doesn't expand to character tokens.
            – David Carlisle
            2 hours ago










            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

            flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

            Mangá

            Eduardo VII do Reino Unido