{grouping} is about the grouping mechanism in TeX, and about local and global assignments.

learn more… | top users | synonyms (2)

60
votes
2answers
9k views

When should one use \begingroup instead of \bgroup?

Is there ever a case when the use of \begingroup is preferred to \bgroup? Obviously, \bgroup is necessary in cases where you need to have balanced braces and thus cannot use {, but when is it ...
25
votes
3answers
7k views

When to use @ in an \if statement

I am new to LaTeX and so this question might come across as rather basic. It might also reflect my biases/assumptions from my C/C++ programming days. I was seeing the code of the 'exam' class and ...
24
votes
4answers
922 views

Kerning between bold and non-bold text

I'd like to write a word in bold followed by a comma and a non-bold word, like this: \textbf{why}, blah If I just write why, TeX inserts a kern to shift the comma a little closer to the "y". But ...
21
votes
1answer
586 views

] inside an optional argument

I accidentally discovered (here) that a closing square bracket within an optional argument delimited by [ ] can cause problems. Here is example code illustrating the issue: \documentclass{article} ...
19
votes
2answers
245 views

global confusion

Simply put, when should I care if something is global or not? What does this really mean? If my package, for example, provides \newcommand*{\setfooter}[1]{\def\mypackage@footer{#1}} And I later ...
18
votes
4answers
499 views

How do I get a value out of a group?

I've just recovered from my first real foray into LaTeX3 programming. It was ... an experience. Not a completely unpleasant one and once I'd gotten used to the syntax then I found it a lot easier ...
18
votes
3answers
624 views

Should LaTeX counters always be modified globally?

In this answer, egreg states that "LaTeX counters should always be modified globally". However, the etoolbox package provides the \defcounter macro which will assign a value to a previously ...
18
votes
2answers
2k views

What does \begingroup\expandafter…\endgroup do?

I often see code of the following form in package implementations (this example is from the LaTeX3 sources): \begingroup\expandafter\expandafter\expandafter\endgroup \expandafter\ifx\csname ...
18
votes
3answers
343 views

What are the effects of an empty group, {}?

In order to have a syntactic space after a no-argument command, one is taught to delimit the command token with an empty group ({}), like this: \somecommand{} more LaTeX material. However an empty ...
17
votes
2answers
347 views

Why do I have to put the center environment in a group for \@afterindentfalse\@afterheading to suppress indentation?

Consider the following two macros, the only difference being that one has the environment inside a group: \documentclass{article} \makeatletter \newcommand\thisworks{% {\begin{center} $\ast$ ...
17
votes
1answer
502 views

Why is \global\let not always global?

As an LaTeX environment opens a local group, it is normally necessary to use a global definition if changes in the environment should be available after the environment: \documentclass{minimal} ...
15
votes
1answer
185 views

First occurrence of a command

I'm writing some macros that need to behave differently after first occurrence. I decided to use the etoolbox package's toggle. My MWE is as follows: \documentclass{article} \usepackage{etoolbox} ...
14
votes
3answers
327 views

What is the difference between local and global in a TeX meaning?

In tikz declare function and babel french option it came to the question about the difference between local and global in a TeX meaning. In the liked question the babel shorthand ; should be ...
14
votes
1answer
366 views

Is there a way to have an “empty” environment to merely limit scope of declarations?

I am fairly new to LaTeX, so I apologize if this is a beginner question, but I have not found a good way to query for it otherwise. I have observed that when I use an environment like ...
13
votes
3answers
1k views

\global\renewcommand equivalent of \global\def

The following MWE works just fine, but I had to resort to using \global\def (not that there's anything wrong with it). The intent of the MWE is to provide access to the value of macro as set in an ...
13
votes
3answers
744 views

Use \bgroup and \egroup to set some pre and post code to a macro

Why isn’t it possible to use \bgroup and \egroup in this way? \documentclass{article} \def\precode{\fbox\bgroup} \def\postcode{\egroup} \newcommand{\mymacro}[1]{\precode#1\postcode} ...
13
votes
2answers
473 views

\expandafter within a \begingroup … \endgroup [duplicate]

Possible Duplicate: What does \begingroup\expandafter…\endgroup do? In many of Heiko Oberdiek's packages one sees this type of construction: ...
13
votes
1answer
121 views

Difference between “hbox group” and “adjusted hbox group”?

eTeX's \currentgrouptype distinguishes two types of \hbox group types \hbox{\showthe\currentgrouptype} % 3 "adjusted hbox group" \noindent \hbox{\showthe\currentgrouptype} % 2 "hbox group" Looking ...
13
votes
2answers
587 views

Is there a way to set a *global* key value using `pgfkeys`?

The simple answer to this question appears to be "No"; quoting from the pgf manual: The setting of a key is always local to the current TeX group. However, this is very annoying because it means ...
12
votes
2answers
10k views

Temporarily increase line spacing

How do I temporarily increase line spacing? I have some text on my title page: \begin{center} { \Large \textbf{The Great Thesis About Some Very Great Things Indeed With a Pretty Long Title That Will ...
12
votes
2answers
252 views

necessity of braces for accent commands

Many resources on LaTeX teach that "é" and "ä" are typeset with (respectively) \'{e} and \"{a}; and it is similar for other accent symbols. Are the braces really needed? Aren't \'e and \"a shorter but ...
12
votes
2answers
368 views

How to create *local* labels/refs in LaTeX?

Concise question: Is there any way to limit the scope in which a \label is visible to a \ref? Motivation: My current goal is to create a rather large document, containing many exercises and their ...
12
votes
2answers
299 views

Is there a way to limit the scope of \usepackage?

When I tried to use the breqn package suggested by an answer to the question How do I break a long equation over lines It managed to break a whole bunch of unrelated equations (equations that did ...
11
votes
2answers
351 views

Restrict macro to an environment

I am quite a noob when it comes to customising LaTeX/XeLaTeX. I would like to know if there's a way to limit the usage of a macro to a specific environment. Consider the following macro and ...
11
votes
1answer
247 views

Global scope or permanent length or savebox

I am trying to save a minipage width and height in one environment, and use these dimensions in a subsequent environment of mine, to make a picture environment. The idea is to create a picture on the ...
11
votes
2answers
182 views

What combinations of mode and \currentgrouptype exist?

I am trying to predict in any given situation what TeX does if a closing brace reaches its stomach (apart from reverting local assignments). For instance, \vbox{\tracingall H} shows that if it is in ...
11
votes
1answer
305 views

What is the scope of elements of TikZ pictures?

I understand that named elements of a tikzpicture cannot be referenced before they are defined; but I'm confused about how "long" a named element persists. It appears that once named, an element ...
10
votes
2answers
516 views

How can I change the text color in such a way that the effect transcends groups?

I'd like to have a command \globalred that makes all subsequent text red, even if the command is issued inside a group. For example, the output of the TeX file \documentclass{article} ...
10
votes
3answers
1k views

Drawing a proper shadow for overlapping objects

I draw images that are composed of several overlapping nodes. If I add a drop shadow to each node, the shadow of the second node is drawn over the first node, as seen in the picture below (which ...
10
votes
1answer
784 views

Why do \setcounter and \addtocounter commands have global effect, while \setlength and \addtolength commands obey the normal scoping rules?

The \setcounter and \addtocounter commands have global effect, while the \setlength and \addtolength commands obey the normal scoping rules. Why were they designed like this? Why are ...
10
votes
1answer
133 views

Emulate TeX's grouping

In TeX, various kinds of groups must match properly, for instance, the following is wrong: $ \begingroup $ \endgroup I would like macros which allow such mismatched grouping. They would keep ...
9
votes
2answers
556 views

Beamer: content in box register does not survive \end{frame}

Consider the following example (I edited the example to include LaTeX lrbox environment): \documentclass{beamer} \newsavebox\mybox \newsavebox\myboxb \begin{document} \frame{ \sbox\mybox{Hello ...
9
votes
3answers
383 views

Making register declarations local, Undefining a register (count, toks, etc.)

Is it possible to make \newcount, \newtoks, etc. respect grouping, so that, e.g., \newcount\temp\temp=2{\newcount\temp\temp=1}\showthe\temp gives 2 (instead of 0)? Relatedly, is it possible to ...
9
votes
2answers
767 views

siunitx: How do I unset options previously set with \sisetup?

In the siunitx package (v1.3), I know that I can set various options with the \sisetup command. But what if I want a particular option to apply to only a small section of my document, with default ...
9
votes
2answers
108 views

Error when using defs inside a macro to write a row in a table

For the following MWE \documentclass{article} \newcommand{\cmd}[5]{% \def\tA{#1}% \def\tB{#2}% \def\tC{#3}% \def\tD{#4}% \def\tE{#5}% \tA & \tB & \tC & \tD & \tE \\ } ...
8
votes
2answers
150 views

Defining TeX count in a group

I thought that TeX counts were local definitions, but that is not what I am seeing. I was expecting that if I used \newcount within a group, that the etoolbox \ifdefcounter would say that the counter ...
8
votes
2answers
595 views

\newtoks token seems to have local scope

I want to collect the first line of a quotation to use later. I have used \newtoks for a similar purpose so tried it here, this way: \documentclass[12pt] {article} \newtoks{\qfirstlineinternal} ...
8
votes
3answers
551 views

Using execute at begin node with \phantom and \pgfuseimage in TikZ?

Problem I would like to create several nodes whose text is a placeholder for an image, i.e., \phantom{\pgfuseimage{mypicture}}. This is easily done as, for example: \matrix { \node ...
8
votes
1answer
941 views

How to escape “]” in a description's item tag?

In a description like \begin{description} \item[\texttt{DoSomething(items[])}:] ... \end{description} , how do I escape the "]" character?
8
votes
1answer
141 views

Why does the `&` reset grouping?

In this answer, I used \tikzmark to mark the extent of a typeset piece of text, so I could draw changebars next to it. But it initially seemed that the changebars "disappear" with math-mode content. ...
8
votes
1answer
237 views

Creating temporary definitions for commands

In the context of writing something like homework solutions for a math class, I often face an awkward situation. Each problem will have some idiomatic functions that are cumbersome and error prone to ...
8
votes
1answer
59 views

Missing bullet when itemize \item content is empty and buried within a group

When writing my question about various approaches to creating loops, I stumbled across the following anomaly \documentclass{article} \usepackage{pgffor} \begin{document} \begin{itemize} \foreach \x ...
8
votes
1answer
226 views
+100

Restore kerning across empty groups

Context In my document, I attach a tooltip to the middle part of a word. To demonstrate this behavior, try mousing over just the letter V in the word AVA. Here is the implementation and usage of my ...
7
votes
2answers
169 views

why aren't the \left and \right before my parentheses changing their size?

I am working on a document, and in only one case have the \left and \right before my parentheses seemed to do anything. For example, I have this equation: \begin{equation} \mathbb{E}(RS_{nd}) = ...
7
votes
3answers
2k views

LaTeX3: how to define a variable locally

I am trying to learn the LaTeX3 syntax. One of the questions I have come up with is how to define a variable, i. e. a macro without arguments in TeX parlance, with local scope within a group. In TeX ...
7
votes
3answers
210 views

When to use or avoid grouping?

For writing my own TeX code, grouping (\begingroup, \endgroup) helps on automatically saving and restoring TeX "variables". (La)TeX "variables" are either macros or registers. The registers can ...
7
votes
2answers
156 views

Coordinate system along line

I would like to draw a coordinate system along some lines in a framework for a structural course. This little coordinate system is called local coordinate system. At the moment the framework looks ...
7
votes
4answers
806 views

Is there a way of limiting the scope of \newlength?

Is there a way of limiting the scope of \newlength? I'm using the same name in many TikZ picture but each with different value, and I don't want it to live on between the pictures as it's not intended ...
7
votes
2answers
709 views

Limit macro/newcommand scope to section

How can I limit the scope of a \newcommand to a \section? This is what I'd like to have: The command is defined only inside the \section, not globally; The command scope is that \section and cannot ...
7
votes
1answer
132 views

Grouping items onto the same line to avoid wrapping

I am authoring a poker-related document and am using the following commands to allow me to display cards: % Suit image adjustments: \newcommand{\suit}[1]{\includegraphics[width=8pt, height=8pt]{#1}} ...