%!TEX TS-program = sage
\documentclass{article}
\title{MTK Basic Test 03 - Units with Pint}
\author{J. Simmons}

\usepackage{sagetex}
\setlength{\sagetexindent}{5ex}

\begin{document}
\maketitle

% start content here
\section{Test Units with Pint}
This section tests using the Pint library to implement units in MTK.  The following analysis computes the area of a rectangle.

\begin{sageblock}
from pint import UnitRegistry
units = UnitRegistry()
units.default_format = '~L'

length = 2.5 * units.meter
width = 2.0 * units.meter

area = length * width

force = 20 * units.newton
pressure = force / area

def displayFloat(x, formatString):
  return displayFloatMagnitude(x, formatString) + ' ' + displayUnits(x)
  
def displayFloatMagnitude(x, formatString):
  value = x.magnitude
  return '$' + formatString.format(float(x.magnitude)) + '$'
  
def displayUnits(x):
  pintStringElements = '{:}'.format(x).split(' ')
  return '$' + pintStringElements[1] + '$'
  
\end{sageblock}

The area of a rectangle of length \sagestr{displayFloat(length, '{0:.2f}')} and width \sagestr{displayFloat(width, '{0:.2f}')} is \sagestr{displayFloat(area, '{0:.2f}')}.  For a force of \sagestr{displayFloat(force, '{0:.2f}')} the pressure on the rectangle is \sagestr{displayFloat(pressure.to(units.pascal), '{0:.2f}')}.

\section*{}
This document is a work of Mach 30 and is licensed under the Creative Commons Attribution 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/.

\end{document}