Generalized Input

The fundamental paradigm of most computer languages, including Mathematica, is that input is given and processed into output. Historically, such input has consisted of strings of letters and numbers obeying a certain syntax.

Evaluate this input line to generate a table of output.
In[1]:=
Click for copyable input
Out[1]=

Starting in Version 3, Mathematica has supported the use of two-dimensional typeset mathematical notations as input, freely mixed with textual input.

This also generates a table of output.
In[2]:=
Click for copyable input
Out[2]=

Starting with Version 6, a wide range of nontextual objects can be used as input just as easily, and can be mixed with text or typeset notations.

Evaluate the following input, then move the slider and evaluate it again to see a new result.
In[3]:=
Click for copyable input
Out[3]=

The "value" of this slider when it is given as input is determined by its position, in this case an integer between 1 and 10. It can be used anywhere in an input line that a textual number or variable name could be used.

How to create such controls is discussed in the next section, but it is worth noting first that in many cases there are better alternatives to this kind of input.

Casting this example in the form of a Manipulate allows you to see the effect of moving the slider in real time.

But there are situations where using a control inside a traditional Shift+DynamicBox[ToBoxes[If[$OperatingSystem === MacOSX, Return, Enter], StandardForm], ImageSizeCache -> {39., {0., 9.}}] evaluated input works better. Some cases are: if the evaluation is very slow, if you want complete flexibility in editing the rest of the input line around the control(s), or if the point of the code is to make definitions that will be used later, and the controls are being used as a convenient way to specify initial values.

For example, you might want to set up a color palette using ColorSetter to initialize named colors that will be used in subsequent code.
In[5]:=
Click for copyable input
In[6]:=
Click for copyable input
In[7]:=
Click for copyable input
Click any color swatch to get a dialog allowing you to interactively choose a new color. These values can then be used in subsequent programming just as if they had been initialized with more traditional textually specified values.
In[8]:=
Click for copyable input
Out[8]=

These color swatches provide an informative, more easily edited representation of the colors than would an expression consisting of numerical color values.

How to Create Inline Controls

The most flexible and powerful way to create anything in Mathematica is to evaluate a function that returns it.

These examples use Slider, but the same principles apply to any controls. Control Objects lists all the available control objects.

You can create a slider by evaluating Slider[].
In[9]:=
Click for copyable input
Out[9]=

The resulting slider object can be copied and pasted into a subsequent input cell just as if it were any other kind of input. (Or you can just click in the output cell and start typing, which will cause it to be converted automatically into an input cell.)

Controls created this way are inert to evaluation.

For example, type , then paste the previous slider after the to create this input line, and then evaluate it.
In[10]:=
Click for copyable input
Out[10]=

When evaluated, the slider remains a slider, which is not wanted in this case (though it is very useful in other situations). What is needed instead is a slider that, when evaluated as input, becomes the value it is set to, rather than a slider object.

DynamicSetting[e]an object that displays as e, but is interpreted as the dynamically updated current setting of e upon evaluation

Object that evaluates into its current setting.

When DynamicSetting is wrapped around a slider and evaluated, the new slider looks identical to the original one, but has the hidden property of evaluating into its current setting.

This displays the new slider.
In[11]:=
Click for copyable input
Out[11]=
If the new slider is copied and pasted into an input line, the act of evaluation transforms the slider into its current value (by default 0.5 if it has not been moved with the mouse).
In[12]:=
Click for copyable input
Out[12]=

The examples in the previous section were created using DynamicSetting in this way.

While copying and pasting can be used very effectively to build up input lines containing controls, it is often most convenient to use Evaluate in Place Ctrl+Shift+Enter (Command+Return on Mac) to transform control expressions in place, especially once you are familiar with the commands that create controls.

Ctrl+Shift+Enterevaluate a selection "in place", replacing the selection with the output

Evaluating in place.

For example, enter the following input line.
In[13]:=
Click for copyable input
Out[13]=
Then, highlight the entire control expression. (Triple-clicking the word DynamicSetting is an especially quick way to do this.)
In[14]:=
Click for copyable input
Type Ctrl+Shift+Enter (Command+Return on Mac), and the control expression will be transformed in place into an actual control. (Note that Ctrl+Shift+Enter is not the normal Shift+Enter used for evaluating input.)
In[14]:=
Click for copyable input
Evaluating the input cell with Shift+Enter will then give the desired result.
In[15]:=
Click for copyable input
Out[15]=

All the arguments of Slider can be used to change the initial value, range, and step size.

Start with this input expression.
In[16]:=
Click for copyable input
Out[16]=
Then evaluate in place (Ctrl+Shift+Enter) to transform the text command into a slider.
In[17]:=
Click for copyable input
Out[17]=
Of course, this works with other kinds of controls as well.
In[18]:=
Click for copyable input
Out[18]=
This is the result after evaluating in place.
In[19]:=
Click for copyable input
Out[19]=

Note that the control expressions do not contain a dynamic reference to a variable as they normally would (see "Introduction to Dynamic"). Controls used in input expressions as described here are static, inert objects, much like a textual command. They are not linked to each other, and nothing happens when you move one, except that it moves. Basically they are simply recording their current state, for use when you evaluate the input line.

Complex Templates in Input Expressions

