Canvas and Mastery are experiencing issues due to an ongoing AWS incident. Follow the status at AWS Health Dashboard and Instructure Status Page
I'm a newbie to Canvas. I have found the equation editor, which works fine. But I have pages with many equations. Is there a way to include the LaTeX code in a page, and then have Canvas automatically render the equation? Thanks - this would be a big time saver.
Solved! Go to Solution.
I also had trouble with adding equations to Canvas pages. Even if you use the equations editor, there is no easy way of adding equation numbers or references to equations. To workaround this I made a LaTeX to plain HTML converter. You put in a LaTeX article and it spits out basic HTML that can be read by the HTML editor of a Canvas page. I'll add a picture of how it can look and if you want to check out the converter visit GitHub.
For folks like me who get lost when the first step is "install Python," there's an online .tex-->HTML converter at https://www.vertopal.com/en/convert/tex-to-html-html5 You can then follow the steps to paste the HTML code into Canvas's code editor. Any remaining equations (which start/end with $$) just highlight, then click the equation editor, and that equation will be converted. Here's a quick video...https://www.screencast.com/t/ovR7lKb53zA
@stigler , I’m not sure if the answer to this question, but I’m going to share it with the https://community.canvaslms.com/groups/canvas-developers?sr=search&searchId=c6bd43d6-a278-4486-8a88-... group in the Community to see if they can help.
@stigler ,
There is no short, easy way to do this.
That said, here are some things you might consider.
If the web page is housed on a secure server (https vs http), you can embed the page into Canvas through an iframe. It's not native to Canvas, but you can do all those things that Canvas' security model won't let you do like including the JavaScript necessary to render the LaTeX.
This approach doesn't do what you asked (have Canvas render it).
There currently is no support within Canvas for recognizing LaTeX triggers like $ or \[ \] and converting what is inside of it on the fly. To do so would require that your Canvas admin add custom JavaScript to invoke MathJax or KaTeX. That can have some accessibility issues, but I sure do like the ability to zoom with MathJax better than what I get out of Canvas' equation editor.
The custom JavaScript needs done at an account or sub-account level, you cannot add it at the course level and it may not work well with mobile apps. There have been several lengthy discussions and some feature requests about changing the way that math is handled with Canvas.
Your included library would be rendering it, not Canvas, but it would be similar.
It is possible to take (most of) the equations on a page and convert them to Canvas format if you or someone you know has some programming skills. The Equation Editor returns an image that is built off of the LaTeX that went into it.
For example, let's say that I wanted to create the fundamental theorem of calculus and had this equation
What I typed into the advanced view was this
\frac{d}{dx}\left[\int_a^xf(t)dt\right]=f(x)
What Canvas inserted as HTML was this (line breaks are mine):
<img
class="equation_image"
title="\frac{d}{dx}\left[\int_a^xf(t)dt\right]=f(x)"
src="/equation_images/%255Cfrac%257Bd%257D%257Bdx%257D%255Cleft%255B%255Cint_a%255Exf(t)dt%255Cright%255D%253Df(x)"
alt="LaTeX: \frac{d}{dx}\left[\int_a^xf(t)dt\right]=f(x)"
data-equation-content="\frac{d}{dx}\left[\int_a^xf(t)dt\right]=f(x)"
/>
It should be fairly obvious? where all but the src attribute on line 4 came from. The "/equation_images/" portion is clear, its the rest of it that is confusing.
Since that src is really a URI, it has to be encoded. It turns out that it's double URI encoded (URI encoding of the URI encoding). There's a note in the source code that says it's stored in the database double escaped. If it wasn't, then a / in the equation would be unescaped once and treated as part of the path rather than part of the equation.
In JavaScript, this is done with the encodeURIComponent function. Note that I had to double escape the \ to get it to work.
var latex='\\frac{d}{dx}\\left[\\int_a^xf(t)dt\\right]=f(x)';
var single=encodeURIComponent(latex);
var double=encodeURIComponent(single);
console.log('LaTeX : ' + latex);
console.log('Single: ' + single);
console.log('Double: ' + double);
Other languages have support for URI encoding, but they are normally greedier than the JavaScript version. By that, I mean they tend to encode all reserved characters like the ( and ). The JavaScript encodeURIComponent does not adhere strictly to the RFC3986 standard in that it does not escape these characters while most of the other implementations will.
A-Z a-z 0-9 - _ . ! ~ * ' ( )
The standard would also say to escape ! * ' ( ). But there is a note that says you don't have to escape them if they won't be used as delimiters, so we're okay. It just means that whatever implementation we find in a library will probably go ahead and escape them. However, the support for regular expressions and the ease of use (over command line JavaScript) may mean that you want to use a different language
Note that for PERL, which has excellent text processing and regular expression support, there is the URI::Escape::XS module that supports encodeURIComponent. Here is some PERL code to do the conversion.
use URI::Escape::XS;
my $latex = '\frac{d}{dx}\left[\int_a^xf(t)dt\right]=f(x)';
my $single = encodeURIComponent($latex);
my $double = encodeURIComponent($single);
print "$latex\n";
print "$single\n";
print "$double\n";
Ahh -- we're almost there now. We need a way to recognize the LaTeX delimiters, pull out the inside, and convert it. Consider this block. Here's I'm taking a paragraph that contains two LaTeX values:
<p>Consider a function $f$ whose domain is $(-\infty,\infty)$</p>
I'm going to convert it to Canvas format, using a subroutine I called canvexify (you could rename it to cantexify or cantex or latex2canvas if you like). In this case, it only looks for items with a single $ delimiter.
#!/usr/bin/perl
use strict;
use diagnostics;
use warnings;
use URI::Escape::XS;
my $t = q|<p>Consider a function $f$ whose domain is $(-\infty,\infty)$</p>|;
$t =~ s{\$(.*?)\$}{canvexify($1)}xmsge;
print "$t\n";
sub canvexify {
my $tex = shift || return '';
my $src = encodeURIComponent( encodeURIComponent($tex) );
my $img =
q{<img class="equation_image" title="%s" src="/equation_images/%s" alt="LaTeX: %s" data-equation-content="%s" />};
my $t = sprintf( $img, $tex, $src, $tex, $tex );
return $t;
}
The code is relatively simple and doesn't trap for things like newlines within the LaTeX code (it will work if they're present, but not remove them, so the title attribute contains a newline). If there was a quote in the code, you may need to do something special -- luckily, we don't use double quotes in LaTeX, but use two apostrophes instead.
When you run the code, you get this (I manually added the line breaks:
<p>
Consider a function
<img class="equation_image" title="f" src="/equation_images/f" alt="LaTeX: f" data-equation-content="f" />
whose domain is
<img class="equation_image" title="(-\infty,\infty)" src="/equation_images/(-%255Cinfty%252C%255Cinfty)" alt="LaTeX: (-\infty,\infty)" data-equation-content="(-\infty,\infty)" />
</p>
You could take and paste that into Canvas in the HTML editor view. When I do that and then save it, I get this in Canvas
Now that we know it's working, you could create a function that invokes it so it looked cleaner. You could replace lines 10 and 11 of the code with this:
print latex2canvas($t) . "\n";
sub latex2canvas {
my $t = shift;
$t =~ s{\$(.*?)\$}{canvexify($1)}xmsge;
return $t;
}
Now the task is to modify the code to read the input from a file so that you don't have to hard-code it into the program itself. That gets a little bit more complicated since you don't want to copy/paste the entire document, you only need the portion within the <body> </body> elements.
There may be someone out there who has already written a solution, I just threw this code together while writing this response so you could get a sense of what would be involved.
Thanks for the tips! It would be nice if there was a more seamless option, though.
I explored the possibility of taking the code I wrote and creating a web page that would do the conversion for you. That is, you could just take the HTML code you already had, copy and paste it into a textbox, and it would go through and convert it to the Canvasized format that you could copy/paste into Canvas.
A few things stopped me, but that doesn't mean that a less-function version still wouldn't be helpful. I especially thought about it with all of the wiki pages I have with statistics notes that would have been nice to convert into Canvas pages.
Ideally, I would like to add a button to the editor that would take everything that is in the RCE textbox, look for valid math nestled between $ or other other delimiters and replace it with you having to do anything else. In this way, you could just type the content in the RCE, add something really quick like $\frac{2}{3}$ and keep going, then click the button.
I read a lot about inserting content but didn't find anything that would allow you take the existing content and edit it short of a TinyMCE plugin. I've done some work trying to modify the TinyMCE plugin and shared that in other places, but I consider it highly experimental and very likely to break something else.
Then I thought about adding a separate button to the page, through a UserScript manager like Tampermonkey. That may still be a viable solution, I just haven't made time to explore it further.
Writing a web page that people could copy/paste into and from is probably the quickest way, but it's not as friendly.
The quickest way to develop it is also the least friendly. That would be to write some code that would take the contents of the RCE and scan for the math content and replace it. But you would have to paste it into the browser's console to run whenever you wanted to.
Another option, and probably the one I would ultimately use if I was to go, would be to write a script that would download the contents of a web page from Canvas, make the modifications, and then re-upload the page back. I've already got scripts written that would do all of that except for the LaTeX portion, but it would require that people be familiar with coding to use it, so it's not something I would publish for mass consumption.
Mixed into all of that automatic conversion thing is the possibility that you have invalid LaTeX markup -- at least as far as Canvas knows -- and what to do with it. Canvas doesn't load some packages that I use regularly, so I might forget that I can't do something in Canvas that I can while directly editing LaTeX or other packages like my Differential Equations wiki where I've loaded the rsfs package to get my script L for the Laplace transform symbol.
James,
Another approach that would be user friendly would be to write a Chrome Extension. You could be in the RCE, then click a button on your Chrome toolbar to go through and replace all the $$...$$ stuff with the proper Canvas equation format.
Jim
James,
Thanks for all this. It really is something that Canvas should fix. Meanwhile, though, I have found a solution that works for me. I write my content in markdown, and just include the $$ delimited equations in the markdown. I then go to a really useful website: Upmath: Markdown & LaTeX Online Editor . I paste my markdown on the left, and it renders a preview on the right. If you click the HTML button, it displays the complete HTML code on the right, which I can copy/paste into Canvas. It works beautifully. It actually is transforming the equations into images using Tex Live. It does all this automatically, stores them on their server (somewhere), and embeds the image calls into the HTML. The integration is beautiful
Jim
I doubt that Canvas is going to fix it any time soon. There's just not enough demand for it and there are bigger things to work on that will impact more people. Math and science people have been long frustrated by the lack of support within quizzes.
The solution you found looks interesting and I love that allowed for Tikz graphs, which just isn't available in Canvas at all. If I want to use those now, I convert my LaTeX document to PDF and then let people open the PDF file. When I was younger, I used to spend hours converting things all of my PDFs to HTML to display on the web and now that I'm older and find it harder to get thing done, I just make the PDF available to people to download. Since Canvas offers inline previewing of PDFs and browsers have built-in support for displaying PDFs, it's easier for the user to see the content than it used to be.
One thing your solution (and my conversion to a PDF) falls a little short on is accessibility. It marks up the alt attribute with the LaTeX as does Canvas, but Canvas also adds a prefix of "LaTeX: " to the alt tag so people knows that's what it is. Canvas also uses the LaTeX for the title property so that when you mouse over the image, it displays. Finally, Canvas converts the LaTeX into MathML so that the screen readers can read the MathML.
Accessibility may not be high on your list of criteria, especially given the difficulty in obtaining with mathematical content in the first place. I mention this because it is high on Canvas' list and because someone asked me just today how Canvas handled accessibility with math and so the answer was fresh in my mind.
Hi @stigler
Great solution with that Upmath!
I'm going to toss my old-school solution in the ring as well... I usually make pictures of LaTeX or other non-native HTML font stuffs, and put those in where they belong in the Canvas page/quiz.
The beauty of this solution is that I can also include alt text. And if I name my images cleverly enough, I can swap them out easy enough from the files section if the instructor makes a change or there was an error I did not catch.
Oh and if it's on a quiz, don't worry I do not give away the answer with the alt text. I call it something like QuizImage01 (in the alt-text and filename) and so forth.
Cheers - Shar
Jim - and James
I tried some of the Upmath examples direct in Canvas advanced math/equation editor, and the simple equation and matrix works fine (with appropriate unescaping of the html ampersand character). However, those using the Tikz library don't.
I imagine that colleagues may also be using other specialist Latex libraries. Also, trying to get a basic Latex document rendered (like the Stanford LaTeX example) doesn't seem to work, although this is correctly processed by services such as https://pandoc.org.
Is there any way of knowing what Latex Canvas does and does not support? In addition, are there other equivalent services to the one offer by Roman Parpalak via Upmath (which also doesn't cope with a copy and paste of the Stanford example - assuming I am not doing something silly), which would support full/wider Latex processing?
Thanks
guy
If you're using the built-in mechanism to produce the SVG/MathML/PNG version from your LaTeX, then you're at the mercy of what Canvas loads. Copying and pasting unsupported LaTeX, like tikz, isn't going to make it work in Canvas because Canvas doesn't load those libraries. It doesn't matter what else it works in.
I haven't been able to find the list of supported commands. I know someone out there has reported issues with things not working when Canvas changes the way they process it. I don't know that Canvas would have a complete list unless they were doing some kind of whitelisting like they do with the HTML editor. Otherwise, they would just use whatever the underlying library uses and direct people to their site.
I normally don't speak unless I have a good idea of what I'm talking about. I've looked into this with a little bit of success but I haven't fully explored it. So I'll preface what I write below with a warning that I say it with a low degree of confidence.
It looks like Canvas might be a microservice that uses MathJax 2.7.2 and mathjax-node with the defaults plus the color extension.
Based off that, there are a couple of lists that might help. But ultimately, you'll want to test anything too specialized inside Canvas.
Note that some of the supported commands require inclusion of an extension. I'm not sure of the configuration they're using, but the AMSmath, AMSsymbols, noErrors, and noUndefined extensions are preloaded on most of the predefined configurations from MathJax. Here are some highlights from the MathJax 2.7 documentation page.
My working hypothesis is start with the list starting about 50% through this page. If the command is included in AMSmath, AMSsymbols, noErrors, or noUndefined extensions, it works out of the box. If it's in the Color extension, then Canvas loads it. If it's in an extension that has brackets around it in the list, which includes bbox, boldsymbol, HTML, newcommand, HTML, mathchoice, unicode, and verb then it will probably work.
There's also a list of supported environments at the bottom of the page.
In other places in the community, I've seen people talk about Canvas using MathQuill. I know less about MathQuill than I do MathJax, so I won't say anything else about that.
Thanks James - hugely useful, as ever.
g
About three years ago I could paste \parbox{box-width}{LaTeX source} into the "Advanced Maths Mode" in Canvas and it would typeset the LaTeX source beautifully. However, some Canvas bureaucrat then disabled \parbox and refused to restore it, much to my annoyance.
One way to still proceed is to manually put in the line breaks yourself. For example, paste into Advanced Maths Mode the following
\text{not a minimiser of $f(\vec{x})$, and $-\nabla f(\vec{x})$}\\
\text{is perpendicular to the nonempty set $S(\vec{x})$ }
and it will get typeset by Canvas much the same as if these two LaTeX lines are within the following in a LaTeX document (which is of course how I prepare the info)
\(\begin{array}{l}
...
\end{array}\)
(Curiously, the preview in Canvas centers each of the lines, but the eventual actual display is left-aligned---at least for our installation.)
Most probably related to the same topic. I find the preview of the advanced equation editor to be extremely slow but even more disturbing I created three very equation-heavy pages lately only to find out today that these equations only render as long as I am in editor mode. Once I save the page the equations are blank:
Same observation for Firefox, Chrome and Opera.
Other pages with equations within the same course project work just fine.
The problem appears to be caused by equations which are inside <p></p> environments, e.g. short inline equations are currently not rendered at all on the final page - however they appear to be fine in the editor.
Hi Uwe Zimmermann,
What happens when you PUBLISH your pages?
I just tried your \alpha\beta\gamma example and it worked for me, but I also published my pages.
Cheers - Shar
ishar-uw I discovered it on pages which I already had published for my students - so for me the problem persists whether a page is published or not.
But it also appears to be intermittent, for suddenly the equations appears again on the published pages yesterday evening (European time), but then they disappeared again... they were always rendering in the student app by the way.
As of today - 2019-09-25 - the equations appear to be back. Perhaps some failed maintenance at the server and I managed to just see it an panic?
Uwe.
We are seeing something similar, but haven't nailed down the circumstances yet. For now (10/21/19), the equations seem to be showing properly.
I wonder if you are able to suggest the purchase of EquatIO, a Chrome extension?
I think you would like it if you check it out.
I strictly object to an option which would lock me or anyone else to the use of a specific browser. Also - I currently still do not have access to our university's own installation of Canvas due to bureaucratic and administrative hurdles, therefore I am putting my course contents onto Instructure's own server and ask my students to voluntarily register there.
Furthermore I would not like to try to medicate the symptoms without looking for the root cause. The slow preview which forces me to type very slowly when entering equations should simply not be that slow. I do not know where the bottleneck is behind the scenes of the built-in editor, for me it would just be very convenient if I could disable the live-preview in the advances LaTeX-mode. I am fluent in LaTeX, I use it for everything, and when I am in the advanced mode I simply do not expect a real-time preview, especially if it slows me down as much as it does.
By the way: The problem with the suddenly disappearing equations which started my contribution to this discussion has not happened again since... (keeping fingers crossed)
I found a quicker way to enter LaTeX equations which goes around the annoyingly slow preview-editor. If you are comfortable with entering LaTeX-code it might work for you as well. Just enter the plain LaTeX-code on you page in the normal rich-text-editor, then select the LaTeX code, enter the equation editor and voilà.
Wow! Thanks for sharing that trick.
You showed it in the video, but people may not have picked up on it. You may get an warning unless you go into the editor once ahead of time and switch to Advanced View. You can cancel exit (as you did), but Canvas remembers that you're in advanced view.
Here's what I got with a nonsensical expression I tried before switching.
That's slightly funny. It says it cannot be rendered in Basic View, yet the link says "Switch View to Basic," which means that I'm already in advanced view.
If I click on the "Switch View to Basic", it renders the content, and the link remains "Switch View to Basic."
try to exit not cancel the editor before switching to advanced view, i.e. never ever entering advanced view!
In spite of the error message it still works!
(and the question remains: who in their sane mind would ever use the basic view anyway...)
Uwe.
Sorry, I edited my post to use the word exit. It was the end of a long day and I meant "cancel" as "exit without saving."
People who are used to using a graphical interface and not the keyboard will use the basic view. Most of my students (first two years of college) right click a mouse to get copy/paste rather than using the keyboard shortcuts. For them, clicking on a √ symbol is a lot easier than typing \sqrt{}.
It's also necessary for people who don't use it often enough to memorize it. Do you want to have students get frustrated learning LaTeX or focus on the material of the course?
Word and WordPerfect both have graphical equation editors in them that don't require LaTeX. For years, I used Design Science's MathType for editing. I created a Mediawiki site for my Differential Equations class about 10 years ago and that's when I really started using LaTeX to render the math content. Then I needed to generate exams programmatically and started using LaTeX full time. When MathType went to a subscription model, I stopped using it, plus it never came out looking good when converting WordPerfect to a PDF anyway.
The reasons for a basic view are many and for some people, they wouldn't be able to enter any math content without it.
For others, the advanced view is a requirement because the basic view just doesn't cut it. The choice of symbols on the palette may make the basic view less useful. It looked like you hesitated in your video when you moused over the \coprod, perhaps thinking "Why is that basic?" I know I wondered that. On the other hand, the new RCE has moved subscript to now require two mouse clicks and I use that a lot when dealing with systems of equations.
What would be nice would be to have a way to check your advanced LaTeX in New Quizzes without having to save the question first. It would be nice to confirm that you have the correct LaTeX (like the RCE). Maybe show the LaTeX when you're editing between the \( and \) delimiters and render it as math when your cursor is outside the delimiters.
The way I have used in the past, i.e. to write the plain LaTeX-code in the main editor, before entering the flawed "Equation editor" is yet another feature which Instructure now has broken with their latest, fancy new editor...
Firstly, the dialog box for entering an equation now looks even more like a pre-school playground than before, making it harder or impossible to use it for a serious equation typing.
Secondly, pre-selected text from the main editor window is no longer copied into the editor field inside the equation editor.
Thirdly, the choice of "directly editing LaTeX" is not sticky anymore and needs to be click-activiated every single time you want to enter an equation.
WHY? WHY are all the working features replaced by kindergarten-stuff?
Hi! I was trying to import some Jupyter Notebooks with Latex code (equations between $ or $$). I managed to do it with a Python3 script. Basically I'm using parse.quote(string, safe='()') two times from urllib library to mimic the equation editor used in canvas. I shared the code in Jupyter notebook with Latex to Canvas html equation converter · GitHub if anyone is interested.
It has some notebook specific cleaning (basically all style and scripts) but it could be adapted to almost any html.
Hope it helps.
Now this is very interesting, not to say exciting!
I would really appreciate if there was an LTI for Jupyter (not just a single-cell Sage as there is). I had tried to fool the system previously myself for quizzes with pre-generated LaTeX code, but that did not render at the time.
I will definitely have a look at your code!
Dear Uwe:
I tried to make the code a little more useful to anyone by creating this function: Function to convert Latex to Canvas html equation format. · GitHub .
Hope it helps.
David
I also had trouble with adding equations to Canvas pages. Even if you use the equations editor, there is no easy way of adding equation numbers or references to equations. To workaround this I made a LaTeX to plain HTML converter. You put in a LaTeX article and it spits out basic HTML that can be read by the HTML editor of a Canvas page. I'll add a picture of how it can look and if you want to check out the converter visit GitHub.
@rgerritsen Thank you for all your great contribution. I hadn't used Python before, so I had to start from scratch and learn some basic things, but for the most part I think I was able to follow your instructions in the LatexToCanvas README file. On Windows10, I installed pandoc, then Python, then added pandocfilters and pygments, then navigated to my LatexToCanvas directory and ran "python textToHtml.py input.tex" (this is the only point where I changed anything: I had to write 'python' instead of 'python3' in my command, because python3 was "not a recognised cmdlet, function, script file, or operable program.")
At the end of all of that, I have a problem. The output.html file that is generated does not display my Latex math. It displays the Latex code itself, rather than the typeset output that you would normally get in a PDF.
Any advice on what is wrong here? Am I missing a step? It seems the latex code hasn't been compiled, so the html is just displaying the code itself, rather than the output. I'm not sure what I am missing...
@phonehome, looking at the image you provide I think that you viewed the html in your browser and not withing a canvas environment. MathML (the math markup language used by canvas) is not necessarily natively supported by webbrowsers (I think only firefox has full MathML support). So for the math to work, you need to copy paste the HTML into the HTML editor of canvas and view it as a canvas page.
If you, however, want to use it to generate HTML that is fully supported by your browser, you could try to delete the MathML option in the textToHtml.py file (it is in the runPandoc function in the args variable), simply remove the string "--mathml" from the list. It will then default to use MathJax, which I think is more widely supported, but strangely enough not by the canvas HTML environment.
I hope that helps! If I was wrong with the assumption that it is outside the canvas environment let me know, then I will have a more detailed look.
@rgerritsen yes, you are right. Thank you!
I was looking at the html output itself in Chrome. It works fine within Canvas, after pasting the html file contents into the Canvas html editor.
However, I am a math teacher, not a coder. Why doesn't the math equation editor stick to the information typed in? Sometimes the image is fine for the student, sometimes it is not and I cannot tell ahead of time. When I want to do question groups or shuffle answers, this flaw definitely slows down the test taking of the students, since I have to show them a paper copy (also not easy to do) and hope their answers are in the same order as the paper copy.
For folks like me who get lost when the first step is "install Python," there's an online .tex-->HTML converter at https://www.vertopal.com/en/convert/tex-to-html-html5 You can then follow the steps to paste the HTML code into Canvas's code editor. Any remaining equations (which start/end with $$) just highlight, then click the equation editor, and that equation will be converted. Here's a quick video...https://www.screencast.com/t/ovR7lKb53zA
To interact with Panda Bot, our automated chatbot, you need to sign up or log in:
Sign inTo interact with Panda Bot, our automated chatbot, you need to sign up or log in:
Sign in
This discussion post is outdated and has been archived. Please use the Community question forums and official documentation for the most current and accurate information.