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[{x = 7}, x ^ 2]With[{x = 7, y = a + 1}, x / y]With works even without evaluation:
With[{x = a}, HoldComplete[1 + x ^ 2]]Define the local variable y in terms of the earlier local variable x:
With[{x = 5}, {y = x + 1}, y ^ 2]Scope (5)
Use With to insert values into held expressions:
With[{x = y}, Hold[x]]Table[With[{i = j}, Hold[i]], {j, 5}]Use := to insert the unevaluated form of an expression into the result:
With[{x := 2 + 2}, Hold[x]]With[{x = 2 + 2}, Hold[x]]The variable names can be the same:
x = 5;
With[{x = x}, Hold[x]]Use a constant for a value that is needed more than once:
With[{y = Sin[1.0]}, Sum[y ^ i, {i, 0, 10}]]Set multiple variables with independent values:
With[{x = y + 1, y = x - 1}, {x, y}]Set multiple variables with later values depending on earlier values:
With[{x = y + 1}, {y = x - 1}, {x, y}]Applications (1)
With allows inserting values into unevaluated expressions:
With[{v = {a, b, c}, w = {x, y, z}}, Thread[Unevaluated[Dot[v, w]]]]Thread[Dot[{a, b, c}, {x, y, z}]]Properties & Relations (7)
Local variables declared within a single list are independent of each other:
With[{x = 5, y = x + 3}, {x, y}]Local variables declared in subsequent lists depend on the variables declared in earlier lists:
With[{x = 5}, {y = x + 3}, {x, y}]With[def1,def2,expr] is equivalent to With[def1,With[def2,expr]]:
With[{x = 5}, {y = Hold[x ^ 2]}, y]% === With[{x = 5}, With[{y = Hold[x ^ 2]}, y]]Using x:=x0 inserts the unevaluated form of x0, which then evaluates based on its surroundings:
With[{x := RandomReal[]}, {x, x, Hold[x]}]Using x=x0 evaluates x0 once and inserts the result everywhere x appears:
With[{x = RandomReal[]}, {x, x, Hold[x]}]Module introduces local variables to which values can be assigned:
Module[{x = 2.0}, While[x > 0, x = Log[x]];x]With variables are read only:
With[{x = 2.0}, x = x]Timing[Do[Module[{x = 5}, x;], {10 ^ 5}]]Timing[Do[With[{x = 5}, x;], {10 ^ 5}]]Block localizes values only; it does not substitute values. Module creates new symbols:
{Block[{x = 5}, Hold[x]], With[{x = 5}, Hold[x]], Module[{x = 5}, Hold[x]]}ReleaseHold[%]With allows substitution inside an unevaluated expression, preserving nested scopes:
With[{e = x}, Function[x, e]]Ordinary substitution does not preserve scoping:
Function[x, e] /. e :> xPossible Issues (2)
With is a scoping construct; variables are renamed in nested scopes:
With[{e = Expand[(1 + x) ^ 5]}, Function[x, e]]%[10]Build the function from its elements to avoid the renaming:
With[{e = Expand[(1 + x) ^ 5]}, Function@@{x, e}]%[10]The notebook interface by default warns if a variable is declared at multiple levels:
With[{x = 1}, {x = x + 1}, HoldComplete[x]]This valid syntax uses the last value in the function body, as would be the case with literal nesting:
With[{x = 1}, With[{x = x + 1}, HoldComplete[x]]]Neat Examples (2)
Find a zero of an arbitrary function using Newton's method:
newton[f_, x0_] :=
With[{fp = f'}, FixedPoint[# - f[#] / fp[#]&, x0]]newton[Cos, 1`20]newton[Cos[#] - #&, 1`20]A version of With where the initializer is within the scope of the local variable:
SetAttributes[letrec, HoldAll];
letrec[{var_ = val_}, body_] := Module[{var}, var = val;body]letrec[{f = Function[n, If[n == 0, 1, n * f[n - 1]]]}, f[10]]Here the f inside the function definition is not inside its own scope:
With[{f = Function[n, If[n == 0, 1, n * f[n - 1]]]}, f[10]]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_2026_with, author="Wolfram Research", title="{With}", year="2025", howpublished="\url{https://reference.wolfram.com/language/ref/With.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_with, organization={Wolfram Research}, title={With}, year={2025}, url={https://reference.wolfram.com/language/ref/With.html}, note=[Accessed: 12-June-2026]}