Variables

%reload_ext rubberize
from rubberize.config import config

Python variables are rendered considering the following rules:

%%tap --dead --grid
a; b; x; y; apple; boy; xylophone; yellow
\( \displaystyle a \)
\( \displaystyle b \)
\( \displaystyle x \)
\( \displaystyle y \)
\( \displaystyle \mathrm{apple} \)
\( \displaystyle \mathrm{boy} \)
\( \displaystyle \mathrm{xylophone} \)
\( \displaystyle \mathrm{yellow} \)

Depending on the Config Options, additional rendering rules are applied.

Subscripts

@use_subscripts controls whether subscript rules will be applied to variables:

config.use_subscripts
True

When it is set to True, the following rules apply:

Names with Underscores

The first underscore is treated as a subscript marker. The left part is rendered as the base name of the variable, and the right part is rendered as the variable subscript.

Subsequent underscores are rendered as commas within the subscript part.

%%tap --dead --grid
f_o; I_x_male; foo_bar; foo_a_bravo_c_d
\( \displaystyle f_{o} \)
\( \displaystyle I_{x, \mathrm{male}} \)
\( \displaystyle \mathrm{\bar{foo}} \)
\( \displaystyle \mathrm{foo}_{a, \mathrm{bravo}, c, d} \)

Two or more consecutive underscores are rendered as a single literal underscore rather than indicating a subscript.

%%tap --dead --grid
f__o; I__x_male; I_x__male; foo__a_bravo_c__d
\( \displaystyle \mathrm{f\_o} \)
\( \displaystyle \mathrm{I\_x}_{\mathrm{male}} \)
\( \displaystyle I_{\mathrm{x\_male}} \)
\( \displaystyle \mathrm{foo\_a}_{\mathrm{bravo}, \mathrm{c\_d}} \)

Leading and Trailing Underscores

Leading and trailing underscores are not converted to subscripts. If the variable name includes subscripts, the leading and trailing underscores are attached to the base part and not on the subscript.

%%tap --dead --grid
_private; __very__private; __init__; __base_subscript__
\( \displaystyle \_\mathrm{private} \)
\( \displaystyle \_\_\mathrm{very\_private} \)
\( \displaystyle \_\_\mathrm{init}\_\_ \)
\( \displaystyle \_\_\mathrm{base}\_\__{\mathrm{subscript}} \)

Disabling Subscripts

When @use_subscripts=False, all underscores are shown as literal underscores, and variables will be rendered exactly as they are written but using a Roman font.

%%tap --dead --grid @use_subscripts=False
I_x_male; I__x_male; foo__a_bravo_c__d; __base_subscript__
\( \displaystyle I_{x, \mathrm{male}} \)
\( \displaystyle \mathrm{I\_x}_{\mathrm{male}} \)
\( \displaystyle \mathrm{foo\_a}_{\mathrm{bravo}, \mathrm{c\_d}} \)
\( \displaystyle \_\_\mathrm{base}\_\__{\mathrm{subscript}} \)

Math Symbols

@use_symbols controls whether certain words will be transformed to symbols in variables:

config.use_symbols
True

When it is set to True, Greek letters, accents, and modifier keywords are rendered with their proper math symbols.

Greek Letters

Python variable name parts that match Greek or Hebrew letter names are replaced with their corresponding symbols. For example, alpha becomes \(\alpha\), and Omega becomes \(\Omega\).

%%tap --dead --grid
x_gamma; Omega_b_o; delta_max; phiR_n
\( \displaystyle x_{\gamma} \)
\( \displaystyle \Omega_{b, o} \)
\( \displaystyle \delta_{\mathrm{max}} \)
\( \displaystyle \phi R_{n} \)

These are all the Greek letter names that are converted when used:

%%tap --dead --grid
alpha; beta; (Gamma, gamma); (Delta, delta)
(epsilon, varepsilon); zeta; eta; (Theta, theta, vartheta)
iota; (kappa, varkappa); (Lambda, lambda_); mu
nu; (Xi, xi); omicron; (Pi, pi, varpi)
(rho, varrho); (Sigma, sigma, varsigma); tau; (Upsilon, upsilon)
(Phi, phi, varphi); chi; (Psi, psi); (Omega, omega)
\( \displaystyle \alpha \)
\( \displaystyle \beta \)
\( \displaystyle \left( \Gamma,\, \gamma \right) \)
\( \displaystyle \left( \Delta,\, \delta \right) \)
\( \displaystyle \left( \epsilon,\, \varepsilon \right) \)
\( \displaystyle \zeta \)
\( \displaystyle \eta \)
\( \displaystyle \left( \Theta,\, \theta,\, \vartheta \right) \)
\( \displaystyle \iota \)
\( \displaystyle \left( \kappa,\, \varkappa \right) \)
\( \displaystyle \left( \Lambda,\, \lambda \right) \)
\( \displaystyle \mu \)
\( \displaystyle \nu \)
\( \displaystyle \left( \Xi,\, \xi \right) \)
\( \displaystyle \omicron \)
\( \displaystyle \left( \Pi,\, \pi,\, \varpi \right) \)
\( \displaystyle \left( \rho,\, \varrho \right) \)
\( \displaystyle \left( \Sigma,\, \sigma,\, \varsigma \right) \)
\( \displaystyle \tau \)
\( \displaystyle \left( \Upsilon,\, \upsilon \right) \)
\( \displaystyle \left( \Phi,\, \phi,\, \varphi \right) \)
\( \displaystyle \chi \)
\( \displaystyle \left( \Psi,\, \psi \right) \)
\( \displaystyle \left( \Omega,\, \omega \right) \)

Due to name conflict with the Python lambda statement, lambda_ is used to render a standalone \(\lambda\).

Note

