MACRO

\label{sec:macro}

The MACRO construct allows the execution of a group of statements via a single command. Optionally the MACRO construct takes arguments.

\madbox

label: MACRO = { statements; };

label(arg1, …,argn): MACRO = { statements; };

The first form allows the execution of the defined group of statements via a single command, \madxmpEXEC, label; that executes the statements defined between curly brackets exactly once. The EXEC command can then be issued any number of times.

The second form allows to replace strings anywhere inside the statements in curly brackets by other strings, or integer numbers prior to execution. This is a powerful construct and should be handled with care.

Simple example: \madxmp simple(xx,yy): MACRO = { xx = yy*yy + xx; VALUE, xx;};
a = 3;
b = 5;

EXEC, simple(a,b);

Passing arguments
In the following example we use the fact that a ”$” in front of an argument means that the truncated integer value of this argument is used for replacement, rather than the argument string itself. \madxmp tricky(xx,yy,zz): MACRO = {mzz.yy: xx, l = 1.yy, kzz = k.yy;};
n=0;
WHILE (̄n ¡ 3) {
n = n+1;
EXEC, tricky(quadrupole, $n, 1);
EXEC, tricky(sextupole, $n, 2);
};

Whereas the actual use of the preceding example is NOT recommended, a real life example, showing the full power (!) of macros is to be found under macro usage for the usage, and under macro definition for the definition.

Because MACRO statements are a \madxconstruct and not part of a full language, \madxallows only one level of inclusion of another IF ... ELSEIF ... ELSE, WHILE or MACRO statements.

Macros cannot be called with number arguments but always with string arguments. In case numerical values should be passed to a MACRO in an EXEC statement, one can conveniently use variables names: \madxmp n1=99; n2=219;
EXEC, thismacro($n1, $n2); instead of \madxmp EXEC, thismacro($99, $129); ! fails…

General Control Statements

\madxconsists

of a core program, and modules for specific tasks such as twiss parameter calculation, matching, thin lens tracking, and so on.

The statements listed here are those executed by the program core. They deal with the I/O, element and sequence declaration, sequence manipulation, statement flow control (e.g. IF, WHILE), MACRO declaration, saving sequences onto files in \madxor \madeightformat, and so on.