With

With[{x=x0,y=y0,},expr]

specifies that all occurrences of the symbols x, y, in expr should be replaced by x0, y0, .

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.
  • You can use With[{vars},body/;cond] as the righthand 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 is a scoping construct that implements readonly lexical variables.

Examples

open allclose all

Basic Examples  (3)

Evaluate an expression with x locally set to 7:

In[1]:=
Click for copyable input
Out[1]=

Locally set both x and y:

In[1]:=
Click for copyable input
Out[1]=

With works even without evaluation:

In[1]:=
Click for copyable input
Out[1]=

Scope  (3)

Applications  (1)

Properties & Relations  (4)

Possible Issues  (1)

Neat Examples  (2)

See Also

Module  Block  ReplaceAll

Tutorials

Introduced in 1991
(2.0)