Algebra question type

From MoodleDocs

The algebra question type allows algebraic expressions for student answers which are evaluated by instructor provided answers using the basic rules of algebra. It was created by Roger Moore and first offered as contributed code in July 2009. It is currently maintained by Jean-Michel Védrine


This is a question type for Moodle.

It implements an algebraic question type where student responses are treated as an algebraic expression and compared to instructor provided answers using the basic rules of algebra.

For example, if the instructor provided response is , then a student entering will have their answer treated as correct.

The code has been tested in production use with ~230 first year physics students without problems (using the Evaluation comparison method only) - mainly thanks to some excellent feedback and testing from Moodle users on the quiz forum before deployment!


For the display to work you need to have some way of displaying TeX expressions activated on your Moodle website: either the TeX filter or the MathJax filter enabled.

Note that the new method to display a formatted answer formula "Using dynamic AJAX request" only works with the MathJax filter.

Question Formulation

To create a new Algebra question, go to the Question Bank and click on "Create a new question ..."

Some fields (Question name, Question text, Default Mark, General Feedback) on the question edit page are common to all question types so they don't need special explanation.

But some other are specific to the Algebra question type.

Expand the Options section.

Comparison Algorithm

First you define which comparison method is used to determinate if the student answer is correct or not and grade it, three method are provided:

  1. Evaluation
  2. Sage
  3. Equivalence


This is the default and best tested method. It's also the easier to understand. It "cheats" by evaluating both the answer and the student response for various random combinations of the variable's values and ensures that both match to within tolerance at all points.

You can specify the number of random values that will be generated for the test (more is better but also slower !) in the Number of Evaluation Checks field (default: 10) and the tolerance in the Tolerance for Evaluation Checks field (default 0.001).

If you plan to use this method, you must understand that it is not bulletproof and maybe not what you want.

As long as the student entered answer and the teacher provided answer take the same values for all random values of the variables generated by the plugin, the student answer will be considered as correct. For instance 2*x and x+x are considered as identical.


This method requires the external, open source Sage symbolic algebra system to be installed on a server. A simple XMLRPC server script in Python is included in the package,, and this provides a method to symbolically compare two expressions.

From a theorical point of view this method is surely the best one. But it's also the uneasiest one because it require an external server and depending on the way your Moodle server is hosted this may or may not be possible.

If you absolutely need that sort of mathematic question and answer verification, it is suggested that you look at a more modern, more secure and more powerful question type, the STACK question type


This uses the internal PHP algebra parser included in the parser to compare the two expressions. It was intended for use in "expand..." and "factor..." type questions where a full symbollic comparison would allow the student to enter the unexpanded (or unfactorized) expression.

The usage of this comparison method has received few testing and no users of the Moodle Quiz forum reported using it. So it is suggested to be very cautious if you plan to use it on a production server.

As a general rule it is recommended that you stick with the Evaluation comparison method unless very special need and that you know what you are doing.

Answer box prefix

If you want some text displayed before the answer input box, you can type it here. For instance in a question about functions you can type f(x) = so that the student knows what to answer. See an example on one of the screenshots bellow.

Disallowed Answer

You can type here a comma separated list of words that will be forbidden in student's answers. For instance if you type here pi, max students will not receive any grade for answer including one of these words.

Allowed Functions

Here you can choose what functions will be allowed (default: All) in student answer.


All Algebra question must include at least one variable and all defined variable must be used in at least one answer.

You must provide a variable name and if you use the Evaluation method a Minimum and Maximum value (SAGE and Equivalence methods don't use these values they are only used by the Evaluation method)

The variable name must be typed using linear notation not TeX syntax. If the variable name has more than one character, the rest is used as a subscript so the variable defined as xp will be displayed as , and x1 will be displayed as

You can also use Greek names as variables names with or without a subscript, theta1 for instance will be displayed as


This is where you defines the answer and their matching grade. Again, you shall use linear syntax notation.

You may choose to give different answers with different values for the grade, but it is required that an answer with the highest grade is included.

Answer can only use varables names defined in the Variables section of the form and all defined variables must be used by at least one answer.


We create a new Algebra question entering these values in the General section

edit algebra question 1.png

The formulas in the Question text and General feedback were typed using The Equation editor and will be latter rendered by the MathJax or TeX filter.

We now expand the Options section and enter the following values (most of them are default)

edit algebra question 2.png

We now create one variable named x that will take values between -5 and 5 when the Evaluation comparison method will be used to grade student answers

edit algebra question 3.png

And finally we create 2 answers the first one is for the correct answer and the second one for students that forgot the 2 and we choose to give them some little reward anyway :-)

edit algebra question 4.png

The student answer does not have to be exactly this, only shall be evaluated to (very similar) values. For instance, the equivalent answer x + x would evaluate correctly and gives full mark.

The question is now finished we can save it and test it

If you use the old "Using an iframe" setting to display formula it will look like this:

algebra question test.png

And if you use the new "Using dynamic AJAX request" setting to display formula, it will look like this:

qtype algebra attempt.png

