CL Newsletter, February 2023 - formatLatex and substituteLatexVariable

Hey there, activity builders!

Sometimes we get a new tool so powerful that we have to take some time to really learn about it before we can share it with you. This month, we’ll talk about two not-so-recent features that will not only make your life easier, but also make so many things that used to just be dreams (or unreliable hacks!) possible. We want to jump right in, so in lieu of a wordy introduction, here’s a two gif to pique your interest:


We’ll start with the simpler of the two tools. formatLatex does exactly what it says. It helps you format your latex strings by simplifying and removing the unnecessary bits. Take this movable parabola for example:

When the one of the coordinates of the vertex is 0 or the a is 1 or -1, your equation looks cluttered and confusing without formatting.

Before formatLatex, you’d need to write a bunch of code needed in order to display the equation clearly.

Now all you have to do is apply the function to your latex string — and voila! Your equation gets displayed in its most concise form.

Try it yourself! One thing to note is that formatLatex won’t do complete simplifications, meaning it won’t simplify things that might be there by choice (such as uncombined like terms).

Try It!


The original purpose of the substituteLatexVariable function was to show the substitution of a variable with a numeric value. For example, by replacing the x’s in the expression below with “(3)”

This function requires three arguments:

  1. The latex string you want to work on
  2. The variable you want to substitute
  3. The string you want to substitute it with
    When applied to the example above, the code looks something like this:
    where the string we’re working with is 3x^{2}+5x-2, the variable we’re looking for is x, and the numeric value we’re replacing it with is (3).

This function opens up possibilities for replacing not only standard English letters, but also a selection of special characters and functions, with virtually any string. This allows us to do all sorts of things, like:

  • Erasing characters.
  • Recoloring/reformatting.
  • Substituting in mathematical expressions.
    We can even nest this function to convert words to numbers.

Or we can combine the function with countNumberUsage to count the number of times a variable appears in a string.

You can play with all of these examples here.

Using the formatLatex and substituteLatexVariable functions in tandem lets us modify expressions to our liking, clean them up, and display them neatly. They’re really handy tools for anyone wanting to generate random problems or create reusable modules to interpret student work in various forms (and with different variables).