Capital Greek letters that resemble Latin letters (A, B, E, Z, H, K, I, M, N, O, R, T, and X) are excluded to avoid ambiguity.

In addition, these Hebrew letter names are also converted:

%%tap --dead --grid
digamma; aleph; beth; gimel
\( \displaystyle \digamma \)
\( \displaystyle \aleph \)
\( \displaystyle \beth \)
\( \displaystyle \gimel \)

Starting Greek Letter

If the base part of a variable starts with certain Greek letters, it will be rendered as a symbol alongside the rest of the base part.

%%tap --dead --grid
DeltaT; psiR_k
\( \displaystyle \Delta T \)
\( \displaystyle \psi R_{k} \)

These Greek letter keywords that are defined in @greek_starts:

config.greek_starts
{'Delta', 'gamma', 'phi', 'psi'}

To add or remove Greek letters that should be rendered when it appears in the beginning of the base name part, use the config methods add_greek_start() or remove_greek_start(), respectively.

For example:

config.add_greek_start("Sigma", "Pi")
config.remove_greek_start("gamma", "phi", "psi")

config.greek_starts
{'Delta', 'Pi', 'Sigma'}
%%tap --dead --grid
Sigmaboy; phiF_y
\( \displaystyle \Sigma \mathrm{boy} \)
\( \displaystyle \mathrm{phiF}_{y} \)

Accents and Modifiers

Variable names with specific keywords after an underscore (e.g., _hat, _bar) are rendered with the corresponding accent or modifier.

%%tap --dead --grid
x_bar; k_hat; f_prime; L_star_a
\( \displaystyle \bar{x} \)
\( \displaystyle \hat{k} \)
\( \displaystyle f' \)
\( \displaystyle L^{*}_{a} \)

These are all the accents and modifiers that can be converted:

%%tap --dead --grid
foo_hat; foo_widehat; foo_bar; foo_widebar
foo_tilde; foo_widetilde; foo_dot; foo_ddot
foo_dddot; foo_ddddot; foo_breve; foo_check
foo_acute; foo_grave; foo_ring; foo_mat
foo_vec; foo_vec2; foo_widevec2; foo_prime
foo_star; foo_sstar; foo_ssstar; foo_sssstar
foo_plus; foo_minus; foo_oplus; foo_ominus
foo_plusminus
\( \displaystyle \mathrm{\hat{foo}} \)
\( \displaystyle \mathrm{\widehat{foo}} \)
\( \displaystyle \mathrm{\bar{foo}} \)
\( \displaystyle \mathrm{\overline{foo}} \)
\( \displaystyle \mathrm{\tilde{foo}} \)
\( \displaystyle \mathrm{\widetilde{foo}} \)
\( \displaystyle \mathrm{\dot{foo}} \)
\( \displaystyle \mathrm{\ddot{foo}} \)
\( \displaystyle \mathrm{\dddot{foo}} \)
\( \displaystyle \mathrm{\ddddot{foo}} \)
\( \displaystyle \mathrm{\breve{foo}} \)
\( \displaystyle \mathrm{\check{foo}} \)
\( \displaystyle \mathrm{\acute{foo}} \)
\( \displaystyle \mathrm{\grave{foo}} \)
\( \displaystyle \mathrm{\mathring{foo}} \)
\( \displaystyle \mathrm{\mathbf{foo}} \)
\( \displaystyle \mathrm{\mathbf{foo}} \)
\( \displaystyle \mathrm{\vec{foo}} \)
\( \displaystyle \mathrm{\overrightarrow{foo}} \)
\( \displaystyle \mathrm{foo'} \)
\( \displaystyle \mathrm{foo^{*}} \)
\( \displaystyle \mathrm{foo^{**}} \)
\( \displaystyle \mathrm{foo^{***}} \)
\( \displaystyle \mathrm{foo^{****}} \)
\( \displaystyle \mathrm{foo^{+}} \)
\( \displaystyle \mathrm{foo^{-}} \)
\( \displaystyle \mathrm{foo^{\oplus}} \)
\( \displaystyle \mathrm{foo^{\ominus}} \)
\( \displaystyle \mathrm{foo^{\pm}} \)
Tip

foo_mat and foo_vec will render similarly. To avoid confusion, they are not recommended to be used together on a single notebook.

Disabling Symbols

When @use_symbols=False, all greek, accent, and modifier keywords will be rendered exactly as they are typed but using a Roman font.

%%tap --dead --grid @use_symbols=False
alpha_widebar
\( \displaystyle \overline{\alpha} \)

Attributes

Attribute access is rendered the same as you would write it in Python.

class Foo:
    def __init__(self):
        self.bar = 42  # Instance attribute
%%tap
foo = Foo()
foo.bar  # Attribute access
\( \displaystyle \mathrm{foo} = \operatorname{Foo} \left(\right) \)
\( \displaystyle \mathrm{foo}.\mathrm{bar} = 42 \)
Attribute access

Hidden Modules

Some module names, such as math, are not rendered, leaving only its attributes:

%%tap
import math

math.pi  # Hidden module name
\( \displaystyle \pi = 3.14 \)
Hidden module name

This is controlled by the @hidden_modules:

config.hidden_modules
{'math', 'np', 'sp', 'ureg'}

To add or remove module names that should be hidden when the attribute is rendered, use the config methods add_hidden_module() or remove_hidden_module(), respectively.

For example:

config.add_hidden_module("scipy", "foo")
config.remove_hidden_module("math")

config.hidden_modules
{'foo', 'np', 'scipy', 'sp', 'ureg'}
%%tap
foo.bar  # `foo` now hidden
math.pi  # `math` now shown
\( \displaystyle \mathrm{bar} = 42 \)
foo now hidden
\( \displaystyle \mathrm{math}.\pi = 3.14 \)
math now shown