Difference between revisions of "Using TeX Notation"
Line 1:  Line 1:  
{{Filters}}  {{Filters}}  
+  {{Work in progress}}  
TeX (pronounced TEK) is a very widespread and popular way of representing Mathematics notation using only characters that you can type on a keyboard (see [https://en.wikipedia.org/wiki/TeX Wikipedia]). This makes it a useful format to use in Moodle, since it can be entered anywhere you can type text, from forum posts to quiz questions.  TeX (pronounced TEK) is a very widespread and popular way of representing Mathematics notation using only characters that you can type on a keyboard (see [https://en.wikipedia.org/wiki/TeX Wikipedia]). This makes it a useful format to use in Moodle, since it can be entered anywhere you can type text, from forum posts to quiz questions.  
Revision as of 05:01, 18 May 2014
Note: This page is a workinprogress. Feedback and suggested improvements are welcome. Please join the discussion on moodle.org or use the page comments.
TeX (pronounced TEK) is a very widespread and popular way of representing Mathematics notation using only characters that you can type on a keyboard (see Wikipedia). This makes it a useful format to use in Moodle, since it can be entered anywhere you can type text, from forum posts to quiz questions.
TeX expressions can be entered in multiple ways:
 typing them directly into texts.
 using the Javabased Dragmath editor in Moodle's TinyMCE editor.
 using the HTMLbased equation editor in Moodle's Atto editor (since Moodle 2.7).
Afterwards, TeX expressions are rendered into Mathematics notation:
 using the TeX filter in Moodle, which uses a TeX binary installed on the server to convert expressions into .gif images (or if that is not available, it falls back to a simple builtin mimetex binary).
 using the MathJax_filter] which identifies TeX expressions and uses the Mathjax JS library to render them in browsers at display time (since Moodle 2.7).
 using other thirdparty solutions.
As you can imagine, the whole field is not simple as we'd like, especially because there are many flavours of TeX and slight variations between tools.
This page focusses only on using TeX in core Moodle. See the links at the bottom of this page for more information on other tools from the Moodle community that may be suitable for advanced users.
Contents
Using TeX Notation with the Moodle Tex filter
For the most part, the TeX Notation has been built using a subset of characters from the TeX "default" character set. The trouble is there does not seem to be a "default" character set for TeX. This is one of the most confusing aspects of using TeX Notation in Moodle. When we realise that the documentation we are using is related to the creation of printed documents, and we want to use TeX on line, in Moodle, then further problems occur. There are no environment statements to be made. There are few \begins and \ends. If you go to Administration > Modules > Filters > Filter Manager you will see what filters have been enabled. If you then go to the TeX Notation page, the default preamble is editable via the text box. Using this tool you can add in or subtract font packages and other packages, change the default font package, etc.
Language Conventions
To invoke the TeX filter, use the $$ symbols to open and close statements. To invoke a particular command or control sequence, use the backslash, \. A typical control sequence looks like:
$$ x\ =\ \frac{\sqrt{144}}{2}\ \times\ (y\ +\ 12) $$
Fraction and square root. 
Additional spaces are placed into the equation by using the \ without a trailing character. Escape characters, of which there are a few in Moodle's TeX Notation, have the \ in front of them. These are usually set aside for reserved characters.
NOTE:it also appears that different Moodles will produce different results with regard to spacing so it may require a bit of trial and error to get right.
Available Characters
There seems to be a number of differences between what characters are available in Tex Notation and what are not and what is described in Mimetex. There also seems to be great discrepancies between what the Tex and Latex manuals tell you is available and what is actually available. If you are using such manuals or web sites, they are likely to be more confusing than helpful. Using TeX Notation 2 shows a lot of what is available, but not all.
Windows and TeX
Using TeX in Windows is simpler than it used to be. Download and install a TeX for Windows program, like MikTeX. (MikTeX is probably the most useful at this point in time for Windows Users.) Also, a graphics package is required for rendering the scripted TeX statements into images, and probably the most readily available and easily installable program useful for this purpose would be ImageMagick. While there may be better programs available for these purposes, these are the most immediately useful.
Recommendation: The programs are easily installed and configured however, be aware, that for XP you can use the Program files folder to install the graphics program, but not the textmf folder, it should be installed as a stand alone folder. It has been noted that in Windows Vista or Windows 7 you should never use system folders for latex distributions, GhostScript or Imagemagick.
Known Issue
The introduction of PHP 5.3 caused some TeX rendering to break in Moodle. The flaw was traced back to the way deprecated functions were reported. Moodle Tracker responded with this:
In file filter/tex/pix.php line 29:
error_reporting(E_ALL);
corrupts TeX images with php 5.3.X and there are at least 2 easy ways to fix it:
1) To totally remove line 29 and error reporting from that file
2) To change line 29 to:
error_reporting(E_ALL & ~E_DEPRECATED);
so that php 5.3.X does not show the deprecated functions warnings that otherwise would prevent showing of TeX images.
Reserved Characters and Keywords
Most characters and numbers on the keyboard can be used at their default value. As with any computing language, though, there are a set of reserved characters and keywords that are used by the program for its own purposes. TeX Notation is no different, but it does have a very small set of Reserved Characters. This will not be a complete list of reserved characters, but some of these are:
@ # $ % ^ & * ( ) .
To use these characters in an equation just place the \ in front of them \$ or \%. If you want to use the backslash, just use \backslash. The only exception here seems to be the &, ampersand. See the characters listed in Using TeX Notation 2 for more details.
The Keywords, they are different. There is only one that is of concern here and that it the word "line". If the \line control sequence, or command, is not properly implemented, then the consequences can be indeterminate. Controlling lines is an adventure of its own, so getting it right when written is important, you can always reposition the line, but you might get it really wrong if you do not use it with some caution.
To use the \line control sequence, go to your text editor, open the filter\lib.php file. In this file look for the array $tex_blacklist = array(, and in the list of words that follow, you will see the word "\line". Delete the word, with its enclosing single quote marks and trailing comma, from the list.
If you do not have direct access to the server, then the whole thing becomes problematic. You may want to download and install your own Moodle and develop things there rather than on your production site. You can write things out in your own Moodle, render the statement, save the image and then upload it to your production site. If something goes seriously wrong, it is easier to restart your own Moodle than it is your production site.
WARNING: If you get a dimension wrong on a line, it can actually prevent you from creating any new TeX. You will see the offending control sequence in its natural state. Everything that you have written to that point will work, but nothing you have written after that will.
Maths Mode
A full TeX version has three modes, a "text mode", an "inline maths mode" and a "maths display mode" but Moodle seems to stay in "inline maths mode". Perhaps a better description of what happens is that Moodle allows a writer to enter "inline maths mode" when the $$ symbols are written and leave it when the $$ symbol appears a second time. Moodle does not appear to use the "maths display mode". The command sequences beginning with the $$ are not current practice in any major version of TeX, and why they are used and work in Moodle is not an issue for discussion here. Current common practice in most other flavours of TeX uses a different set of command initiation sequences.
Superscripts, Subscripts and Roots
Superscripts are recorded using the caret, ^, symbol. An example for a Maths class might be:
$$ 4^2 \ \times \ 4^3 \ = 4^5 $$
This is a shorthand way of saying:
(4 x 4) x (4 x 4 x 4) = (4 x 4 x 4 x 4 x 4)
or
16 x 64 = 1024.
Subscripts are similar, but use the underscore character.
$$ 3x_2 \ \times \ 2x_3 $$
This is OK if you want superscripts or subscripts, but square roots are a little different. This uses a control sequence.
$$ \sqrt{64} \ = \ 8 $$
You can also take this a little further, but adding in a control character. You may ask a question like:
$$ If \ \sqrt[n]{1024} \ = \ 4, \ what \ is \ the \ value \ of \ n? $$
Using these different commands allows you to develop equations like:
$$ The \sqrt{64} \ \times \ 2 \ \times \ 4^3 \ = \ 1024 $$
Superscripts, Subscripts and roots can also be noted in Matrices.
Fractions
Fractions in TeX are actually simple, as long as you remember the rules.
$$ \frac{numerator}{denominator} $$ which produces .
This can be given as:
.
This is entered as:
$$ \frac{5}{10} \ is \ equal \ to \ \frac{1}{2}.$$
With fractions (as with other commands) the curly brackets can be nested so that for example you can implement negative exponents in fractions. As you can see,
$$\frac {5^{2}}{3}$$ will produce
$$\left(\frac{3}{4}\right)^{3}$$ will produce and
$$\frac{3}{4^{3}}$$ will produce
You likely do not want to use $$\frac{3}{4}^{3}$$ as it produces
You can also use fractions and negative exponents in Matrices.
Brackets
As students advance through Maths, they come into contact with brackets. Algebraic notation depends heavily on brackets. The usual keyboard values of ( and ) are useful, for example:
This is written as:
$$ d = 2 \ \times \ (4 \  \ j) $$
Usually, these brackets are enough for most formulae but they will not be in some circumstances. Consider this:
Is OK, but try it this way:
This can be achieved by:
$$ 4x^3 \ + \ \left(x \ + \ \frac{42}{1 + x^4}\right) $$
A simple change using the \left( and \right) symbols instead. Note the actual bracket is both named and presented. Brackets are almost essential in Matrices.
Ellipsis
The Ellipsis is a simple code:
Written like:
$$ x_1, \ x_2, \ \ldots, \ x_n $$
A more practical application could be:
Question:
"Add together all the numbers from 1 38.
What is an elegant and simple solution to this problem?
Can you create an algebraic function to explain this solution?
Will your solution work for all numbers?"
Answer: The question uses an even number to demonstrate a mathematical process and generate an algebraic formula.
Part 1:  Part 2.  Part 3. 

An algebraic function might read something like:
Where t = total and n = the last number. 
The solution is that, using the largest and the smallest numbers, the numbers are added and then multiplied by the number of different combinations to produce the same result adding the first and last numbers.
The answer must depend on the number, being a whole number. Therefore, the solution will not work for an odd range of numbers, only an even range. 
See Also
 Using TeX Notation 2
 TeX notation filter To turn on the TeX Notation
 Advanced Maths Tools Moodle 2.x  The Next Generation of TeX Tools
 DragMath equation editor