# Scope of LaTeX code

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

• 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

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.

• 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 {\Huge\lipsum[1]\small\par}, that gives a different result with TL2017 than with the up-to-date version. – egreg Jan 25 at 8:57
• @Max no \flushright should never be used in that form. It is not an error as \begin{foo} \end{foo} is \begingroup\foo...\endfoo\endgroup 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
• 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
• 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
• @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