Variables as text storage

Back to Variables

This section introduces the basic use of variables in Remark. Here we view variables as a way to store and retrieve text.

Defining variables

[[set Gallery.thumbnail_max_width: 500]]
[[Gallery.thumbnail_max_width]]

500

[[set some_file_list]]:
    filename.txt
    another.txt
[[some_file_list]]

filename.txt another.txt

[[set_many]]:
    Gallery.thumbnail_max_width 300
    Gallery.thumbnail_max_height 400
[[Gallery.thumbnail_max_width]]
[[Gallery.thumbnail_max_height]]

300 400

[[set_many Gallery]]:
    thumbnail_max_width 200
    thumbnail_max_height 100
[[Gallery.thumbnail_max_width]]
[[Gallery.thumbnail_max_height]]

200 100

Note that with set_many it is not possible to assign multi-line parameters.

Appending to a variable

[[set some_file_list]]:
    filename.txt
    another.txt
[[add some_file_list]]:
    third_file.txt
[[some_file_list]]

filename.txt another.txt third_file.txt

Alternative

[[set some_file_list]]:
    filename.txt
    another.txt
[[+set some_file_list]]:
    [[some_file_list]]
    third_file.txt
[[some_file_list]]

filename.txt another.txt third_file.txt

However,

[[set some_file_list]]:
    filename.txt
    another.txt
[[set some_file_list]]:
    [[some_file_list]]
    third_file.txt
[[some_file_list]]

sends Remark into a never-ending loop (Python cuts the recursion).

Variable scopes

Each variable lives in some scope which defines its visibility and life-time. Each macro-invocation creates a child-scope into the current scope. Variables defined inside the macro-invocation are placed into this scope. When a macro finishes, its scope is closed and all the local variables in the scope are destroyed. When no macro invocations are active, the only active scope is the global scope. Child scopes can refer to variables in the parent scopes, but not vice versa. When a variable that does not exist in the current scope is set, a new local variable is created.

[[set word: world]]
[[set my_macro]]:
    [[set word: jello]]
    Hello [[word]]!
[[my_macro]]
word = [[word]]

Hello jello! word = world

That is, the variable word inside set my_macro is introduced as a local variable and hence does not affect the variable word at the outer scope.

Retrieving a variable in an outer scope

The get command will look at outer scopes if a variable is not found at the local scope. However, when there is both a local variable and a variable of the same name in an outer scope, the latter can be retrieved with the outer command.

[[set word: world]]
[[set my_macro]]:
    [[set word: jello]]
    Hello [[outer word]]!
[[my_macro]]
word = [[word]]

Hello world! word = world

Setting a variable in an outer scope

The set command always creates a local variable. You can use the set_outer command to set an existing variable at an outer scope. In case no such variable is found, a new variable is created at the global scope.

[[set word: world]]

[[set my_macro]]:
    [[set word: cello]]
    [[set_outer word: jello]]
    Hello [[word]]!
    Hello [[outer word]]!
[[my_macro]]
word = [[word]]

Hello cello! Hello jello! word = jello

Appending a variable in an outer scope.

The add_outer command works similarly to set_outer, but instead of assigning, it appends text.