Skip to content

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.

You can target either a base ingredient or an intermediate variable:

Prefix the target ingredient’s name with @&:

[Add] @flour{100g} to the bowl.

[Mix] In @water{70% @&flour}. // 70g of water

Pattern: @name{ percentage% @&TargetIngredient }

When the compiler resolves a relative quantity, it follows four strict scoping and calculation principles:

RuleBehaviorDescription
Section scopingCurrent section onlyYou cannot directly target a base @ingredient declared in a different section. To bridge sections, capture the result into a globally scoped &variable.
AccumulationSums all occurrencesIf the target @ingredient appears multiple times within the same section, the percentage applies to their cumulative total.
Mass-basedEvaluated in gramsCalculations are always performed against physical mass in grams. For example, 50% of “2 eggs” (each standardized at 50g) produces 50g, not 1 egg.
AlternativesOne option at a timeEach 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}).
v1.5.0

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.

[Mix] @flour{500 g} with @eggs{1 / 100 g @&flour}. // 5 eggs

[Pour] @water{2 l} and @salt{15 g / 1 L @&water}. // 30 g of salt

[Crush] @garlic{3 cloves} with @parsley{1 tsp / 2 cloves @&garlic}. // 1.5 tsp

Pattern: @name{ amount / targetAmount @&TargetIngredient } (or &TargetVariable).

  • amount takes its unit (15 g) or none for counts (1 egg), and may be a range (15-20 g).
  • targetAmount is 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 fraction 1/100, and {1 / 100 g of @&flour} treats g of as the unit.

Gram only computes a ratio when its units can be compared safely—it never guesses:

Target amountTarget written inResult
Weight (100 g)Weight (1 kg)Computed directly
WeightVolume or countComputed using density or unit_weight from the ingredient database
Count (2 cloves, or none)Same count (3 clove, 4)Computed directly (singular/plural matched)
CountDifferent 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.

  • Same scope as percentages: target an @&ingredient declared 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.

Because relative percentages compute ratios of physical mass, the analyzer (@gram-lang/analyzer) standardizes target quantities before applying the percentage:

Target unit typeClassificationPre-calculation logicExample
Explicit massDirectDirect computation without conversion.50% of 500g = 250g
VolumeDensityConverts volume to grams using physical.density from the ingredient database.500ml water ➔ 500g.
50% = 250g
CountUnit weightMultiplies count by average physical.unit_weight from the ingredient database.2 eggs (50g ea) ➔ 100g.
50% = 50g
Unknown unitUnresolvedCannot infer mass. Emits a RELATIVE_QUANTITY_UNKNOWN_MASS warning.Preserves formula as fallback.

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:

- Sugar: 156g

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.