PyTeX v0.5.1 Documentation


Basic Usage

Installation

The source code and binaries for PyTeX are available for download at the following GitHub link. If you are running PyTeX in a binary (executable) make sure to download the version that corresponds for your system. Binaries are available for Macintosh computers for both the M1/M2 processors and for Intel based Macs. Binaries are also available for Windows PCs and Linux.

If you are downloading the source files and running from the Python scripts, please just make sure you have Python version 3 installed on your system as this is incompatible with version 2. In addtion, you may need at least version 3.11 as that is all this has been tested on. PyTeX relies on NumPy, SymPy and SciPy for calculation, so those libraries will need to be installed via pip prior to usage.

PyTeX is a command line program and your executable file should be saved in a directory that is on your system path so that it can be run from any folder. Type "echo $PATH$" to get a list of folders on your system path. If you are using source code, then placing a shell script in a folder in your path will be convenient. There is an example shell script for mac users in the /misc folder of the source code.

Adding a Version to a Document

In order to add a version to your document when running PyTeX, for instance if you want to make a version A of an exam, you can do so through the command line option "--version=A". You can name the version anything you like, but it is not advisable to inlude spaces.

In order for this to work, there has to be a line in your LaTeX document that declares \version as a new command: \newcommand{\version}{} PyTeX can find this line and insert the version value you have provided and then you can reference the \version command within your LaTeX code wherever you like and the version you specify will be supplied.

Making an answer Key

Sadly, PyTeX cannot automatically create an answer key for you, but it can at least show and hide answer key text through the use of a command line flag. If you would like to provide answer key text, then use the command line option "--key". If you do not want to include any answer key text, then just don't provide this flag.

