简体   繁体   English

如何在 cytoscape 节点上的 qtip 内使用 mathjax

[英]How to use mathjax inside qtip on a cytoscape node

How could I use the Mathjax library within qtip in a cytoscape node?如何在 cytoscape 节点的 qtip 中使用 Mathjax 库?

In the sample code below, the tooltip is displayed when clicking over the top of a node, but the Mathjax is not rendered.在下面的示例代码中,单击节点顶部时会显示工具提示,但未呈现 Mathjax。

How can this be fixed so that the equation is also shown in the tooltip?如何解决这个问题,以便公式也显示在工具提示中? The solution in this answer by @peterkrautzberger could be an option, but I didn't manage to adapt it to this case. @peterkrautzberger 在这个答案中的解决方案可能是一个选项,但我没有设法使其适应这种情况。

Edited: The answer by @dipenshah solves the original question.编辑: @dipenshah 的回答解决了原来的问题。 However, when extending the sample code allowing for dynamic Mathjax content it doesn't seem to work.但是,当扩展允许动态 Mathjax 内容的示例代码时,它似乎不起作用。 If instead of having a fixed text for the tooltips, the text is taken from the element txt in the definition of the node, then the Mathjax is not rendered.如果工具提示没有固定文本,而是从节点定义中的元素txt中获取文本,则不会呈现 Mathjax。 I have modified the code below to show it.我修改了下面的代码来显示它。

 $(function(){ var cy = window.cy = cytoscape({ container: document.getElementById('cy'), ready: function(){ }, elements: { nodes: [ { data: { id: '1', name: 'Node 1', txt: '$$\\\\int f(x)dx=\\\\frac{x^2+y}{2x} + C\\\\,.$$' }, }, { data: { id: '2', name: 'Node 2', txt: '$$\\\\frac{\\\\partial x^2}{dx}\\\\,.$$' }, } ], edges: [ { data: { source: '1', target: '2' } } ] } }); cy.elements().nodes().qtip({ content: function(){ return this.data('txt') }, position: { my: 'top center', at: 'bottom center' }, style: { classes: 'qtip-bootstrap', tip: { width: 16, height: 8 } }, events: { render: function() { MathJax.Hub.Queue(["Typeset", MathJax.Hub]); } } }); });
 <link rel="stylesheet" type="text/css" href="http://cdnjs.cloudflare.com/ajax/libs/qtip2/2.2.0/jquery.qtip.css"> <script src="http://code.jquery.com/jquery-2.0.3.min.js"></script> <script src="https://unpkg.com/cytoscape/dist/cytoscape.min.js"></script> <script src="http://cdnjs.cloudflare.com/ajax/libs/qtip2/2.2.0/jquery.qtip.js"></script> <script src="https://unpkg.com/cytoscape-qtip@2.7.1/cytoscape-qtip.js"></script> <script type="text/javascript" async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-MML-AM_CHTML"> </script> <h3>Cytoscape qtip demo</h3> <p> $$\\int f(x)dx=\\frac{x^2+y}{2x} + C\\,.$$ </p> <div id="cy" style="width:50%; height:50%; position:absolute;"></div>

Based on updated question: For dynamic content there is no guarantee that by the time MathJax starts processing dom element will be available in the same event callback, so here we can use setTimeout to queue processing after current Javascript calls have finished processing:基于更新的问题:对于动态内容,无法保证在 MathJax 开始处理 dom 元素时将在同一事件回调中可用,因此在这里我们可以使用setTimeout在当前 Javascript 调用完成处理后排队处理:

render: function(event, api) {
  var tooltip = api.elements.tooltip;
  tooltip.bind('tooltipshow', function(event, api) {
    setTimeout(() => MathJax.Hub.Queue(["Typeset", MathJax.Hub]));
  });
}

Let's take a look:让我们来看看:

 $(function() { var cy = window.cy = cytoscape({ container: document.getElementById('cy'), ready: function() {}, elements: { nodes: [{ data: { id: '1', name: 'Node 1', txt: '$$\\\\int f(x)dx=\\\\frac{x^2+y}{2x} + C\\\\,.$$' }, }, { data: { id: '2', name: 'Node 2', txt: '$$\\\\frac{\\\\partial x^2}{dx}\\\\,.$$' }, } ], edges: [{ data: { source: '1', target: '2' } }] } }); cy.elements().nodes().qtip({ content: function() { return this.data('txt'); }, position: { my: 'top center', at: 'bottom center' }, style: { classes: 'qtip-bootstrap', tip: { width: 16, height: 8 } }, events: { render: function(event, api) { var tooltip = api.elements.tooltip; tooltip.bind('tooltipshow', function(event, api) { setTimeout(() => MathJax.Hub.Queue(["Typeset", MathJax.Hub])); }); } } }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/qtip2/3.0.3/jquery.qtip.min.js"></script> <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/qtip2/2.2.0/jquery.qtip.css"> <script src="https://unpkg.com/cytoscape/dist/cytoscape.min.js"></script> <script src="https://unpkg.com/cytoscape-qtip@2.7.1/cytoscape-qtip.js"></script> <script type="text/javascript" async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-MML-AM_CHTML"> </script> <h3>Cytoscape qtip demo</h3> <p> $$\\int f(x)dx=\\frac{x^2+y}{2x} + C\\,.$$ </p> <div id="cy" style="width:50%; height:50%; position:absolute;"></div>

Original answer:原答案:

You can use render callback on qtip , to let MathJax know about update:您可以在qtip上使用render回调,让MathJax知道更新:

events: {
  render: function() {
    MathJax.Hub.Queue(["Typeset", MathJax.Hub]);
  }
}

checkout updated code:结帐更新代码:

 $(function() { var cy = window.cy = cytoscape({ container: document.getElementById('cy'), elements: { nodes: [{ data: { id: '1', name: 'Node 1' }, }, { data: { id: '2', name: 'Node 2' }, } ], edges: [{ data: { source: '1', target: '2' } }] } }); var qtip = cy.elements().nodes().qtip({ content: '$$\\int f(x)dx=\\frac{x^2+y}{2x} + C\\,.$$', position: { my: 'top center', at: 'bottom center' }, style: { classes: 'qtip-bootstrap', tip: { width: 16, height: 8 } }, events: { render: function() { MathJax.Hub.Queue(["Typeset", MathJax.Hub]); } } }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/qtip2/3.0.3/jquery.qtip.min.js"></script> <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/qtip2/2.2.0/jquery.qtip.css"> <script src="https://unpkg.com/cytoscape/dist/cytoscape.min.js"></script> <script src="https://unpkg.com/cytoscape-qtip@2.7.1/cytoscape-qtip.js"></script> <script type="text/javascript" async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-MML-AM_CHTML"> </script> <h3>Cytoscape qtip demo</h3> <p> $$\\int f(x)dx=\\frac{x^2+y}{2x} + C\\,.$$ </p> <div id="cy" style="width:50%; height:50%; position:absolute;"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM