@@ -31,9 +31,11 @@ exports.convertToTspans = function(_context, gd, _callback) {
3131 // allow some elements to prohibit it by attaching 'data-notex' to the original
3232 var tex = ( ! _context . attr ( 'data-notex' ) ) &&
3333 gd && gd . _context . typesetMath &&
34- ( typeof MathJax !== 'undefined' ) &&
3534 matchTex ( str ) ;
3635
36+ // Only complain about MathJax version once we know there's actually math to render
37+ if ( tex && ! isMathJaxVersionSupported ( ) ) tex = null ;
38+
3739 var parent = d3 . select ( _context . node ( ) . parentNode ) ;
3840 if ( parent . empty ( ) ) return ;
3941 var svgClass = ( _context . attr ( 'class' ) ) ? _context . attr ( 'class' ) . split ( ' ' ) [ 0 ] : 'text' ;
@@ -204,18 +206,33 @@ function cleanEscapesForTex(s) {
204206// and reused for subsequent calls.
205207var mathjaxSVGDocument = null ;
206208
207- function texToSVG ( _texString , _config , _callback ) {
208- const MathJaxVersion = parseInt (
209- ( MathJax . version || '' ) . split ( '.' ) [ 0 ]
210- ) ;
209+ // Function which returns the major version of MathJax as an integer,
210+ // or null if MathJax is undefined or MathJax.version is falsy.
211+ const mathJaxMajorVersion = ( ) => ( typeof MathJax !== 'undefined' && MathJax . version ) ? parseInt ( MathJax . version . split ( '.' ) [ 0 ] ) : null ;
211212
212- if (
213- MathJaxVersion !== 3 &&
214- MathJaxVersion !== 4
215- ) {
213+ // Only warn once per page about each of these conditions
214+ var warnedMissingMathJax = false ;
215+ var warnedUnsupportedMathJax = false ;
216+
217+ // plotly.js is only compatible with MathJax v3 and v4.
218+ function isMathJaxVersionSupported ( ) {
219+ const version = mathJaxMajorVersion ( ) ;
220+ if ( version === 3 || version === 4 ) return true ;
221+
222+ if ( version === null ) {
223+ if ( ! warnedMissingMathJax ) {
224+ warnedMissingMathJax = true ;
225+ Lib . warn ( 'MathJax is not loaded. Math equations will not be rendered.' ) ;
226+ }
227+ } else if ( ! warnedUnsupportedMathJax ) {
228+ warnedUnsupportedMathJax = true ;
216229 Lib . warn ( 'Unsupported MathJax version:' , MathJax . version ) ;
217- return ;
218230 }
231+ return false ;
232+ }
233+
234+ function texToSVG ( _texString , _config , _callback ) {
235+ const MathJaxVersion = mathJaxMajorVersion ( ) ;
219236
220237 var tmpDiv ;
221238
0 commit comments