In order fo this to work, you first need to make sure you have provided your LaTeX document the ability to use boolean variables. This is done by including the package: \usepackage{ifthen} Then you must have a variable named make_key declared such as: \newboolean{make_key} \setboolean{make_key}{false} If you supply the command line option "--key" then PyTeX will find this line and set the value of make_key to true. In addition, I define the following commands: \newcommand{\iskey}[1]{\ifthenelse{\boolean{make_key}}{{\color{red}#1}}{}} \newcommand{\iskeyelse}[2]{\ifthenelse{\boolean{make_key}}{{\color{red}#1}}{#2}} These are not necessary, but make it very convenient to insert text meant for an answer key when requested and leaving it hidden otherwise.

Working with SymPy

If you are creating questions for algebra classes, then you will very likely have the need to use SymPy to assist. The SymPy documentation page is going to be an invaluable resource to help you along. The SymPy library is already loaded into PyTeX for your use, and all functions can be accessed by using sp.function_name, but the following have been imported directly:

In addition to SymPy, NumPy has already been imported as np, so it's functions may be called as necessary.


Randomization Functions

Array Functions

String Functions

Math Functions

Most math functions are provided by Python's built-in math module, and the documentation provided at docs.python.org will provide the most complete reference for these functions.

Statistical Functions

Display Functions

Python Built-in Functions

The list of built-in functions available at docs.python.org may prove helpful and are available when using pytex.

Detailed Documentation

seed(value) => void

Uses value as the initial value for the internal randomization process. This would generally be defined at the start of a document and would be useful in making different versions of a document to ensure that the randomized values would stay the same for repeated creations. For instance, if you have a document that can produce an exam and a key, then you would need to ensure that when you create the exam and it's key that all the randomized values would be consistent from one run of pytex to the next. This would be the case if the randomization seed has been set at the start of the document.

rand(min, max, [prec])

Choose a random value between the input parameters min, and max, inclusive. If the optional paramter prec is provided, then a random value to a specified number of decimal places will be used. The precision is provided as a power of 10 such as: 1, 0.1, 0.01, 0.001, etc.

e.g. rand(20.5, 30.8, 0.1) might return 25.2 and rand(15, 25) might return 18

rands(min, max, n, [prec])

Chooses a list of n random values between the specified min, and max values. In addition, if a value for the optional parameter prec is provided, then random values to the specified number of decimals will be chosen. The value of the precision is indicated as a power of 10 such as: 1, 0.1, 0.01, 0.001, etc.

diffrands(min, max, n, [prec])

Selects a set of n random values between min and max, inclusive and ensures that none of the values are equal. If the optioanl parameter prec is provided, then random values to a specified number of decimals will be chosen. The value of the precision is indicated with a power of 10 such as: 1, 0.1, 0.01, 0.001, etc.

nzrand(min, max, [prec])

Choose a random value between the input parameters min, and max, inclusive, but will ensure that the selected value is not zero. If the optional paramter prec is provided, then a random value to a specified number of decimal places will be used. The precision is provided as a power of 10 such as: 1, 0.1, 0.01, 0.001, etc.

nzrands(min, max, n, [prec])

Chooses a list of n random values between the specified min, and max values, but will ensure that none of the selected values will be zero. In addition, if a value for the optional parameter prec is provided, then random values to the specified number of decimals will be chosen. The value of the precision is indicated as a power of 10 such as: 1, 0.1, 0.01, 0.001, etc.

nzdiffrands(min, max, n, [prec])

Selects a set of n random values between min and max, inclusive and ensures that none of the values are equal or zero. If the optioanl parameter prec is provided, then random values to a specified number of decimals will be chosen. The value of the precision is indicated with a power of 10 such as: 1, 0.1, 0.01, 0.001, etc.

randfrom(values)

Selects a random value from a list of items. The list can be an array, or a comma separated string, and it will choose one of the available items. The advantage here is that you can get random strings, numbers, or any other type of value that exists within your list to choose from.

randsfrom(values, n)

Selects n random values from a list of items. The list can be an array, or a comma separated string, and it will choose from the available items.

diffrandsfrom(values, n)

Selects n random values from a list of items, but this function will ensure that the selected items are unique. The list to select from can be an array, or a comma separated string, and it will choose among the available items.

singleshuffle(array)

Repositions the items within an array in a randomized order. All the items will be preserved, only the position of the items will be changed.

jointshuffle(array1, array2)

Shuffles the items within two lists, but whichever values correspond in array1 and array2 originally will still correspond after the shuffling is complete. In effect, only array1 is shuffled, and the corresponding elements in array2 will follow accordingly.

randsgn()

Randomly chooses positive or negative one to use as a multiplier. This makes it easier to choose a random value that can be positive or negative, but must skip a certain amount.

e.g. z = randsgn() * rand(1.5, 2.4, 0.1) will choose a random number for a normal distribution that is at least 1.5 standard deviations from the mean (in either direction), but no further than 2.4.

gcd(values)

Calculates the greatest common divisor for all of the input values. This can be any number of values, separated by commas.

for example, gcd(12, 18) would return 3, and gcd(4, 12, 22) would return 2

reducefraction(numerator, denominator)

Reduces a fraction to its lowest terms. The return is an array with two elements, the first of which is the numerator of the reduced fraction, and the second is the denominator of the reduced fraction.

sgn(value)

Checks to see if the input value is positive or negative and returns +1 or -1 respectively.

sign(value)

Similar to sgn(x), except that a string is returned rather than a float value. This can be useful when typesetting randomized polynomials in order to ensure the display of the polynomial is nicer.
For instance, if a = -5, then "y=2x+a" will display as: "y=2x+-5", but if you use "y=2x sign(a)" then it will display as "y=2x-5".

frac(value)

Returns the fractional (decimal) portion of a float.

e.g. frac(21.51825) returns 0.51825

abs(value)

Returns the absolute value of a floating point number or an integer. For whatever the input value is, the return is an equivalent number in magnitude, but is always positive.

round(value, [decimals = 0])

Rounds a floating point number to a specified number of decimals. If no decimals are provided, the default is to round to an integer. This function currently uses Python's method of rounding which is called rounding half-even. So 0.5 on an even number will round down, and 0.5 on an odd value will round up. Since this is so different from student expectations, I will eventually override this function for my own that always round 0.5 up.

ceil(value)

Removes the decimal portion of a floating point number and then returns the next integer value. If an integer value is provided, then it is simply returned unchanged.

floor(value)

Removes the decimal portion of a floating point number and returns the integer portion. If an integer is provided, then it is returned unchanged.

evalfunc(f, variables)

Takes a function expressed in as a string and evaluates that function to a number. If any variables are contained in the expression, the values must be specified in a dict object so that the values can be substituted.

e.g. evalfunc("2*x**2 - 4*x+15", {'x': -1}) returns 21

showdataarray(array, [columns=1], [options=""])

Creates a rectangular table to display numeric or string values. The table is simple, it is a grid with a specified number of columns and however many rows are needed to display all the data. Currently, the data is fully centered and there are borders drawn around all values.

(title1, array1, title2, array2, etc...)

Creates a table for multiple arrays arranged vertically. The title string is created in a bold font and all the data within its corresponding array will appear below. Additional columns will be created for any additional title/array pairs.

(title1, array1, title2, array2, etc...)

Creates a table for multiple arrays arranged horizontally. The title string is created in a bold font and all the data within its corresponding array will appear to the right. Additional rows are created for any additional title/array pairs.

(leftbound, rightbound, [mean=0], [stdev=1], [twotail=False])

Creates a plot for a normal distribution curve that can be shaded between the values of leftbound, and rightbound. If twotail is set to true, then the regions to the left of leftbound and to the right of rightbound will be shaded. A value for the mean and stdev for the distribution can be provided and the drawing will shift accordingly. If you only want the normal distribuion curve to be drawn, you can just set the left and right bounds to both be greater than +4 because any value more than 4 standard deviations from the mean are ignored.