Scope of LaTeX code












6















In C/C++ or other language, the code and variables have scope. Is this the same in LaTeX?



I struggled to understand the behavior of below code--



documentclass[doubleside]{article}
usepackage{lipsum}

begin{document}

%% case 1
%%
lipsum[5]
{
flushright
Huge
}

%% case 2
%%
lipsum[5]
{
Huge
flushright
}

%% case 3
%%
lipsum[5]

{
Huge
flushright
}

end{document}


Hope you can understand my difficulty of understanding what I got (maybe it's my familiarity with C/C++ that makes it difficult) --



enter image description here










share|improve this question




















  • 3





    Note if you want to compare with C, remember TeX is a macro expansion language not a compiled language, so it is far closer to the C pre-processor #define mechanism than it is to C itself.

    – David Carlisle
    Jan 25 at 8:41
















6















In C/C++ or other language, the code and variables have scope. Is this the same in LaTeX?



I struggled to understand the behavior of below code--



documentclass[doubleside]{article}
usepackage{lipsum}

begin{document}

%% case 1
%%
lipsum[5]
{
flushright
Huge
}

%% case 2
%%
lipsum[5]
{
Huge
flushright
}

%% case 3
%%
lipsum[5]

{
Huge
flushright
}

end{document}


Hope you can understand my difficulty of understanding what I got (maybe it's my familiarity with C/C++ that makes it difficult) --



enter image description here










share|improve this question




















  • 3





    Note if you want to compare with C, remember TeX is a macro expansion language not a compiled language, so it is far closer to the C pre-processor #define mechanism than it is to C itself.

    – David Carlisle
    Jan 25 at 8:41














6












6








6


0






In C/C++ or other language, the code and variables have scope. Is this the same in LaTeX?



I struggled to understand the behavior of below code--



documentclass[doubleside]{article}
usepackage{lipsum}

begin{document}

%% case 1
%%
lipsum[5]
{
flushright
Huge
}

%% case 2
%%
lipsum[5]
{
Huge
flushright
}

%% case 3
%%
lipsum[5]

{
Huge
flushright
}

end{document}


Hope you can understand my difficulty of understanding what I got (maybe it's my familiarity with C/C++ that makes it difficult) --



enter image description here










share|improve this question
















In C/C++ or other language, the code and variables have scope. Is this the same in LaTeX?



I struggled to understand the behavior of below code--



documentclass[doubleside]{article}
usepackage{lipsum}

begin{document}

%% case 1
%%
lipsum[5]
{
flushright
Huge
}

%% case 2
%%
lipsum[5]
{
Huge
flushright
}

%% case 3
%%
lipsum[5]

{
Huge
flushright
}

end{document}


Hope you can understand my difficulty of understanding what I got (maybe it's my familiarity with C/C++ that makes it difficult) --



enter image description here







scoping






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 25 at 8:50







Max

















asked Jan 25 at 8:08









MaxMax

856




856








  • 3





    Note if you want to compare with C, remember TeX is a macro expansion language not a compiled language, so it is far closer to the C pre-processor #define mechanism than it is to C itself.

    – David Carlisle
    Jan 25 at 8:41














  • 3





    Note if you want to compare with C, remember TeX is a macro expansion language not a compiled language, so it is far closer to the C pre-processor #define mechanism than it is to C itself.

    – David Carlisle
    Jan 25 at 8:41








3




3





Note if you want to compare with C, remember TeX is a macro expansion language not a compiled language, so it is far closer to the C pre-processor #define mechanism than it is to C itself.

– David Carlisle
Jan 25 at 8:41





Note if you want to compare with C, remember TeX is a macro expansion language not a compiled language, so it is far closer to the C pre-processor #define mechanism than it is to C itself.

– David Carlisle
Jan 25 at 8:41










1 Answer
1






active

oldest

votes


















13














tex has local and global scope as determined by groups ({...} and begin...end in your examples (begin...end forming a group as they are macros that expand to use of the tex primitive begingroup and endgroup group constructs.)



Commands can be defined to have local or global action but the ones you show are local, a global assignment is not restored when the group ends.



However your confusing output is caused by user error, flushright is not intended to be used as a command (the command form is raggedleft) it is the implementation of the start of the begin{flushright} end{flushright} environment.



TeX's linebreaking is optimised over a paragraph, using the settings at the end of the paragraph.



The important thing here is that (unlike raggedleft) flushright executes par so ends the previous paragraph so:



In the first case the paragraph is finished, and set with normal settings then locally huge font and ragged setting is set up but discarded at } before being used.



In the second case Huge fonts and baseline is set up, so the paragraph is set with a huge baseline when the par in flushright is executed.



In the third case, the paragraph is set with the normal settings by the implicit par at the blank line, and so the local settings in the following group are not used at all.






share|improve this answer


























  • It is perhaps worth noting that with a previous version of lipsum the same behavior would not show; it does now because lipsum issues par at the beginning rather than at the end. This can be exemplified by {Hugelipsum[1]smallpar}, that gives a different result with TL2017 than with the up-to-date version.

    – egreg
    Jan 25 at 8:57






  • 1





    @Max no flushright should never be used in that form. It is not an error as begin{foo} end{foo} is begingroupfoo...endfooendgroup so flushright needs to be defined to implement begin{flushright} but you could replace every use of flushright in the above by begin{flushright}end{flushright} or in fact replace them all by a blank line, and see the same output. the only part of the flushright definition that you are using is par

    – David Carlisle
    Jan 25 at 9:06






  • 1





    the scope affects the settings of tex variables and registers etc, but the outputs are global structures. If you go aaa {itshape bbb} ccc then the italic font is set in the local scope and just affects bbb but only the font setting is discarded at the } the current horizontal list that is being built is a global structure and has roman aaa, italic bbb and roman ccc, then eventually the par primitive will be executed and this global horizontal list will be broken into lines with whatever settings are in place at that point. @Max

    – David Carlisle
    Jan 25 at 10:12








  • 1





    I imagine that if LaTeX were being written today, it would cause an error to use the backslashed-form of environment names. (E.g. in place of begin{document} … end{document}, writing document, or similarly writing itemize, enumerate, etc.) Or maybe they'd even have different names, with some combination of @s and underscores and colons so that no one can type them accidentally. :-)

    – ShreevatsaR
    Jan 25 at 18:11






  • 1





    @Max yes and no you can, because of that general rule use begin{raggedright}...end{raggedright} without it raising a tex error but the behaviour, while well defined, is a bit odd because of the end-of-paragraph issues you raise here, so latex provides an environment form that inserts par and vertical a space so environments center, flushleft and flushright to match centering, raggedright and raggedleft

    – David Carlisle
    Jan 26 at 8:53











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f471793%2fscope-of-latex-code%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









13














tex has local and global scope as determined by groups ({...} and begin...end in your examples (begin...end forming a group as they are macros that expand to use of the tex primitive begingroup and endgroup group constructs.)



Commands can be defined to have local or global action but the ones you show are local, a global assignment is not restored when the group ends.



However your confusing output is caused by user error, flushright is not intended to be used as a command (the command form is raggedleft) it is the implementation of the start of the begin{flushright} end{flushright} environment.



TeX's linebreaking is optimised over a paragraph, using the settings at the end of the paragraph.



The important thing here is that (unlike raggedleft) flushright executes par so ends the previous paragraph so:



In the first case the paragraph is finished, and set with normal settings then locally huge font and ragged setting is set up but discarded at } before being used.



In the second case Huge fonts and baseline is set up, so the paragraph is set with a huge baseline when the par in flushright is executed.



In the third case, the paragraph is set with the normal settings by the implicit par at the blank line, and so the local settings in the following group are not used at all.






share|improve this answer


























  • It is perhaps worth noting that with a previous version of lipsum the same behavior would not show; it does now because lipsum issues par at the beginning rather than at the end. This can be exemplified by {Hugelipsum[1]smallpar}, that gives a different result with TL2017 than with the up-to-date version.

    – egreg
    Jan 25 at 8:57






  • 1





    @Max no flushright should never be used in that form. It is not an error as begin{foo} end{foo} is begingroupfoo...endfooendgroup so flushright needs to be defined to implement begin{flushright} but you could replace every use of flushright in the above by begin{flushright}end{flushright} or in fact replace them all by a blank line, and see the same output. the only part of the flushright definition that you are using is par

    – David Carlisle
    Jan 25 at 9:06






  • 1





    the scope affects the settings of tex variables and registers etc, but the outputs are global structures. If you go aaa {itshape bbb} ccc then the italic font is set in the local scope and just affects bbb but only the font setting is discarded at the } the current horizontal list that is being built is a global structure and has roman aaa, italic bbb and roman ccc, then eventually the par primitive will be executed and this global horizontal list will be broken into lines with whatever settings are in place at that point. @Max

    – David Carlisle
    Jan 25 at 10:12








  • 1





    I imagine that if LaTeX were being written today, it would cause an error to use the backslashed-form of environment names. (E.g. in place of begin{document} … end{document}, writing document, or similarly writing itemize, enumerate, etc.) Or maybe they'd even have different names, with some combination of @s and underscores and colons so that no one can type them accidentally. :-)

    – ShreevatsaR
    Jan 25 at 18:11






  • 1





    @Max yes and no you can, because of that general rule use begin{raggedright}...end{raggedright} without it raising a tex error but the behaviour, while well defined, is a bit odd because of the end-of-paragraph issues you raise here, so latex provides an environment form that inserts par and vertical a space so environments center, flushleft and flushright to match centering, raggedright and raggedleft

    – David Carlisle
    Jan 26 at 8:53
















13














tex has local and global scope as determined by groups ({...} and begin...end in your examples (begin...end forming a group as they are macros that expand to use of the tex primitive begingroup and endgroup group constructs.)



Commands can be defined to have local or global action but the ones you show are local, a global assignment is not restored when the group ends.



However your confusing output is caused by user error, flushright is not intended to be used as a command (the command form is raggedleft) it is the implementation of the start of the begin{flushright} end{flushright} environment.



TeX's linebreaking is optimised over a paragraph, using the settings at the end of the paragraph.



The important thing here is that (unlike raggedleft) flushright executes par so ends the previous paragraph so:



In the first case the paragraph is finished, and set with normal settings then locally huge font and ragged setting is set up but discarded at } before being used.



In the second case Huge fonts and baseline is set up, so the paragraph is set with a huge baseline when the par in flushright is executed.



In the third case, the paragraph is set with the normal settings by the implicit par at the blank line, and so the local settings in the following group are not used at all.






share|improve this answer


























  • It is perhaps worth noting that with a previous version of lipsum the same behavior would not show; it does now because lipsum issues par at the beginning rather than at the end. This can be exemplified by {Hugelipsum[1]smallpar}, that gives a different result with TL2017 than with the up-to-date version.

    – egreg
    Jan 25 at 8:57






  • 1





    @Max no flushright should never be used in that form. It is not an error as begin{foo} end{foo} is begingroupfoo...endfooendgroup so flushright needs to be defined to implement begin{flushright} but you could replace every use of flushright in the above by begin{flushright}end{flushright} or in fact replace them all by a blank line, and see the same output. the only part of the flushright definition that you are using is par

    – David Carlisle
    Jan 25 at 9:06






  • 1





    the scope affects the settings of tex variables and registers etc, but the outputs are global structures. If you go aaa {itshape bbb} ccc then the italic font is set in the local scope and just affects bbb but only the font setting is discarded at the } the current horizontal list that is being built is a global structure and has roman aaa, italic bbb and roman ccc, then eventually the par primitive will be executed and this global horizontal list will be broken into lines with whatever settings are in place at that point. @Max

    – David Carlisle
    Jan 25 at 10:12








  • 1





    I imagine that if LaTeX were being written today, it would cause an error to use the backslashed-form of environment names. (E.g. in place of begin{document} … end{document}, writing document, or similarly writing itemize, enumerate, etc.) Or maybe they'd even have different names, with some combination of @s and underscores and colons so that no one can type them accidentally. :-)

    – ShreevatsaR
    Jan 25 at 18:11






  • 1





    @Max yes and no you can, because of that general rule use begin{raggedright}...end{raggedright} without it raising a tex error but the behaviour, while well defined, is a bit odd because of the end-of-paragraph issues you raise here, so latex provides an environment form that inserts par and vertical a space so environments center, flushleft and flushright to match centering, raggedright and raggedleft

    – David Carlisle
    Jan 26 at 8:53














13












13








13







tex has local and global scope as determined by groups ({...} and begin...end in your examples (begin...end forming a group as they are macros that expand to use of the tex primitive begingroup and endgroup group constructs.)



Commands can be defined to have local or global action but the ones you show are local, a global assignment is not restored when the group ends.



However your confusing output is caused by user error, flushright is not intended to be used as a command (the command form is raggedleft) it is the implementation of the start of the begin{flushright} end{flushright} environment.



TeX's linebreaking is optimised over a paragraph, using the settings at the end of the paragraph.



The important thing here is that (unlike raggedleft) flushright executes par so ends the previous paragraph so:



In the first case the paragraph is finished, and set with normal settings then locally huge font and ragged setting is set up but discarded at } before being used.



In the second case Huge fonts and baseline is set up, so the paragraph is set with a huge baseline when the par in flushright is executed.



In the third case, the paragraph is set with the normal settings by the implicit par at the blank line, and so the local settings in the following group are not used at all.






share|improve this answer















tex has local and global scope as determined by groups ({...} and begin...end in your examples (begin...end forming a group as they are macros that expand to use of the tex primitive begingroup and endgroup group constructs.)



Commands can be defined to have local or global action but the ones you show are local, a global assignment is not restored when the group ends.



However your confusing output is caused by user error, flushright is not intended to be used as a command (the command form is raggedleft) it is the implementation of the start of the begin{flushright} end{flushright} environment.



TeX's linebreaking is optimised over a paragraph, using the settings at the end of the paragraph.



The important thing here is that (unlike raggedleft) flushright executes par so ends the previous paragraph so:



In the first case the paragraph is finished, and set with normal settings then locally huge font and ragged setting is set up but discarded at } before being used.



In the second case Huge fonts and baseline is set up, so the paragraph is set with a huge baseline when the par in flushright is executed.



In the third case, the paragraph is set with the normal settings by the implicit par at the blank line, and so the local settings in the following group are not used at all.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 25 at 8:30

























answered Jan 25 at 8:24









David CarlisleDavid Carlisle

495k4111381886




495k4111381886













  • It is perhaps worth noting that with a previous version of lipsum the same behavior would not show; it does now because lipsum issues par at the beginning rather than at the end. This can be exemplified by {Hugelipsum[1]smallpar}, that gives a different result with TL2017 than with the up-to-date version.

    – egreg
    Jan 25 at 8:57






  • 1





    @Max no flushright should never be used in that form. It is not an error as begin{foo} end{foo} is begingroupfoo...endfooendgroup so flushright needs to be defined to implement begin{flushright} but you could replace every use of flushright in the above by begin{flushright}end{flushright} or in fact replace them all by a blank line, and see the same output. the only part of the flushright definition that you are using is par

    – David Carlisle
    Jan 25 at 9:06






  • 1





    the scope affects the settings of tex variables and registers etc, but the outputs are global structures. If you go aaa {itshape bbb} ccc then the italic font is set in the local scope and just affects bbb but only the font setting is discarded at the } the current horizontal list that is being built is a global structure and has roman aaa, italic bbb and roman ccc, then eventually the par primitive will be executed and this global horizontal list will be broken into lines with whatever settings are in place at that point. @Max

    – David Carlisle
    Jan 25 at 10:12








  • 1





    I imagine that if LaTeX were being written today, it would cause an error to use the backslashed-form of environment names. (E.g. in place of begin{document} … end{document}, writing document, or similarly writing itemize, enumerate, etc.) Or maybe they'd even have different names, with some combination of @s and underscores and colons so that no one can type them accidentally. :-)

    – ShreevatsaR
    Jan 25 at 18:11






  • 1





    @Max yes and no you can, because of that general rule use begin{raggedright}...end{raggedright} without it raising a tex error but the behaviour, while well defined, is a bit odd because of the end-of-paragraph issues you raise here, so latex provides an environment form that inserts par and vertical a space so environments center, flushleft and flushright to match centering, raggedright and raggedleft

    – David Carlisle
    Jan 26 at 8:53



















  • It is perhaps worth noting that with a previous version of lipsum the same behavior would not show; it does now because lipsum issues par at the beginning rather than at the end. This can be exemplified by {Hugelipsum[1]smallpar}, that gives a different result with TL2017 than with the up-to-date version.

    – egreg
    Jan 25 at 8:57






  • 1





    @Max no flushright should never be used in that form. It is not an error as begin{foo} end{foo} is begingroupfoo...endfooendgroup so flushright needs to be defined to implement begin{flushright} but you could replace every use of flushright in the above by begin{flushright}end{flushright} or in fact replace them all by a blank line, and see the same output. the only part of the flushright definition that you are using is par

    – David Carlisle
    Jan 25 at 9:06






  • 1





    the scope affects the settings of tex variables and registers etc, but the outputs are global structures. If you go aaa {itshape bbb} ccc then the italic font is set in the local scope and just affects bbb but only the font setting is discarded at the } the current horizontal list that is being built is a global structure and has roman aaa, italic bbb and roman ccc, then eventually the par primitive will be executed and this global horizontal list will be broken into lines with whatever settings are in place at that point. @Max

    – David Carlisle
    Jan 25 at 10:12








  • 1





    I imagine that if LaTeX were being written today, it would cause an error to use the backslashed-form of environment names. (E.g. in place of begin{document} … end{document}, writing document, or similarly writing itemize, enumerate, etc.) Or maybe they'd even have different names, with some combination of @s and underscores and colons so that no one can type them accidentally. :-)

    – ShreevatsaR
    Jan 25 at 18:11






  • 1





    @Max yes and no you can, because of that general rule use begin{raggedright}...end{raggedright} without it raising a tex error but the behaviour, while well defined, is a bit odd because of the end-of-paragraph issues you raise here, so latex provides an environment form that inserts par and vertical a space so environments center, flushleft and flushright to match centering, raggedright and raggedleft

    – David Carlisle
    Jan 26 at 8:53

















