Macros Overview¶
Macros are built-in functions that handle common operations in question writing — math formatting, string manipulation, array processing, conditional logic, and feedback generation.
Macro Categories¶
| Category | Purpose | Key Functions |
|---|---|---|
| Math | Numeric computations, formatting | numfmt(), fracsimp(), gcd(), lcm() |
| String | Text processing and formatting | strtoupper(), trim(), str_replace() |
| Array | Array creation and manipulation | array(), sort(), array_sum() |
| Format | Display formatting | displaynum(), fracformat(), sciformat() |
| Conditional | Branching and logic | if/else, isneg(), isint() |
| Feedback | Adaptive hints and messages | wrongans(), showwork(), ifansunit() |
Using Macros¶
Macros are called in the question's setup code or inline within the answer evaluation block. They look like function calls:
$formatted = numfmt($ans, 2); // format $ans to 2 decimal places
$simplified = fracsimp($num, $den); // simplify a fraction
You can chain macro calls and use their return values in variable assignments, conditional checks, or directly in the question text.
Macros vs. PHP Functions¶
Many macros parallel standard PHP or math library functions, but are adapted for educational math contexts — for example, fracsimp() returns a properly reduced fraction, and displaynum() handles special cases like negative numbers, zero, and irrational values.
For standard string and array operations, PHP built-ins like strlen(), count(), sort(), and implode() also work directly in question code.
Quick Reference¶
See each category page for the full list of available macros with examples.