Overview of Mathematica Syntax

This page contains a very brief overview of the syntax used throughout the rest of the article. Further information can be found at Wolfram Research's website. Their Documentation Center currently offers free access to The Mathematica Book (Wolfram, 2003) which describes all aspects of the program. The Document Center also has a searchable index of Mathematica functions with examples.

If you wish, you may jump directly to one of the following topics by clicking on the link.


Any expression delineated by (* and *), such as

(* Sample Comment *)

is essentially invisible to either Mathematica or LiveGraphics3D.

Mathematical Constants

Mathematica and LiveGraphics3D recognize a number of mathematical constants. The most recognizable of these are Pi and E. (The capitalization is important.) The constant Degree might also be useful to convert from degrees to radians. It is defined to be Pi/180 so that, for example, 270*Degree evaluates to 3*Pi/2.

Assignments and Rules

As with most languages, you can assign a value to a variable using an equal sign, as in a = 2. It is not necessary to declare the data type of a variable before assigning it a value, and Mathematica does automatic conversion between the various types of numbers. In LiveGraphics3D, these are all treated as (double precision) floating point numbers.

LiveGraphics3D's parameters use a slightly different construction known as a rule, written a -> 2. Within Mathematica there are significant differences between = and ->, but these are not important for the scope of this article.

If Statement

The If statement in Mathematica has the following syntax.

If[ test, trueResult, falseResult ]

Here test is a condition such as x => 0, x < 0 or x == 0. If test is true, the If statement returns the expression trueResult; otherwise the If statement evaluates to falseResult. It is common in LiveGraphics3D to use nested If statments, such as:

If[ x < 0, 0, If[ x > 1, 1, x] ]

This statement returns 0 if x < 0, 1 if x > 1, and x otherwise.


Mathematica's functions are always capitalized and use square brackets around their arguments. For example, the following input would compute the square root of 5. (Mathematica would do this symbolically, but LiveGraphics3D converts all numbers to a decimal format.)


Common functions such as Sin[x], Cos[x], Exp[x] and Abs[x] should all be recognizable in context.

User Defined Functions

User defined functions can be given names. For example,


(Here the use of the underscore in x_ is due to the way Mathematica uses its pattern-matching abilities when defining functions.) With the given definition, f[2] would return a value of 1, whereas f[π] would return a value of (1-π)2.

Lists and Tables

A list is one of the basic objects in Mathematica. Elements of a list are separated by commas and are surrounded by curly brackets:


In this article we will use two different types of lists. The first is a list of three numbers, like the example above; this is how Mathematica represents coordinates in three-dimensions. The second is a list of graphical objects to be plotted on the screen; more on that below.

Mathematica's implementation of vectors is based on lists, so we can use scalar multiplication, component-wise addition, the dot product and (in three-dimensions) the cross product:

Lists of objects can be easily constructed using Mathematica's Table function. The function

Table[ expression, {var, min, max(, optional stepsize)} ]

creates a list of the values of expression as the variable var runs from the given minimum value to the stated maximum. (If the minimum and maximum values are integers, the default stepsize is 1; this can be adjusted using the optional stepsize in the command.) For example, the command

Table[ i, {i, 0, 5} ]

generates the list {0, 1, 2, 3, 4, 5}, whereas

Table[ i, {i, 0, 5, 2.5} ]

results in {0, 2.5, 5}.

Table can generate multi-dimensional lists as well. Consider the following commands:

Table[ {i, j, f[i,j]}, {i,-1,1},{j,-1,1}]

This generates the following list; ignoring the extra curly brackets for now, it is nothing more than a list of points on the graph of z=x2-y2.

{{{-1, -1, 0}, {-1, 0, 1}, {-1, 1, 0}},
{{0, -1, -1}, {0, 0, 0}, {0, 1, -1}},
{{1, -1, 0}, {1, 0, 1}, {1, 1, 0}}}

Graphics3D Objects

A three-dimensional picture is represented with the following syntax.

Graphics3D[ list of graphics primitives, list of graphics options ]

