Skip to content

Commit

Permalink
feat(latex): get latex render looking nice in jupyter notebooks and lab
Browse files Browse the repository at this point in the history
  • Loading branch information
sammorley-short committed May 15, 2024
1 parent ea5c285 commit 99ab4d4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
9 changes: 7 additions & 2 deletions src/bartiq/_routine.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ class Routine(BaseModel):
Attributes:
name: Name of the subroutine.
type: Type of the subroutine, might be None.
ports: Dicionary mapping port name to corresponding Port object with the same name.
ports: Dictionary mapping port name to corresponding Port object with the same name.
parent: A Routine whose this routine is subroutine of. Might be None, in which
case the routine is considered to be root of computation.
children: Dictionary mapping name of subroutine of this routine into routine
Expand All @@ -204,7 +204,7 @@ class Routine(BaseModel):
Importantly, by convention, connection objects cannot descend further then one
generation (i.e. there might not be a connection between routine and its
grandchild).
resources: Dictionary mapping name of the resource to corrresponding Resource object.
resources: Dictionary mapping name of the resource to corresponding Resource object.
input_params: Sequence of symbols determining inputs for this routine.
local_variables: Convenience aliases to expressions commonly used within this Routine.
For instance, for a Routine with input parameter d one of the local variables
Expand Down Expand Up @@ -369,6 +369,11 @@ def absolute_path(self) -> str:
"""Returns a path from root."""
return self.relative_path_from(None).removeprefix(".")

def _repr_markdown_(self):
from .integrations.latex import represent_routine_in_latex

return represent_routine_in_latex(self)


class Resource(BaseModel):
"""Resource associated with a routine.
Expand Down
15 changes: 11 additions & 4 deletions src/bartiq/integrations/latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,20 @@ def represent_routine_in_latex(routine: Routine, show_non_root_resources: bool =
Returns:
A LaTeX snippet of the routine.
"""
lines = [format_line(data) for attr_name, format_line in SECTIONS if (data := getattr(routine, attr_name))]
lines = [_format_object_header(routine)]
lines.extend([format_line(data) for attr_name, format_line in SECTIONS if (data := getattr(routine, attr_name))])

# We deal with resources separately due to show_non_root_resources option
if resource_section := _format_resources(routine, show_non_root_resources):
lines.append(resource_section)

return "\\begin{align}\n" + "\\\\\n".join(lines) + "\n\\end{align}"
return "$\\begin{align}\n" + "\\newline \n".join(lines) + "\n\\end{align}$"


def _format_object_header(routine: Routine) -> str:
"""Formats the standard object repr as a header."""
cls = type(routine)
return rf"&\text{{{cls.__name__} \textrm{{({routine.name.replace('_', r'\_')})}}}}"


def _format_input_params(input_params: list[str]):
Expand Down Expand Up @@ -92,12 +99,12 @@ def _format_local_variables(local_variables):

def _format_section_one_line(header, entries):
"""Formats a parameter section into a bolded header followed by a comma-separated list of entries."""
return f"&\\bf\\text{{{header}:}}\\\\\n&" + ", ".join(entries)
return f"&\\underline{{\\text{{{header}:}}}}\\\\\n&" + ", ".join(entries)


def _format_section_multi_line(header, lines):
"""Formats a parameter section into a bolded header followed by a series of lines."""
return f"&\\bf\\text{{{header}:}}\\\\\n" + "\\\\\n".join(lines)
return f"&\\underline{{\\text{{{header}:}}}}\\\\\n" + "\\\\\n".join(lines)


def _format_param(param):
Expand Down

0 comments on commit 99ab4d4

Please sign in to comment.