pl-variable-output element¶
Displays a list of variables that are formatted for import into the supported programming languages and libraries (e.g. MATLAB, Mathematica, NumPy, R, or SymPy).
Sample element¶

<pl-variable-output digits="3">
<!-- Example comment inside of this element. -->
<pl-variable params-name="matrixC">C</pl-variable>
<pl-variable params-name="matrixD">D</pl-variable>
</pl-variable-output>
import prairielearn as pl
import numpy as np
def generate(data):
# Create fixed matrix
matrixC = np.matrix("5 6; 7 8")
matrixD = np.matrix("-1 4; 3 2")
# Random matrices can be generated with:
# mat = np.random.random((2, 2))
# Export each matrix as a JSON object for the question view.
data["params"]["matrixC"] = pl.to_json(matrixC)
data["params"]["matrixD"] = pl.to_json(matrixD)
Customizations¶
Attributes for <pl-variable-output>:
| Attribute | Type | Default | Description |
|---|---|---|---|
default-tab |
"matlab", "mathematica", "numpy", "r", "sympy" |
"matlab" |
Select the active tab. |
digits |
integer | 2 | Number of digits to display after the decimal. |
show-mathematica |
boolean | true | Toggles the display of the Mathematica tab. |
show-matlab |
boolean | true | Toggles the display of the Matlab tab (also compatible with Octave). |
show-numpy |
boolean | true | Toggles the display of the NumPy tab. |
show-r |
boolean | true | Toggles the display of the R tab. |
show-sympy |
boolean | true | Toggles the display of the SymPy tab. |
Attributes for <pl-variable> (one of these for each variable to display):
| Attribute | Type | Default | Description |
|---|---|---|---|
comment |
string | — | Comment to add after the displayed variable. |
digits |
integer | 2 | Number of digits to display after the decimal for the variable. Defaults to the digits value set on the enclosing <pl-variable-output>. |
params-name |
string | — | Name of variable in data["params"] to display. Required. |
Migrating from deprecated attributes¶
The following deprecated attributes and values are still supported for backward compatibility:
| Old syntax | New syntax |
|---|---|
default-tab="python" |
default-tab="numpy" |
show-python="false" |
show-numpy="false" |
show-python="true" |
show-numpy="true" |
pl-matrix-output is a deprecated alias for pl-variable-output. It supports only the Matlab and NumPy tabs and always defaults to the Matlab tab. Use pl-variable-output instead.
The legacy <variable> child tag is still accepted as a deprecated alias for <pl-variable>. Use <pl-variable> instead.
Details¶
This element displays a list of variables inside <pl-code> tags that are formatted for import into either MATLAB, Mathematica, NumPy, R, or SymPy (the user can switch between them). Each variable must be either a scalar or a 2D numpy array (expressed as a list). Each variable will be prefixed by the text that appears between the <pl-variable> and </pl-variable> tags, followed by =. Below are samples of the format displayed under each language tab.
MATLAB format:
A = [1.23; 4.56]; % matrix
Mathematica format:
A = [1.23; 4.56]; (* matrix *)
NumPy format:
import numpy as np
A = np.array([[1.23], [4.56]]) # matrix
R format:
A = c(1.23, 4.56) # vector
A = matrix(c(1.23, 4.56, 8.90, 1.23), nrow = 2, ncol = 2, byrow = TRUE) # matrix
SymPy format:
from sympy import *
A = Matrix([[1.23], [4.56]]) # matrix
If a variable v is a complex object, you should use import prairielearn as pl and data["params"][params-name] = pl.to_json(v).
Example implementations¶
See also¶
pl-matrix-latexfor displaying the matrix using LaTeX commands.pl-matrix-component-inputfor individual input boxes for each element in the matrixpl-matrix-inputfor input values formatted in a supported programming language.pl-codeto display blocks of code with syntax highlighting