Graphics primitive is a name given to objects such as points and polygons. These are all defined in terms of coordinates which, as mentioned above, are represented by a list of three numbers. Hence the origin in three-dimensional space is written:

{0, 0, 0}

The rest of this overview covers the most common graphics primitives and options.

Graphics Primitives

This article uses four basic graphics primitives: points, lines, polygons and text. Each is described below.


To draw a three-dimensional point, use the Point primitive. The only argument is the list of coordinates.



A line segment between two points is created using the Line primitive, together with a list of the two points.

Line[{{0,0,0}, {1,2,3}}]

In fact, Line can accept an arbitrarily long list of points; line segments are drawn from one point to the next in the list. This allows us to use Line to create curves. For example,

f[t_]={Cos[t], Sin[t], t}
Line[ Table[ f[t], {t, 0, 2Pi, 2Pi/30.} ] ]

represents 30 tiny line segments which approximate the graph of the helix parametrized by f[t]. (The Table function generates the list of points on the curve.) These segments are shown in the following LiveGraphics3D applet, whose input was generated by the command Graphics3D[ Line[ Table[ f[t], {t, 0, 2Pi, 2Pi/30.} ] ] ]


A filled-in polygon is described by the Polygon primitive, together with an ordered list of its vertices. The following primitive represents the unit square in the plane z=0.

Polygon[{{0,0,0}, {1,0,0}, {1,1,0}, {0,1,0}}]

When creating surfaces, we generally want a long list of polygons which approximate the surface. This can be created with the Table function.

dx = 0.1
dy = 0.1
Table[ Polygon[{{i, j, f[i,j]}, {i+dx, j, f[i+dx,j]},
                {i+dx, j+dy, f[i+dx,j+dy]}, {i, j+dy, f[i,j+dy]}}],
        {i,-1,1-dx/2,dx}, {j,-1,1-dy/2,dy}]

The commands above generate a list of polygons which approximate the graph of z=x2-y2. (For the curious reader, the use of 1-dx/2 and 1-dy/2 instead of 1 is to prevent Mathematica from generating extra polygons, where x and y would range from 1 to 1.1.) Here are those polygons, as displayed by LiveGraphics3D;


You can insert text into a picture using the Text[ "string", coordinates ] primitive. When using LiveGraphics3D's INPUT parameter (as opposed to INPUT_FILE -- see the main article), you should replace " with a pair of single quotes '' in your definition of the string. (Replacing " with &quot; may also work with some browsers.) The following LiveGraphics3D applet was given the input Graphics3D[ Text[ ''Hello World!'', {0,0,0} ]. Try rotating the box; note that LiveGraphics3D does not rotate the text, but instead always leaves it facing the reader.

Graphics Directives

The appearance of a graphics primitive can be altered using special functions known as graphics directives. The most common of these include:

There are complicated issues involving lists and the "scope" of a directive, but at the most basic level you use them by replacing a primitive (such as Point[{1,2,3}] with a list. The last element of the list should be the primitive; the preceding elements are directives. For example,

{RGBColor[1,0,0], PointSize[0.05], Point[{1,2,3}]}.

This new object is displayed in the following applet. The point is now red, and five times larger than normal.

Graphics3D Options

Options for Graphics3D objects are invoked using the format OptionName -> Setting. While there are too many options to mention here, we can describe a few of the more commonly used ones.

Putting Everything Together

Compare the input for LiveGraphics3D shown here to the picture you see in the applet. If you can see how the primitives, directives and options lead the displayed picture, you know enough about Mathematica's syntax to create your own material with LiveGraphics3D. Note that the Lighting is set to False; because the square in the plane x = 1 has no RBGColor[] directive, it is black on both sides.

          {RGBColor[0,1,0], PointSize[0.05], Point[{1,2,3}]},
          {EdgeForm[], FaceForm[ RGBColor[1,0,0], RGBColor[0,0,1] ],
          {RGBColor[0,1,1], Thickness[0.05], Line[{{-1,1,2},{1,1,2}}]}
     Lighting->False, Boxed->False, Axes->True, AxesLabel->{X,Y,Z}]