It is perhaps worth noting that with a previous version of lipsum the same behavior would not show; it does now because lipsum issues par at the beginning rather than at the end. This can be exemplified by {Hugelipsum[1]smallpar}, that gives a different result with TL2017 than with the up-to-date version.

– egreg
Jan 25 at 8:57





It is perhaps worth noting that with a previous version of lipsum the same behavior would not show; it does now because lipsum issues par at the beginning rather than at the end. This can be exemplified by {Hugelipsum[1]smallpar}, that gives a different result with TL2017 than with the up-to-date version.

– egreg
Jan 25 at 8:57




1




1





@Max no flushright should never be used in that form. It is not an error as begin{foo} end{foo} is begingroupfoo...endfooendgroup so flushright needs to be defined to implement begin{flushright} but you could replace every use of flushright in the above by begin{flushright}end{flushright} or in fact replace them all by a blank line, and see the same output. the only part of the flushright definition that you are using is par

– David Carlisle
Jan 25 at 9:06





@Max no flushright should never be used in that form. It is not an error as begin{foo} end{foo} is begingroupfoo...endfooendgroup so flushright needs to be defined to implement begin{flushright} but you could replace every use of flushright in the above by begin{flushright}end{flushright} or in fact replace them all by a blank line, and see the same output. the only part of the flushright definition that you are using is par

