TeX & (C or JavaScript)


TeX & (C or JS) is the most commonly used question type to introduce randomization in variables. It can take the form of Numeric Free Response (NFR, also called TeX Open, template #179792) or TeX multiple choice (template #179793). These TeX problems are useful to introduce variation of numbers within questions so students in a large class each see the same question, but with different numbers.


TeX/Latex

TeX formatting is a powerful tool for creating content in Quest.  The upper TeX box serves as a display of the question. Write this code in TeX (and sometimes LaTeX). A standard TeX reference card can be found here, commonly used functions available here, and more tools for finding the appropriate commands for various symbols can be found here: Detexify (external site)

To make a comment in the TeX box (ie leave a note that will not be run or acted upon), use a single % at the front of the line (two examples below).

%%%%%%%%%%%% quest 1
    % the above line denotes part 1 of a question


    % The question text goes here

Javascript (JS) or C

The lower code box (written in JS or C) performs calculations and initialize variables that are used in the top TeX portion. Each question part in the question body must define and initialize an answer in the code portion--it is in this code box where variable, any randomization, and the answer (ans1) are set. (Follow these steps to add more than one part.)

As the name indicates, this question type requires either Javascript (JS) or C in the code portion of questions.  No TeX is allowed in the code portion of a TeX question, especially in the definition of answer units. To avoid errors, spell out units that require more complicated symbols, like degrees, percents, etc. 

Coding Javascript/C advice is:

  • All variables must be defined prior to the body of the code.
  • Any variable defined in the top of the code, must be set to a value to prevent an error in compilation.
  • All variables must be unique, while there is no error, the compiler cannot distinguish based on case. Ex. A1 and a1 would be interchangeable when compiled and could cause errors in the question.
  • 'y1' and 'j1' are not allowed to be used as variable names due to the layered nature of the system. If you receive an error saying "error: '[name of variable]' redeclared as different kind of symbol" simply change the name of that variable.
  • The correct answer of each part of the question is called as ans#, i.e. the answer to part 1 of a question would be ans1, part 2 would be ans2, etc.
    • These answer variables cannot support TeX formatting in the units designation, but all other variables can.


A Javascript question will be defined like a Javascript function: function js_answer() { }. All code you write in this section must be contained in the function brackets. When using Javascript in the code portion of a TeX question, take special notice of semicolons and matching opening and closing braces {}. Like any Javascript code, each statement must end in a semicolon and all functions, loops, and conditional statement blocks must begin and end in curly braces. If you want to learn a little more about Javascript in general, try this link.

For more details about using Javascript, use question ID 169286 as a template to create a reference question in your bank.

Javascript Example

function js_answer(){
  /* global double ans1 u={ [specify units here] } */
  ans1 = 1;
}


When using items as templates, you may come across items written in C. They will have the primary function declaration as follows:

void answer(void){
  /* global double ans1 u={ [specify units here] } */
  ans1 = 1;
}

We're attempting to convert all these items to Javascript, so if you feel comfortable, please do so in your edits. If you don't feel comfortable, simply shoot us an email (questhelp@austin.utexas.edu) to let us know and one of our staff will assist you in that process.


To make a note in the code box for Javascript (JS), use // 

example:

function js_answer() {

    /* global double ans1 u={} */

    ans1 = 3;   // the third answer choice

}


here "// the third answer choice" is referring to ans1=3 means the correct answer will be the third answer choice in the multiple choice above.

C

When writing the coding portion of the question in C, begin with "void answer(void) { }" and insert all code between the curly braces. Any variables you want to reference in the TeX portion of the problem must be defined as follows: /* global (insert type) (insert name) */ (without the parentheses). You can also specify units you want to be displayed after the variable by typing "u={type}" where type can be m, kg, etc. The rest of the problem can then be coded like a normal C program.

What about that 'execute' button (at the top of the code box)?

Pushing this button will run the code in the code block, and print the stuff in printjs(<stuff>)​; the purpose would be to generate a display of what the code box is telling it to do, for debugging purposes. 


Using variables (@)

Variables that have been declared and given a value in the code portion (see below) can be referenced in the question body with a preceding "@" symbol. For instance, if you calculated the variable k in the code portion and wanted to display the square root of k in an equation in the question body, you would simply type $\sqrt{@k}$. If k has been given a value, the editor will grab this value from the code portion and display it in the question body. Make sure you have initialized the variable in the code portion or you will receive an error message, and the question will not work.


Please follow these links for more information to create specific numeric free response (aka TeX open) or multiple choice questions.


For help beyond this brief intro, StackExchange is a very useful site for finding advice on a wide variety of subjects, most relevant here, is the communities about TeX/LaTeX, JavaScript and C. A quick internet search should yield a variety of forums to help.


Formatting and troubleshooting tends to be the main time sink of these problems–please do call for help if you get frustrated or want backup: questhelp@austin.utexas.edu