# Generalized Input

How to Create Inline Controls | Advanced Topic: Dealing with Premature Evaluation in Templates |

Complex Templates in Input Expressions | Graphics as 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.

In[1]:= |

Out[1]= |

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

In[2]:= |

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.

In[3]:= |

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.

In[4]:= |

Out[4]= | Play Animation ▪ |

But there are situations where using a control inside a traditional 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.

In[5]:= |

In[6]:= |

In[7]:= |

In[8]:= |

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.

In[9]:= |

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.

In[10]:= |

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.

In[11]:= |

Out[11]= |

In[12]:= |

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.

In[13]:= |

Out[13]= |

In[14]:= |

In[14]:= |

In[15]:= |

Out[15]= |

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

In[16]:= |

Out[16]= |

In[17]:= |

Out[17]= |

In[18]:= |

Out[18]= |

In[19]:= |

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=x_{0},y=y_{0},...},e,expr] | allows local variables in e and expr |

Object that displays as one expression and evaluates as another.

In[20]:= |

Out[20]= |

In[21]:= |

Out[21]= |

In[1]:= |

Out[1]= |

In[23]:= |

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.

In[24]:= |

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.

In[25]:= |

Out[25]= |

In[26]:= |

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.)

In[27]:= |

Out[27]= |

In[28]:= |

Out[28]= |

In[29]:= |

Out[29]= |

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

In[30]:= |

Out[30]= |

In[31]:= |

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.

In[32]:= |

Out[32]= |

In[33]:= |

In[2]:= |

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.

In[29]:= |

Out[29]= |

In[30]:= |

Out[30]= |

In[31]:= |

Out[31]= | Play Animation ▪ |