Latex Printing

Pyomo models can be printed to a LaTeX compatible format using the pyomo.contrib.latex_printer.latex_printer function:

pyomo.contrib.latex_printer.latex_printer.latex_printer(pyomo_component, latex_component_map=None, ostream=None, use_equation_environment=False, explicit_set_summation=False, throw_templatization_error=False)[source]

This function produces a string that can be rendered as LaTeX

Prints a Pyomo component (Block, Model, Objective, Constraint, or Expression) to a LaTeX compatible string

Parameters:
  • pyomo_component (BlockData or Model or Objective or Constraint or Expression) – The Pyomo component to be printed

  • latex_component_map (pyomo.common.collections.component_map.ComponentMap) – A map keyed by Pyomo component, values become the LaTeX representation in the printer

  • ostream (io.TextIOWrapper or io.StringIO or str) – The object to print the LaTeX string to. Can be an open file object, string I/O object, or a string for a filename to write to

  • use_equation_environment (bool) –

    If False, the equation/aligned construction is used to create a single

    LaTeX equation. If True, then the align environment is used in LaTeX and each constraint and objective will be given an individual equation number

  • explicit_set_summation (bool) – If False, all sums will be done over ‘index in set’ or similar. If True, sums will be done over ‘i=1’ to ‘N’ or similar if the set is a continuous set

  • throw_templatization_error (bool) – Option to throw an error on templatization failure rather than printing each constraint individually, useful for very large models

Returns:

A LaTeX string of the pyomo_component

Return type:

str

Note

If operating in a Jupyter Notebook, it may be helpful to use:

from IPython.display import display, Math

display(Math(latex_printer(m))

Examples

A Model

>>> import pyomo.environ as pyo
>>> from pyomo.contrib.latex_printer import latex_printer

>>> m = pyo.ConcreteModel(name = 'basicFormulation')
>>> m.x = pyo.Var()
>>> m.y = pyo.Var()
>>> m.z = pyo.Var()
>>> m.c = pyo.Param(initialize=1.0, mutable=True)
>>> m.objective    = pyo.Objective( expr = m.x + m.y + m.z )
>>> m.constraint_1 = pyo.Constraint(expr = m.x**2 + m.y**2.0 - m.z**2.0 <= m.c )

>>> pstr = latex_printer(m)

A Constraint

>>> import pyomo.environ as pyo
>>> from pyomo.contrib.latex_printer import latex_printer

>>> m = pyo.ConcreteModel(name = 'basicFormulation')
>>> m.x = pyo.Var()
>>> m.y = pyo.Var()

>>> m.constraint_1 = pyo.Constraint(expr = m.x**2 + m.y**2 <= 1.0)

>>> pstr = latex_printer(m.constraint_1)

A Constraint with Set Summation

>>> import pyomo.environ as pyo
>>> from pyomo.contrib.latex_printer import latex_printer
>>> m = pyo.ConcreteModel(name='basicFormulation')
>>> m.I = pyo.Set(initialize=[1, 2, 3, 4, 5])
>>> m.v = pyo.Var(m.I)

>>> def ruleMaker(m): return sum(m.v[i] for i in m.I) <= 0

>>> m.constraint = pyo.Constraint(rule=ruleMaker)

>>> pstr = latex_printer(m.constraint)

Using a ComponentMap to Specify Names

>>> import pyomo.environ as pyo
>>> from pyomo.contrib.latex_printer import latex_printer
>>> from pyomo.common.collections.component_map import ComponentMap

>>> m = pyo.ConcreteModel(name='basicFormulation')
>>> m.I = pyo.Set(initialize=[1, 2, 3, 4, 5])
>>> m.v = pyo.Var(m.I)

>>> def ruleMaker(m):  return sum(m.v[i] for i in m.I) <= 0

>>> m.constraint = pyo.Constraint(rule=ruleMaker)

>>> lcm = ComponentMap()
>>> lcm[m.v] = 'x'
>>> lcm[m.I] = ['\\mathcal{A}',['j','k']]

>>> pstr = latex_printer(m.constraint, latex_component_map=lcm)

An Expression

>>> import pyomo.environ as pyo
>>> from pyomo.contrib.latex_printer import latex_printer

>>> m = pyo.ConcreteModel(name = 'basicFormulation')
>>> m.x = pyo.Var()
>>> m.y = pyo.Var()

>>> m.expression_1 = pyo.Expression(expr = m.x**2 + m.y**2)

>>> pstr = latex_printer(m.expression_1)

A Simple Expression

>>> import pyomo.environ as pyo
>>> from pyomo.contrib.latex_printer import latex_printer

>>> m = pyo.ConcreteModel(name = 'basicFormulation')
>>> m.x = pyo.Var()
>>> m.y = pyo.Var()

>>> pstr = latex_printer(m.x + m.y)