This question is about several things at once, which makes answering it somewhat interesting. others have taken one tack, I'll take a different one.
First, bear in mind that TeX is a macro expansion language, not a functional language. Secondly, note that TeX has very few built-in variable types. A lot of 'variables' are therefore macros with appropriate structure.
The \newif
macro creates three new macros from the argument \if<name>
:
\if<name>
, a switch to be used in tests;
\<name>true
, which sets the switch logically true;
\<name>false
, which sets the switch logically false;
Typically, the \if<name>
here is something like \if@myswitch
or \ifmy@switch
. Other answers have mentioned that @
here is a 'letter', which is used to namespace TeX macros. Thus the @
has no special meaning to TeX: it's there for the programmer. (The usual pattern is to use @
to make the names easier to read, so \if@<package>@<meaning>
or \if<package>@<meaning>
are common.)
So analysising the question, the two lines \newif\ifcancelspace
and \newif\if@addpoints
create the macros
\ifcancelspace
\cancelspacetrue
\cancelspacefalse
\if@addpoints
\@addpointstrue
\@addpointsfalse
The two switches can now be used in a constructions
\if@addpoints % or \ifcancelspace
% Do stuff
\else
% Do other stuff
\fi
Taking point [4] next, the macro \@addpointsfalse
sets the \if@addpoints
switch to logically false. So it means my test above would 'do other stuff'. Setting \@addpointstrue
would mean that the test would 'do stuff'.
The last point to deal with is [3], for which you need to understand grouping in TeX, macro expansion and how the switches actually work. As TeX is a macro language, it does not have a concept of a variable being used 'within' a function. Thus groups are created by the constructs
{ ... }
or
\begingroup ... \endgroup
(A brace group is also used in places where TeX 'expects' grouping, and so it effectively disappears. That is the case, for example, with the group needed to use \def
.)
An assignment will be trapped within such as group unless it made globally. Now, the definition \def\addpoints{\global\@addpointstrue}
means that where we use \addpoints
, TeX will replace it with \global\@addpointstrue
(macro expansion). We'll come back to \global
in a bit, but first note that \@addpointsture
is a macro which expands to
\let\if@addpoints\iftrue
This is an assignment, and normally applies only within the current TeX group level. However, the \global
prefix means that the assignment ignores grouping. So the result is that \addpoints
will globally set the switch \if@addpoints
to logically true.
Yep, this can easily be done with \tikzmark
. With this you
- complete the
enumerate
list as you normally would
- mark specfic points with a
\tikzmark{<name>}
- access these
\tikzmark
s in a \tikzpicture
with the [overlay,remember picture]
options and draw as desired.
Note:
References:
Code:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing}
\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node[baseline] (#1) {};}
\tikzset{My Node Style/.style={midway, right, xshift=3.0ex, align=left, font=\small, draw=none, thin, text=black}}
\newcommand\VerticalBrace[4][]{%
% #1 = draw options
% #2 = top mark
% #2 = bottom mark
% #4 = label
\begin{tikzpicture}[overlay,remember picture]
\draw[decorate,decoration={brace, amplitude=1.5ex}, #1]
([yshift=1ex]#2.north east) -- ([yshift=-1ex]#3.south east)
node[My Node Style] {#4};
\end{tikzpicture}
}
\begin{document}
\begin{enumerate}
\item Item 1\tikzmark{top 1}
\item Item 2\tikzmark{bottom 1}
\item Item 3\tikzmark{top 2}
\item Item 4
\item Item 5
\item Item 6\tikzmark{bottom 2}
\end{enumerate}
\VerticalBrace[ultra thick, blue]{top 1}{bottom 1}{The first two items}
\VerticalBrace[ultra thick, blue]{top 2}{bottom 2}{The last four items}
\end{document}
Best Answer
Just to add a bit on the other answers and to try and answer the title of the question:
As David mentioned it mostly depends on your use case but here are some typical areas where using grouping is advisable:
When setting the
\@elt
in lists.When defining macros that affect paragraph(s) of text.
When you want to split a command into two parts, for example:
When changing catcodes:
In math commands to avoid `leaking out' of style changes
When to avoid grouping is mostly a matter of programming style. Good programming practice dictates that
globals
are an evil, however, is very hard to achieve this with TeX/LaTeX. The LaTeXsource2e
tends to use grouping for most of the important macros, even in cases such as:Can you guess why?