Lesson goal: Displaying math

Previous: Displaying text | Home | Next: How programs flow

Math on the web can be pretty ugly. Looking at things like 3/2x^2+5/sin(x) makes us cringe. We'd much rather see $\frac{3}{2}x^2+\frac{5}{\sin(x)}$ instead.

We've developed a function called print_math that will take an "ugly" mathematical expression and make it look nice.

print_math("ugly expression")
Move the mouse over a dotted box for more information.

Now you try. Try programming in some expressions, and make them look nice.

Type your code here:

See your results here:

This example will nicely print x^2 x^3 like $x^2x^3$.

Other things to try:

  • Grouping exponents. Do x^{23} not x^23 to get $x^{23}$.
  • Fractions. Do %frac{numerator}{denominator} to get nice fractions. So, %frac{a+b}{x^2-1} will render $\frac{a+b}{x^2-1}$.
  • Square roots. Do %sqrt{radicand} to get a square root sign. So %sqrt{x^2+1} will render $\sqrt{x^2+1}$.
Show a friend, family member, or teacher what you've done!

Here is a share link to your code:

Does your code work? Want to run it on your iPhone?

Here's your code:

  1. Use [Control]-[C] (Windows) or [⌘]-[C] (MacOS) to copy your code.

  2. Paste it using [Control]-[V] (Windows) or [⌘]-[V] (MacOS) into this page

  3. Then click the "Use on iPhone" button that you'll see.