Thanks Glenn:
I apologize for my foolishness, but your button looked so similar to the superscript button that my eyes completely skipped over it when I was browsing. Your code works, even with the "DOMContentLoaded" event.
However, the subscript button just doesn't sit right with me. Since your solution was a fair hack, I decided to go full on and just modify /opt/webwork/webwork2/htdocs/js/apps/MathQuill/mqeditor.js in order to create a logarithm base b button. I have neither the time nor the programming chops to create a new LaTeX command for logb in MathQuill, so my compromise was to create a toggle in mqeditor.js which tells it to add a log button to the bar, and it creates a custom onClick event for that specific button in order to properly render a logarithm base b. Rather than post a file which may not be compatible with other versions of WeBWorK, here is the code in it that I changed.
<begin mqeditor.js - change code is underlined>
<snip>
answerQuill.buttons = [
{ id: 'frac', latex: '/', tooltip: 'fraction (/)', icon: '\\frac{\\text{\ \ }}{\\text{\ \ }}' },
{ id: 'abs', latex: '|', tooltip: 'absolute value (|)', icon: '|\\text{\ \ }|' },
{ id: 'sqrt', latex: '\\sqrt', tooltip: 'square root (sqrt)', icon: '\\sqrt{\\text{\ \ }}' },
{ id: 'nthroot', latex: '\\root', tooltip: 'nth root (root)', icon: '\\sqrt[\\text{\ \ }]{\\text{\ \ }}' },
{ id: 'exponent', latex: '^', tooltip: 'exponent (^)', icon: '\\text{\ \ }^\\text{\ \ }' },
{ id: 'infty', latex: '\\infty', tooltip: 'infinity (inf)', icon: '\\infty' },
{ id: 'pi', latex: '\\pi', tooltip: 'pi (pi)', icon: '\\pi' },
{ id: 'vert', latex: '\\vert', tooltip: 'such that (vert)', icon: '|' },
{ id: 'cup', latex: '\\cup', tooltip: 'union (union)', icon: '\\cup' },
// { id: 'leq', latex: '\\leq', tooltip: 'less than or equal (<=)', icon: '\\leq' },
// { id: 'geq', latex: '\\geq', tooltip: 'greater than or equal (>=)', icon: '\\geq' },
{ id: 'text', latex: '\\text', tooltip: 'text mode (")', icon: 'Tt' }
];
if(window.logb_button_toggle) {
answerQuill.buttons.splice(-5, 0, { id: 'logb', latex: '\\log_', tooltip: 'logarithm base b (log_)', icon: '\\log_{\\text{\ \ }}(\\text{\ \ })' }); }
<snip>
$(".symbol-button").on("click", function() {
answerQuill.hasFocus = true;
if(window.logb_button_toggle && this.getAttribute("id") === 'logb'+ "-" + answerQuill.attr('id')) {
answerQuill.mathField.cmd('\\log');
answerQuill.mathField.cmd('_');
answerQuill.mathField.keystroke('Right');
answerQuill.mathField.cmd('(');
answerQuill.mathField.keystroke('Left');
answerQuill.mathField.keystroke('Left'); }
else { answerQuill.mathField.cmd(this.getAttribute("data-latex")); }
answerQuill.textarea.focus();
});
<no more changes until end of file>
In addition to this change, I also added the following code to the bottom of parserLogb.pl so that the toggle will be triggered anytime the parser is loaded. This has the added advantage of not forcing me to update every problem using this solution if the method of loading MathQuill ever changes - I can just work with mqeditor.js and parserLogb.pl.
main::POST_HEADER_TEXT(main::MODES(TeX=>"", HTML=><<"END_SCRIPT"));
<script> window.logb_button_toggle = true; </script>
END_SCRIPT
In conclusion, I would appreciate a warning if I have just done something that will cause major problems :)