Current Features

  • The students enter responses in a text box, like calculated and numeric questions. They use the linear syntax notation. They don't need to know the TeX syntax and they must not use the equation editor.
  • The formula they entered is rendered in the box below by clicking on the Display response button.
  • Option of three different comparison methods: SAGE, Evaluation or Equivalence.
  • Support for multiple answers so partial credit or assistance can be provided to students.
  • Except for the SAGE comparison method core functionality works entirely within Moodle, so it can be used without installing any other program.
  • Works with Moodle 2.8 and up to Moodle 3.4.

New Features Coming Soon

The original documentation listed here some new features that Roger Moore planned to add. But this was never done so they have been removed. If you ave any idea on how to improve this question type, please create an issue in the tracker.

Using Algebra in combined questions

Since version 1.9 it is possible to include Algebra questions as parts of a combined question. You will need to install the combined question type. The placeholder for the algebra subquestion in the combined question text follow the


pattern where <identifier> may be alphanumeric up to 8 characters and <type> is algebra. After you press the "Verify the question text and update the form" button, you will be able to edit the details of the algebra subquestion. There are some limitations from a standalone algebra question:

  • There is no Answer box prefix because you can write what you want in the combined question text before the algebra subquestion.
  • Allowed Functions is always "All" as this feature was never implemented
  • Each answer grade is always 100% and there is no answer feedback, only a global feedback for the subquestion when the student response is not correct

In the mobile app

Since version 1.9 the Algebra question type includes code for the Mobile App. If you access a quiz with the mobile app that contains Algebra questions it will be automatically loaded as a remote addon. Unfortunately currently there is only a field to enter the response, and no display of the result as a LaTeX formula. This is only working with Moodle 3.5 and ulterior versions.

Installing this question type

The Algebra question is a Moodle plugin, so you or your Administrator must install it.

Installing from the Moodle Plugins directory (this is the recommended and easiest method)

The Algebra question type is available from the Moodle Plugins drectory. Install the Algebra question type as any other Moodle question type plugin.

Installing from GitHub

The Algebra question is available at the maintainer's Github repository. To install the question, type the following commands in the root of your Moodle install:

  git clone git:// question/type/algebra 
  echo '/question/type/algebra/' >> .git/info/exclude

Installing from a zip file

Download the zip file from GitHub. Unzip the file in the 'question/type' folder and then rename the new folder to just 'algebra'.

During installation you will be prompted for new Administrator settings algebra settings.png

It is suggested that you don't change the Default comparison method (Teachers can change it on a per question basis if they want and an Administrator can change the default later)

You can ignore the 3 settings related to SAGE server unless you plan to use the SAGE comparison method and you know what you do.

According to the way LaTeX and TeX formulas are displayed on your website it may be necessary to change the delimiters for TeX expressions from the default $$ ... $$ to \[ ...\] or $ ... $ or \( ... \). This setting is only used used to display the formatted answer on the question view during attempt, and also to display the formatted correct answer after the question is submitted. It is not used to display formulas in question text, General feedback and everywhere else

The TeX operator for multiplication setting control the symbol used to display multiplication either times or cdot .

NB: If you wish to test the current Sage functionality you will also need to install Sage on a server as well as run the python server code with the command "sage - python".


  • Failed to load question options from the table qtype_algebra_options for questionid <n>: this usually occurs because you tried to add an algebra question into Moodle without having propertly installed the algebra plugin, so the tables for the question type have not been added to the database. Any existing algebra questions that were added (via editing or import) before this is corrected will need to be deleted since they are missing important information and are broken.

Linear syntax

To use this question type, you must understand that there are 2 very different syntax that are used.

When creating the question, when you type something into the different editors on the question edit page (Question text, General feedback, ...), you do as usual, if you want to enter some mathematical formula, you use the equation editor or you type some TeX formulas as you would do in a forum post, a lesson or a description.

But when you enter a formula in the Answer 1, Answer 2, ... fields the situation is quite different . These formulas must be parsed by the formula question code so a special syntax is used here and TeX syntax must not be used here.,

The algebra question type uses what is called a linear syntax to input math formulas : *, +, -, /, /, (, [, ], ), pi, e, and the known functions sqrt, log, ln, sin, cos, tan, cosh, sinh, asin, acos, atan are used.

Variables can be uppercase and lowercase letters, Greek letters are recognized and subscripts too, for instance if you type u1 it will be interpreted as and alpha3 will be recognized as

Obviously e and pi can't be variables names as they are already constants but they can be used in answer formulas.

Currently a bug in the parser prevent the use of the Greek letter epsilon as a variable name because the first letter is confused with the e constant.

Some examples:

  • 2 x (equivalent to 2*x)
  • e^x
  • 2*pi*x
  • cos(x)+sin(y)
  • sqrt(b^2 - 4 a c)
  • alpha^3-beta^3

When they attempt the Algebra question, your students will need to use the same linear syntax. Don't be afraid of that, they are already using it when they are talking about math and exchanging answers with their phones ;-)

See also