

With
Details

- With allows you to define local constants.
- With replaces symbols in expr only when they do not occur as local variables inside scoping constructs.
- With[{x:=x0,…},expr] inserts the unevaluated form x0 into expr.
- You can use With[{vars},body/;cond] as the right‐hand side of a transformation rule with a condition attached. »
- With has attribute HoldAll.
- With constructs can be nested in any way, with inner variables being renamed if necessary.
- With[def1,def2,expr] is equivalent to With[def1,With[def2,expr]]. »
- With is a scoping construct that implements read‐only lexical variables.
Examples
open all close allBasic Examples (4)
Evaluate an expression with x locally set to 7:
With works even without evaluation:
Define the local variable y in terms of the earlier local variable x:
Scope (5)
Use With to insert values into held expressions:
Use := to insert the unevaluated form of an expression into the result:
The variable names can be the same:
Use a constant for a value that is needed more than once:
Set multiple variables with independent values:
Set multiple variables with later values depending on earlier values:
Applications (1)
With allows inserting values into unevaluated expressions:
Properties & Relations (7)
Local variables declared within a single list are independent of each other:
Local variables declared in subsequent lists depend on the variables declared in earlier lists:
With[def1,def2,expr] is equivalent to With[def1,With[def2,expr]]:
Using x:=x0 inserts the unevaluated form of x0, which then evaluates based on its surroundings:
Using x=x0 evaluates x0 once and inserts the result everywhere x appears:
Module introduces local variables to which values can be assigned:
With variables are read only:

Block localizes values only; it does not substitute values. Module creates new symbols:
With allows substitution inside an unevaluated expression, preserving nested scopes:
Possible Issues (2)
With is a scoping construct; variables are renamed in nested scopes:
Build the function from its elements to avoid the renaming:
The notebook interface by default warns if a variable is declared at multiple levels:
This valid syntax uses the last value in the function body, as would be the case with literal nesting:
Neat Examples (2)
Find a zero of an arbitrary function using Newton's method:
A version of With where the initializer is within the scope of the local variable:
Here the f inside the function definition is not inside its own scope:
Related Guides
Related Workflows
- Substitute Values of Variables in Functions That Hold Their Arguments
History
Introduced in 1991 (2.0) | Updated in 2025 (14.3)
Text
Wolfram Research (1991), With, Wolfram Language function, https://reference.wolfram.com/language/ref/With.html (updated 2025).
CMS
Wolfram Language. 1991. "With." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2025. https://reference.wolfram.com/language/ref/With.html.
APA
Wolfram Language. (1991). With. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/With.html
BibTeX
@misc{reference.wolfram_2025_with, author="Wolfram Research", title="{With}", year="2025", howpublished="\url{https://reference.wolfram.com/language/ref/With.html}", note=[Accessed: 04-August-2025]}
BibLaTeX
@online{reference.wolfram_2025_with, organization={Wolfram Research}, title={With}, year={2025}, url={https://reference.wolfram.com/language/ref/With.html}, note=[Accessed: 04-August-2025]}