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

Max
  • 305
  • 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 '19 at 08:41

1 Answers1

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.

David Carlisle
  • 757,742
  • 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 '19 at 08:57
  • @David: Thanks. Do you mean \flushright is correct in syntax but should not be used because of its "incorrect" semantics? But I don't think there is right or wrong semantics of the code itself. It's up to the user of the code. I struggle to understand while we can use \flushright without LaTeX complaining it's not intended to be used. – Max Jan 25 '19 at 09:01
  • 1
    @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 '19 at 09:06
  • @David Thanks, so \lushright is a red herring. After some more trial I got what you mean and started to understand LaTeX's behavior. A simpler version of the test code then would be {\lipsum[5]\Huge\par}. The spacing in the paragraph before \par is determined by \Huge. What's even more non-intuitive though, is that the font size of the "current" paragraph is not determined by \Huge! – Max Jan 25 '19 at 09:54
  • @Max the text is set using the locally specified fonts onto a horizontal list: you would expect aaa \textbf{bbb} \textit{ccc} to use roman italic and bold fonts, not ignore that and use the font at the end of a paragraph! but then this long horizontal list is broken into lines as a single operation at the end of the paragraph so the linespacing that matters is just the one at the end. – David Carlisle Jan 25 '19 at 10:04
  • Now it seems to me { } constrains the scope of the code inside. \begingroup and \endgroup have the same "scoping" effect. The behavior of in-paragraph spacing determined by setting before \par but AFTER the paragraph is what's different from other languages. But this is somehow irrelevant to scope. – Max Jan 25 '19 at 10:08
  • 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 '19 at 10:12
  • something can not be "after the paragrah" but before \par as it is only \par (or end of vbox) that ends paragraphs. – David Carlisle Jan 25 '19 at 10:14
  • 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 '19 at 18:11
  • Back to the red herring of \flushright (it's a declaration in LaTeX form), I just read this in LaTeX - User's Guide and Reference Manual - "Every declaration has a corresponding environment of the same name (without \)". Seems the inverse is not necessarily (or always) true. – Max Jan 26 '19 at 01:18
  • 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 '19 at 08:53