Skip to content

Add _repr_latex_ for Jupyter notebooks #276

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 7, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions control/xferfcn.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,45 @@ def __str__(self, var=None):
# represent as string, makes display work for IPython
__repr__ = __str__

def _repr_latex_(self, var=None):
"""LaTeX representation of the transfer function, for Jupyter notebook"""

mimo = self.inputs > 1 or self.outputs > 1

if var is None:
# ! TODO: replace with standard calls to lti functions
var = 's' if self.dt is None or self.dt == 0 else 'z'

out = ['$$']

if mimo:
out.append(r"\begin{bmatrix}")

for i in range(self.outputs):
for j in range(self.inputs):
# Convert the numerator and denominator polynomials to strings.
numstr = _tf_polynomial_to_string(self.num[i][j], var=var)
denstr = _tf_polynomial_to_string(self.den[i][j], var=var)

out += [r"\frac{", numstr, "}{", denstr, "}"]

if mimo and j < self.outputs - 1:
out.append("&")

if mimo:
out.append(r"\\")

if mimo:
out.append(r" \end{bmatrix}")

# See if this is a discrete time system with specific sampling time
if not (self.dt is None) and type(self.dt) != bool and self.dt > 0:
out += ["\quad dt = ", str(self.dt)]

out.append("$$")

return ''.join(out)

def __neg__(self):
"""Negate a transfer function."""

Expand Down