iNMR icon

Starting with Lua

Accurate documentation about Lua is available on the official web site. It is a sophisticated language, with clear definitions, but you don't need to learn it all. For all your basic tasks, and more, you only need to learn a few simple rules.
First: all the commands, even when invoked without arguments, must be followed by a pair of parentheses. For example, to show the whole spectrum, write:


You can add a comment, preceded by two dashes:

full() -- what you write after the dashes is not interpreted by the program

When there is a single argument and it is already delimited, like a literal string or a table, the parentheses are optional. Ex:

print ("hello world!")
print "hello world!"		-- identical
color { r=0.5, g=0, b=0 } -- changes the color to dark red

The color() function, like many others, doesn't redraw the spectrum. You can draw programmatically with the function amp() (no arguments) or, interactively, pressing the “Esc” key. You can separate assignements with a comma, as shown above, or a semi-colon. Now let's write a complete program, including input and output...

-- prints the square of the given number
x = ask "enter a number"         -- waits for user input
print("the square of ", x, "is ", x * x) -- compulsory parentheses

To make the program a little more professional, we can declare x as a local variable, so it ceases occupying memory after execution, and we can put the calculation inside a function:

-- a more professional version

function square( num ) -- starts the definition of a new function
  return num * num
end	-- closes the definition

local x = ask("enter a number")        -- x is declared as a local variable
print("the square of ", x, "is ", square( x )) -- calls the function

You can define your own functions for daily tasks. A fitting example is integration, which is otherwise performed with a line like:

region( 4, 3 ) press "i" -- creates an integral region between 4 and 3 ppm

You can declare:

function integral( from, to )
  region( from, to )
  press "i"

function zoom( from, to ) -- another function
  region( from, to )
  press "z"

To keep your functions always in existence, save them into a file. You should keep all your lua scripts into a folder. Then tell iNMR where the folder is (you can do it with the Preferences dialog). iNMR requires that all filenames end with “.lua” If you respect this rule, iNMR will list the rest of the name into the Scripts menu (a special, top-level, menu). If you create subfolders, iNMR will also create submenus. When you select a file from the Scripts menu, iNMR will execute the Lua code therein contained.
You'll want to create a file called “Lua.init”. It will not appear into the menu (because it hasn't the proper suffix), but it will be loaded and executed every time you open the console. Lua.init is the ideal place where you can store your “integral” function defined above and similar initializations.

The following example uses the function to create 10 integral regions, going from 0 to 1 ppm, from 2 to 3, etc..

for i = 1,10 do
  integral( i, i-1 )

It's always a good idea to learn by examples. The iNMR web site hosts a library of scripts and functions, all tested and commented.

Related Topics

Web Library of iNMR scripts

Lua built-in functions (official manual)

The Console: Command Line and Script Editor

Setting Up iNMR to Run the Scripts


Web Tutorial

Accessing Individual Documents