MathJax自动换行

6
我已经使用下面的代码配置了MathJax的额外处理。
<script type="text/x-mathjax-config">

    MathJax.Hub.Config({
    tex2jax: { inlineMath: [["$","$"],["\\(","\\)"]] },
    "HTML-CSS": {
       linebreaks: { width: "container" }          
    }              
    });

</script>

我认为它必须与我在HTML代码中使用的自动换行容器一起工作。

我有这个CSS,它设置了自动换行。

.col {
        background: #f0f0f0; 
        width: 230px; 
        padding: 10px; 
        font-size: 1.5em; 
        word-wrap: break-word; 
    }    

这是我的正文:

<body>

        <div class = 'col'>

        $$
           Pr [( G \bigcup B \bigcup S)^e ] = 1 - Pr ( G \bigcup B \bigcup S)                
           = 1 - [Pr(G)+Pr(B)Pr(S)-Pr(G \bigcap B) - Pr(G \bigcap S) - Pr(B \bigcap S)]
           Pr [( G \bigcup B \bigcup S)^e ] = 1 - Pr ( G \bigcup B \bigcup S)
           = 1 - [Pr(G)+Pr(B)Pr(S)-Pr(G \bigcap B) - Pr(G \bigcap S) - Pr(B \bigcap S)]
           = 1 - (0.28 + 0.29 + 0.19 - 0.14 - 0.10 -0.12 + 0.08) = 1 - 0.48 = 0.52
        $$

        </div>

</body>

但是自动换行在我的情况下不起作用。
我已经附上了 Safari 在 iPad 上的屏幕截图。 enter image description here 单词必须换行到第二行,但是它们没有换行,正如你所看到的那样。
2个回答

9

编辑 如果您使用的是iOS6,则可能会遇到这个MathJax bug

MathJax使用自己的断行算法(实现MathML规范)。设置word-wrap: break-word;不会改变其行为。

除非有一些信息缺失,否则您的代码对我来说可以按预期工作,即MathJax进行了断行。

下面是一个完整的示例。

<!doctype HTML>
<head>
<title>Break!</title>
<script type="text/x-mathjax-config">

MathJax.Hub.Config({
tex2jax: { inlineMath: [["$","$"],["\\(","\\)"]] },
"HTML-CSS": {
  linebreaks: { automatic: true, width: "container" }          
}              
});

</script>
<script type="text/javascript"
  src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
<style>
.col {
    background: #f0f0f0; 
    width: 230px; 
    padding: 10px; 
    font-size: 1.5em; 
    word-wrap: break-word; 
}  
</style>

</head>
<body>

    <div class = 'col'>

    $$
      Pr [( G \bigcup B \bigcup S)^e ] = 1 - Pr ( G \bigcup B \bigcup S)                
      = 1 - [Pr(G)+Pr(B)Pr(S)-Pr(G \bigcap B) - Pr(G \bigcap S) - Pr(B \bigcap S)]
      Pr [( G \bigcup B \bigcup S)^e ] = 1 - Pr ( G \bigcup B \bigcup S)
      = 1 - [Pr(G)+Pr(B)Pr(S)-Pr(G \bigcap B) - Pr(G \bigcap S) - Pr(B \bigcap S)]
      = 1 - (0.28 + 0.29 + 0.19 - 0.14 - 0.10 -0.12 + 0.08) = 1 - 0.48 = 0.52
    $$

    </div>

</body>

1
你是否将配置块放在脚本调用之前了? - Peter Krautzberger
1
是的,但如果你计划将它加载到iPad应用程序中,你需要删除你不使用的组件,以使它变得更小。有关详细信息,请参阅此文章。此外,在这种情况下,您不需要使用组合配置文件,因为在没有网络参与时,使用自己的自定义配置同样快。 - Davide Cervone
谢谢Davide,这些建议非常有用。我遇到了一个大问题 - 处理时间。如果没有互联网连接,构建公式组件的时间比使用CDN要长得多。 - Matrosov Oleksandr
1
那么我怀疑你的设置有问题。渲染时间不应该更长。我在想你是否删除了一些不应该删除的东西,而MathJax正在尝试加载它并超时(它有一个15秒的超时等待文件加载)。您可能希望将 MathJax.Hub.Queue(function(){alert(MathJax.Message.Log())}) 添加到您的配置中,并查看是否有任何“加载失败”的错误消息。 - Davide Cervone
1
我已经更新了答案,因为cdn.mathjax.org即将到达其生命周期的尽头,请参见https://www.mathjax.org/cdn-shutting-down/。 - Peter Krautzberger
显示剩余12条评论

1
我也遇到过类似的问题,但是我无法使用自动换行解决。
这个问题是由MathJax的默认类引起的,因此我们需要覆盖它们,使其根据父元素的宽度工作:
.mjx-chtml {
   font-size: ${IS_MOBILE ? '100%' : '120%'} !important;
}
.mjx-table {
   white-space: pre-line !important;
}
.mjx-table .mjx-char {
   white-space: ${IS_MOBILE ? 'pre-line' : 'inherit'} !important;
}
.mjx-full-width {
   width: 0em !important;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接