Comment commands#
A comment that starts with hc: is a command: it changes how lines are rendered and
is not shown itself. Commands work in every interface and every built-in renderer.
x = a / b # hc: -f .2f <- applies to this line only
# hc: -f .3g <- applies to every following line
The commands#
Command |
Long form |
Effect |
|---|---|---|
|
|
Number format code, e.g. |
|
|
Break a calculation over several lines, aligned on |
|
|
Show only target = result, hiding the symbolic and substituted steps. |
|
|
Show a number times units ( |
|
|
Substitute a SymPy expression: |
|
|
Start a parameter grid of the following assignments and bare names, keeping their comments. |
|
|
Number of columns in |
|
|
Insert vertical space here. |
|
On a comparison, show ✓ or ✗ instead of |
|
|
Draw a box around the result, to make it stand out. |
|
|
Start a new page: before the next line when on its own line, after the line when inline. |
|
|
|
Run the line but leave it out of the output. On its own line, hide every following line. |
|
|
Show the line, or every following line, again after |
|
|
Show the working inside the functions the line calls, before it. See The functions a calculation calls. |
-i and -s may end with a condition, a Python expression: # hc: -s M_f > 0 shows
what follows if it is true and hides it if it is false. See
Show or hide a section on a condition.
Flags can be combined: # hc: -m -f .2f.
-w (--if-winner-only) is accepted but has no effect yet.
Scope#
Where you put a command decides how long it lasts:
At the end of a line (
x = a / b # hc: -f .2f), it applies to that line only. A line can carry a command or a normal comment, not both.On its own line (
# hc: -f .2f), it applies to every following line until another command changes it. The exceptions are-b, which inserts space once, and-g, which applies to the grid directly below it.
a = 2
b = 3
x = a / b # hc: -f .2f
y = a / b
# hc: -f .1f
z = a / b
w = b / a
$$\begin{aligned} a &= 2 \\ b &= 3 \\ x &= \frac{a}{b} = \frac{2.00}{3.00} = 0.67 \\ y &= \frac{a}{b} = \frac{2}{3} = 0.66667 \end{aligned}$$
$$\begin{aligned} z &= \frac{a}{b} = \frac{2.0}{3.0} = 0.7 \\ w &= \frac{b}{a} = \frac{3.0}{2.0} = 1.5 \end{aligned}$$
Warning
A standalone # hc: -i hides everything after it, until a # hc: -s. To hide a
single line, put # hc: -i at the end of that line.
A command changes only the settings it names: # hc: -f .2f leaves an earlier -m or
-i in effect.
Turning a setting off again#
Apart from -s, which undoes -i, the flags can only switch a setting on. To switch
one back off, or to set a value, use the assignment form described next:
# hc: multiline = False, # hc: param_line = False, # hc: format_code = ".5g".
The assignment form#
Instead of flags, a command can be one or more Python assignments:
# hc: format_code = ".2f"
# hc: param_cols = 2
x = a / b # hc: multiline = True
Each value must be a literal: a number, a string, True, False or None. The comment
is read, never run, so # hc: format_code = fc or # hc: format_code = "." + "2f" is an
error.
Each assignment sets a field on the render context, the settings object the renderer reads. The flags are shorthands for these fields:
Flag |
Context field |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Any other name is allowed too, and is passed to the renderer. A
customized or custom
renderer can read its own fields this way, for example # hc: highlight = True.
Note
The assignment form is executed as Python, so only use it in code you trust (which is already true of any code you render, because handcalcs runs it).
Choosing which steps appear: mode#
The mode field picks the steps of a calculation that are shown. The default, "full",
shows them all. Otherwise give a string containing any of:
Word |
Step |
|---|---|
|
the assignment target ( |
|
the symbolic formula ( |
|
the formula with values substituted |
|
the result |
For example, to show formulas only (symbolically, as v1’s symbolic override did):
a = 2
b = 3
# hc: mode = "ass sym"
c = 2*a + b/3
d = a**2 + b**2
$$\begin{aligned} a &= 2 \\ b &= 3 \end{aligned}$$
$$\begin{aligned} c &= 2 \cdot a + \frac{b}{3} \\ d &= a^{2} + b^{2} \end{aligned}$$
Examples#
A long equation#
omega_2 = 1.0; L = 6000; E = 200000; I_y = 160e6; G = 77e3; J = 133e3; C_w = 200e9
M_u = omega_2 * pi / L * sqrt(E * I_y * G * J + (pi * E / L)**2 * I_y * C_w) # hc: -m
$$\begin{aligned} \omega_{2} &= 1 \; &L &= 6000 \; &E &= 200000 \; &I_{y} &= 160 \times 10^{6} \; &G &= 77 \times 10^{3} \; &J &= 133 \times 10^{3} \; &C_{w} &= 200 \times 10^{9} \; \end{aligned}$$
$$\begin{aligned} M_{u} &= \frac{\omega_{2} \cdot \pi}{L} \cdot \sqrt{E \cdot I_{y} \cdot G \cdot J + \left(\frac{\pi \cdot E}{L}\right)^{2} \cdot I_{y} \cdot C_{w}} \\ &= \frac{1 \cdot 3.1416}{6000} \cdot \sqrt{200000 \cdot 1.6 \times 10^{8} \cdot 77000 \cdot 1.33 \times 10^{5} + \left(\frac{3.1416 \cdot 200000}{6000}\right)^{2} \cdot 1.6 \times 10^{8} \cdot 2 \times 10^{11}} \\ &= 4.3134 \times 10^{8} \end{aligned}$$
Results only#
a = 2
b = 3
c = a**2 + b**2 # hc: -p
$$\begin{aligned} a &= 2 \\ b &= 3 \\ c &= 13 \end{aligned}$$
A parameter grid, two columns wide#
# hc: -g -c 2
f_c = 30 # Concrete strength, MPa
f_y = 400 # Rebar yield, MPa
b = 300 # Width, mm
d = 540 # Effective depth, mm
$$\begin{aligned} f_{c} &= 30 \; \text{(Concrete strength, MPa)} \qquad &f_{y} &= 400 \; \text{(Rebar yield, MPa)} \\ b &= 300 \; \text{(Width, mm)} \qquad &d &= 540 \; \text{(Effective depth, mm)} \end{aligned}$$
Hide working lines#
L_mm = 6000
L = L_mm / 1000 # hc: -i
w = 5
M = w * L**2 / 8
$$\begin{aligned} L_{\mathrm{mm}} &= 6000 \\ w &= 5 \\ M &= \frac{w \cdot L^{2}}{8} = \frac{5 \cdot 6^{2}}{8} = 22.5 \end{aligned}$$
A pass/fail check#
M_f = 180
M_r = 240
M_f <= M_r # hc: --check
$$\begin{aligned} M_{f} &= 180 \\ M_{r} &= 240 \\ M_{f} \le M_{r} &= 180 \le 240\quad{\color{green}\checkmark} \end{aligned}$$
A boxed result#
phi = 0.9
Z_x = 1.2e6
F_y = 350
M_r = phi * Z_x * F_y / 1e6 # hc: --boxed
$$\begin{aligned} \phi &= 0.9 \\ Z_{x} &= 1.2 \times 10^{6} \\ F_{y} &= 350 \\ M_{r} &= \frac{\phi \cdot Z_{x} \cdot F_{y}}{10^{6}} = \frac{0.9 \cdot 1.2 \times 10^{6} \cdot 350}{10^{6}} = \boxed{378} \end{aligned}$$
Several settings at once#
a = 2.0
b = 7.0
# hc: format_code = ".3f"; mode = "ass sym res"
c = (a + b) / 3
d = c**2
$$\begin{aligned} a &= 2 \\ b &= 7 \end{aligned}$$
$$\begin{aligned} c &= \frac{a + b}{3} = 3.000 \\ d &= c^{2} = 9.000 \end{aligned}$$
The cookbook has many more. See the comment command reference for a compact table.