– David Carlisle
Jan 25 at 9:06




1




1





the scope affects the settings of tex variables and registers etc, but the outputs are global structures. If you go aaa {itshape bbb} ccc then the italic font is set in the local scope and just affects bbb but only the font setting is discarded at the } the current horizontal list that is being built is a global structure and has roman aaa, italic bbb and roman ccc, then eventually the par primitive will be executed and this global horizontal list will be broken into lines with whatever settings are in place at that point. @Max

– David Carlisle
Jan 25 at 10:12







the scope affects the settings of tex variables and registers etc, but the outputs are global structures. If you go aaa {itshape bbb} ccc then the italic font is set in the local scope and just affects bbb but only the font setting is discarded at the } the current horizontal list that is being built is a global structure and has roman aaa, italic bbb and roman ccc, then eventually the par primitive will be executed and this global horizontal list will be broken into lines with whatever settings are in place at that point. @Max

– David Carlisle
Jan 25 at 10:12






1




1





I imagine that if LaTeX were being written today, it would cause an error to use the backslashed-form of environment names. (E.g. in place of begin{document} … end{document}, writing document, or similarly writing itemize, enumerate, etc.) Or maybe they'd even have different names, with some combination of @s and underscores and colons so that no one can type them accidentally. :-)

– ShreevatsaR
Jan 25 at 18:11





I imagine that if LaTeX were being written today, it would cause an error to use the backslashed-form of environment names. (E.g. in place of begin{document} … end{document}, writing document, or similarly writing itemize, enumerate, etc.) Or maybe they'd even have different names, with some combination of @s and underscores and colons so that no one can type them accidentally. :-)

– ShreevatsaR
Jan 25 at 18:11




1




1





@Max yes and no you can, because of that general rule use begin{raggedright}...end{raggedright} without it raising a tex error but the behaviour, while well defined, is a bit odd because of the end-of-paragraph issues you raise here, so latex provides an environment form that inserts par and vertical a space so environments center, flushleft and flushright to match centering, raggedright and raggedleft

– David Carlisle
Jan 26 at 8:53





@Max yes and no you can, because of that general rule use begin{raggedright}...end{raggedright} without it raising a tex error but the behaviour, while well defined, is a bit odd because of the end-of-paragraph issues you raise here, so latex provides an environment form that inserts par and vertical a space so environments center, flushleft and flushright to match centering, raggedright and raggedleft

– David Carlisle
Jan 26 at 8:53


















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f471793%2fscope-of-latex-code%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

MongoDB - Not Authorized To Execute Command

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

Npm cannot find a required file even through it is in the searched directory