Relative quantities
In cooking, and especially in baking, precision is essential. Relying on fixed quantities can lead to unbalanced outcomes when raw produce yields vary (for example, when a single lemon yields more juice than expected).
Gram solves this by allowing you to define an @ingredient’s quantity dynamically as a percentage of another @ingredient or an intermediate &variable. This ensures that your recipe remains balanced regardless of real-life ingredient variations.
Syntax
Section titled “Syntax”You can target either a base ingredient or an intermediate variable:
Prefix the target ingredient’s name with @&:
Pattern: @name{ percentage% @&TargetIngredient }
Reference the target intermediate variable with &:
Pattern: @name{ percentage% &TargetVariable }
Resolution rules
Section titled “Resolution rules”When the compiler resolves a relative quantity, it follows four strict scoping and calculation principles:
| Rule | Behavior | Description |
|---|---|---|
| Section scoping | Current section only | You cannot directly target a base @ingredient declared in a different section. To bridge sections, capture the result into a globally scoped &variable. |
| Accumulation | Sums all occurrences | If the target @ingredient appears multiple times within the same section, the percentage applies to their cumulative total. |
| Mass-based | Evaluated in grams | Calculations are always performed against physical mass in grams. For example, 50% of “2 eggs” (each standardized at 50g) produces 50g, not 1 egg. |
| Alternatives | One option at a time | Each option in an alternative group computes independently (@butter{10% @&flour}|@oil{8% @&flour} gives 20 g of butter or 16 g of oil for 200 g of flour). An option may also target an earlier option in the same group (@butter{100g}|@oil{80% @&butter}). |
Ratios with units
Section titled “Ratios with units”While a percentage compares mass to mass, a ratio with units relates two quantities in any compatible units—just as recipes state them: 1 egg per 100 g of flour, 15 g of salt per liter of water, or 15 minutes per 500 g of roast.
Pattern: @name{ amount / targetAmount @&TargetIngredient } (or &TargetVariable).
amounttakes its unit (15 g) or none for counts (1egg), and may be a range (15-20 g).targetAmountis a single number and unit (100 g), a unit alone (L), or omitted for one piece ({1 / @&cutlet}).- Always put a space around
/(at least on one side) and write the unit alone before the target:{1/100g @&flour}parses as the fraction1/100, and{1 / 100 g of @&flour}treatsg ofas the unit.
Unit compatibility
Section titled “Unit compatibility”Gram only computes a ratio when its units can be compared safely—it never guesses:
| Target amount | Target written in | Result |
|---|---|---|
Weight (100 g) | Weight (1 kg) | Computed directly |
| Weight | Volume or count | Computed using density or unit_weight from the ingredient database |
Count (2 cloves, or none) | Same count (3 clove, 4) | Computed directly (singular/plural matched) |
| Count | Different count (2 slices for cloves) | Refused (RELATIVE_QUANTITY_UNKNOWN_MASS) |
If a ratio cannot be resolved, it remains as written in views and shopping lists and raises RELATIVE_QUANTITY_UNKNOWN_MASS. Unlike percentages, a resolved ratio keeps the unit of its own amount (e.g. 5 eggs, 15 g of salt) and merges normally in the shopping list.
Scope, intermediates & scaling
Section titled “Scope, intermediates & scaling”- Same scope as percentages: target an
@&ingredientdeclared earlier in the section (repeated uses sum up), an earlier option in an alternative group, or an&intermediate. - Whole intermediates: if a section consumes an intermediate without a quantity (
Roll out the &dough), ratios and percentages (@salt{2 g / 100 g &dough},@sugar{5% &dough}) resolve against the total mass of the section that produces it—provided only that section consumes it. An intermediate made by a step (Mix … ->&dough) has no mass of its own: write the amount used (&dough{800 g}), or make it in its own section. - References & composites: ratios and percentages work on homemade intermediate references (
&stock{250 ml / 100 g @&rice}), but are not yet supported on composite parents (@eggs{1}<@box{1 / 100 g @&flour}>). - Scaling: scaling a recipe recalculates ratios from the scaled targets (unless the target is fixed with
@=). Marking a ratio itself with=is not allowed.
Mass calculation rules
Section titled “Mass calculation rules”Because relative percentages compute ratios of physical mass, the analyzer (@gram-lang/analyzer) standardizes target quantities before applying the percentage:
| Target unit type | Classification | Pre-calculation logic | Example |
|---|---|---|---|
| Explicit mass | Direct | Direct computation without conversion. | 50% of 500g = 250g |
| Volume | Density | Converts volume to grams using physical.density from the ingredient database. | 500ml water ➔ 500g.50% = 250g |
| Count | Unit weight | Multiplies count by average physical.unit_weight from the ingredient database. | 2 eggs (50g ea) ➔ 100g.50% = 50g |
| Unknown unit | Unresolved | Cannot infer mass. Emits a RELATIVE_QUANTITY_UNKNOWN_MASS warning. | Preserves formula as fallback. |
Shopping list behavior
Section titled “Shopping list behavior”Because relative quantities are mathematical expressions (e.g. 125% of lemon juice), the compiler decides how to represent them on the generated shopping list:
When the physical mass of the target is known, the formula is evaluated completely. The resulting weight is merged seamlessly with other purchases of the same ingredient:
If mass standardization is disabled or the target item cannot be converted (e.g. missing from ingredients.yaml), Gram outputs a hybrid representation combining any fixed amounts with the unresolved formula:
This guarantees no shopping requirements are lost, even when unit data is incomplete.
Error handling
Section titled “Error handling”The compiler flags logical inconsistencies in relative definitions:
RELATIVE_QUANTITY_UNRESOLVED: Triggered when the target ingredient or variable has not been declared earlier in the section, displaying(20% of missing ❓).CIRCULAR_REFERENCE: Triggered if an ingredient attempts to compute a percentage of itself (e.g.@flour{10% @&flour}), displaying(10% of self) ⚠️.RELATIVE_QUANTITY_UNKNOWN_MASS: Emitted when the target item’s physical weight cannot be determined from unit conversions or the database.