Last updated
Was this helpful?
Last updated
Was this helpful?
Tokens Studio supports math equations to calculate the Value of a Token.
This is a popular approach to a create scales for typography, spacing, size, or dynamically calculate the radius of a focus ring (pictured above).
Combining math equations with referenced to another Token can create a flexible and powerful design system.
All Token Types that accept numeric values can use math equations to calculate their value in Tokens Studio.
When you apply a Token with a math equation as its value, the Plugin applies the resolved value to the corresponding property in Figma.
Select a card to jump to its technical docs.
This means when you export a Token with math in its value as a Variable in Figma:
You won't see the references or equations.
The plugin sends the resolved value of the equation to Figma as a Number Variable.
Token Values written as equations can include hard-coded number values and references to other Tokens.
For example, if you want spacing.lg
to be twice as large as spacing.md
you can write an equation to calculate its value:
Or, you can replace the multipler with a reference to another Token named scale.multiplier
The syntax for writing a math equation varies based on the kind of equation you are writing.
Simple math equations can be written with or without brackets.
For example, 8 * 8
and (8 * 8)
will both resolve to 64
Complex formulas require spaces between operators to ensure the Tokens can be transformed correctly using Style Dictionary.
For example: 8 * 8
will transform as expected 8*8
will not.
Working with math in the plugin can be a powerful workflow enhancement, but there are some limitations to be aware of.
The plugin supports Tokens with more than one value. When these Tokens are referenced in a math equation, the plugin can not resolve the equation because it doesn't know how to interpret the value.
Referencing Tokens in our math equations that have values that contain units can lead to unexpected results.
This is because the math package we use can resolve equations with units, as long as the units aren't mixed.
For example:
A Token named size-2
with a value of 1.5rem
A Token named density-default
with a value of 2px
When both Tokens are included in a math equation written as:
The equation can't be resolved because the Tokens referenced contain a mix of rem
and pixel
units. If both Tokens have the same units, or are unitless, the equation would resolve as expected.
Workarounds
One method is to create your math equations using the Other or Number Token Types which don't require a unit, and add them in later.
For example, you could create a sizing scale using then add the required unit when it is referenced in a Typography Token.
lineHeights = {lineHeights-calc-body-default}%
fontSize = {scale-rem-md}rem
Here are some math equations used by our team.
When using multipliers that aren't a whole number, you can use a rounding function to calculate values that don't include decimals, because a spacing value of 12.11px
might not be ideal.
To round to the nearest whole number, you can put our equation inside the roundTo
function.
For example, if sizing.sm
has a value of 2
2 * 1.33 = 2.66
Will calculate a resolved value of 3
In Figma, the only option for a responsive line-height value is in percentage, but your engineers might prefer the Token defined as a unitless number.
For example, the Number Token called lineHeights-unitless.heading.relaxed
with a value of 1.5
would be written in a Line Height Token as:
Which calculates a resolved value of 150%
This equation requires having a Token which defines the value of 1 rem, and adjusting the Tokens Studio plugin settings to reference this Token as the value for "base font size".
→ Jump to the guide on Base Font Size Settings
In this example, there is a
Number Token called unitless-Token
with a value of 48
Number Token called rem-base
with a value of 16
A new Dimension Token is created with the value as an equation:
Which calculates a resolved value of 3rem
At the time of writing this doc, Math as a Token value is not mentioned in the most recent Design Tokens Community Group W3C specifications for Design Tokens.
Tokens Studio has included this feature in the plugin to help design system maintainers work more efficiently.
When transforming Tokens with Math in their Values, there are specific configurations to be aware of.
Token Values entered with math equations need to be checked and resolved.
→ SD-Transforms Read-Me Doc, ts/resolveMath
Token Values entered as a number without a unit converted to a number with pixels as a unit.
→ SD-Transforms Read-Me Doc, ts/size/px
Tokens Studio uses the JavaScript Expression Evaluator for the implementation of math in token values.
Mentioned in this doc:
Sam I am Designs has a written guide and Figma community file featuring many of the math equations mentioned above.
Tokens Studio Plugin GitHub - Open issues for Token Values Math
None yet
Math in token value enhancements - Feature Request
For example, a with 2,4
as its value represents spacing on top/bottom and right/left.
In this case, you can create a unitless Number Token with the preferred value, and write an equation in the to convert it to a percentage so it works well with Figma.
💡 Something to share?
💡 Something to share?
💌 Visit to contribute or subscribe to updates.
Engineers typically transform Tokens used in code with , which is tool-agnostic. Tokens coming from Tokens Studio require an additional step: , an npm package that prepares Tokens for Style Dictionary.
🐞 If you are experiencing an issue not listed here, please reach out to us on the Troubleshooting channel of our , , or send us an email support@tokens.studio