It is possible to use whole panels containing multiple controls in input expressions. Constructing such panels is more complex than simply wrapping DynamicSetting around a single control, because you have to specify how the various control values should be assembled into the value returned when the template is evaluated.

The function Interpretation is used to assemble a self-contained input template object, which may contain many internal parts that interact with each other through dynamic variables. The arguments are Interpretation[variables, body, returnvalue].

The first argument gives a list of local variables with optional initializers in the same format as Module or DynamicModule. (In fact, Interpretation creates a DynamicModule in the output. See "Introduction to Dynamic".)

The second argument is typeset to become the displayed form of the interpretation object. Typically it contains formatting constructs and controls with dynamic references to the variables defined in the first argument.

The third argument is the expression that will be used as the value of the interpretation object when it is given as input. Typically this is an expression involving the variables listed in the first argument.

Interpretation[e,expr]an object which displays as e, but is interpreted as the unevaluated form of expr if supplied as input
Interpretation[{x=x0,y=y0,...},e,expr]allows local variables in e and expr

Object that displays as one expression and evaluates as another.

Evaluating the following input cell creates an output cell containing a template for the Plot command.
In[20]:=
Click for copyable input
Out[20]=
This template can be copied and pasted into an input cell, and the values edited as you like. Shift+DynamicBox[ToBoxes[If[$OperatingSystem === MacOSX, Return, Enter], StandardForm], ImageSizeCache -> {39., {0., 9.}}] evaluation of the input cell generates a plot.
In[21]:=
Click for copyable input
Out[21]=
In the following more sophisticated example, the variable definite is used to communicate between the controls in the template, dimming the min and max value fields when indefinite integration is selected.
In[1]:=
Click for copyable input
Out[1]=
This copy of the previous template gives the integral upon evaluation.
In[23]:=
Click for copyable input
Out[23]=

As with the single controls in earlier sections, these input templates can be copied and pasted into new input cells, and they can be freely intermixed with textual input.

To test the result of integration, wrap the template with D to take the derivative and verify that the result is the same as the starting point.
In[24]:=
Click for copyable input
Out[24]=

These examples are fairly generic: they look like dialog boxes in a lot of programs. But there are some important differences. For example, note the in the input field. Input fields in Mathematica may look like those in other programs, but the contents can be any arbitrary typeset mathematics, or even graphics or other controls. (See the next section to learn how to write templates that can be nested inside each other.)

Mathematica templates (and dialog boxes) are also not restricted to using a regular grid of text fields.

Here is a simple definite integration template laid out typographically.
In[25]:=
Click for copyable input
Out[25]=
Note that you do not need a template to evaluate integrals; they can be entered as plain typeset math formulas using keyboard shortcuts (as described in "Entering Two-Dimensional Expressions") or the Basic Input palette.
In[26]:=
Click for copyable input
Out[26]=

Whether it is useful to make a template like this or not depends on many things, but the important point is that in Mathematica the full range of formatting constructs, including text, typeset math, and graphics, is available both inside and around input fields and templates.

Advanced Topic: Dealing with Premature Evaluation in Templates

Templates defined like those in the previous section do not work as you might hope if the variables given in initializers already have other values assigned to them (for example, if the variable x has a value in the previous section), or if template structures are pasted into the input fields. To deal with evaluation issues correctly, it is necessary to use InputField objects that store their values in the form of unparsed box structures rather than expressions. (Box structures are like strings in the sense that they represent any possible displayable structure, whether it is a legal Mathematica input expression or not.)

This defines a template.
In[27]:=
Click for copyable input
Out[27]=
This copy of the previous template gives the integral upon evaluation.
In[28]:=
Click for copyable input
Out[28]=
This template will work properly even under what might be considered abuse. For example, you can nest it repeatedly to integrate a function several times.
In[29]:=
Click for copyable input
Out[29]=

Note how the InputField grows automatically to accommodate larger contents. (This behavior is controlled by the FieldSize option.)

The typographic template can also be made robust to evaluation.
In[30]:=
Click for copyable input
Out[30]=
And it can be nested, though this kind of thing can easily get out of hand, so it is probably more fun than useful.
In[31]:=
Click for copyable input
Out[31]=

Graphics as Input

Graphic objects, including the output of Graphics, Graphics3D, plotting commands, and graphics imported from external image files, can all be used as input and freely mixed with textual input. There are no arbitrary limitations in the mixing of graphics, controls, typeset mathematics, and text.

Evaluate a simple plot command.
In[32]:=
Click for copyable input
Out[32]=
Then click to place the insertion point just to the left of the plot and type .
In[33]:=
Click for copyable input
Complete the command by clicking and typing to the right of the plot, then evaluating.
In[2]:=
Click for copyable input
Out[2]=

Notice how the plot appears in several different sizes depending on its context. There are three standard automatic plot sizes, the largest if the plot is by itself in an output, smaller if used in a list or table output, and smallest if in a textual input line. This is mainly a convenience to make graphical input less bulky. You are always free to resize the graphic by clicking it and using the resize handles, or by including an explicit ImageSize option.

You can import a bitmap image from a file.
In[29]:=
Click for copyable input
Out[29]=
Then copy/paste this into an input cell to do simple image processing on it.
In[30]:=
Click for copyable input
Out[30]=
The ability to use graphics as input allows for remarkably rich input, as in this simple Manipulate example.
New to Mathematica? Find your learning path »
Have a question? Ask support »