szhorvat
(Szabolcs)
2020 年4 月 17 日 12:27
1
We have a discourse forum for a scientific software, and use the math plugin. Unfortunately, math is not quotable.
If I select some text, a Quote button appears:
Clicking it results in the following:
[quote]
definition of the closeness centrality:
ci=1N−1N∑j=11dij c_i = \frac{1}{N-1} \sum_{j=1}^N\frac{1}{d_{ij}}
[/quote]
Notice that in the quotation, the math appeared as a weird textual version (unreadable) followed by the actual LaTeX source, but lacking the $ delimiters.
I assume that there is no fix for this currently. It would be nice if this could be improved in the future.
8 个赞
mbauman
(Matt Bauman)
2021 年5 月 4 日 19:50
2
This is still occurring and also happens with inline math, as reported by a member on our instance:
I’ve noticed that if you try to quote a post with LaTeX equations, e.g. $O(n^3)$ for O(n^3), in the quoted text it gets mangled to something like O(n3)O(n^3). It would be nice if the original $O(n^3)$ were preserved.
Reading time: 1 mins 🕑
Likes: 3 ❤
3 个赞
sam
(Sam Saffron)
2021 年5 月 5 日 02:36
3
Note, I agree completely we should clean this up, but it feels extremely complicated.
I am putting a pr-welcome on this in case someone with advances skills wants to give it a shot.
7 个赞
XertroV
(Max Kaye)
2022 年4 月 6 日 03:03
4
我研究了一下引用数学(我正在考虑编写一个 PR 或为其中一个做出贡献)。
在此记录一些初步的想法和发现。
引用似乎都经过了以下主要组件:
buildQuote 函数接收一个帖子对象 – 我认为这是一个完整的帖子 JSON 描述 示例 。
因此,我的初步印象是以下几点可能可以进行修补:
toMarkdown 根据各种 HTML 标签匹配元素;我认为 mathjax 使用自定义元素是可检测的(可能不是 SVG 渲染),katex 也是如此?
selectedText 对代码块和 onebox 等元素进行一些匹配 – 也许可以检测 mathjax/katex 元素并进行转换?(注意:这里有一个钩子或类似的东西可能是一个好主意,这样转换逻辑就可以添加到 discourse-math 插件中,而不是主 discourse 代码库中)。
其他几点似乎不如这两点合适。
关于方法和其他获取原始数学内容的想法。
根据 katex / mathjax+svg[^1] 的工作方式,我认为每个数学实例都有一个根 HTML 元素(可能不止一个,因为有一些隐藏的元素用于可访问性等)。所以如果我们知道数学存在的位置,我们就可以解析原始帖子中用 $ 分隔的内容(但解释器很棘手 – 可能代码库中已经有一个了?)
[^1]:我认为 mathjax+svg 可能不受支持,所以这可能不是一个问题
或者,mathjax 至少有一个右键单击 > 查看 tex 等功能(这应该是 $ 对之间的文字字符串);我不知道如何挂入其中,但如果有一种方法可以为 mathjax v2、v3 和 katex 执行此操作,那么就可以使用它将选定的格式化数学替换为 $...$(注意:这适用于内联数学,还需要涵盖另一种情况 – 它使用 $$...$$ 或 [/.../] 分隔符。)
目前存在的问题:
我不知道如何实现钩子,也不知道是否有更好的东西可以使用/在 discourse 代码库中有更好的实现方式。
mathjax/katex 有一些未知数需要调查。
HTML 元素 – 如何可靠地检测它们?
如何获取原始 TeX 代码?
如果任何人有关于在哪里/什么可以修补的其他想法、其他可能的实现方法或任何其他内容,请发布。
6 个赞
Ethsim2
(Ethan )
2026 年3 月 8 日 20:20
6
Discourse Math 在 2026 年 1 月进行了大量升级,采用了新的官方核心捆绑版本,默认使用 MathJax 4.1,并提供 KaTeX 作为替代方案。插件文档还指出,KaTeX 包含用于复制 LaTeX 源代码的 CopyTex 扩展。
是否值得添加一个小的 theme-component 补丁来改善对数学内容丰富的帖子的引用选择?
对于像 Physics with Ethan 这样的重度 MathJax 网站,最实用的想法是:
拦截选定的 HTML,然后再由 Discourse 将其转换回 Markdown,
查找数学包装元素,
将它们替换为其原始 TeX 源代码,用 $...$ 或 $$...$$ 包装,
然后让 Discourse 继续构建引用。
如果值得修改 Horizon(或默认)主题的 </head> 或 JS 区域,这是一个我将开始使用的潜在 theme-component 脚本。
<script type="text/discourse-plugin" version="1.0">
apiInitializer("1.34.0", (api) => {
function texFromMathElement(el) {
// MathJax v3/v4 通常将源代码存储在注释子元素中。
const annotation =
el.querySelector('annotation[encoding="application/x-tex"]') ||
el.querySelector('annotation');
if (annotation?.textContent?.trim()) {
const tex = annotation.textContent.trim();
// 启发式:块级数学包装器通常是显示容器。
const isBlock =
el.tagName === "MJX-CONTAINER" &&
(el.getAttribute("display") === "true" ||
el.getAttribute("display") === "block");
return isBlock ? `$$\n${tex}\n$$` : `$${tex}$`;
}
return null;
}
function patchMathInFragment(fragment) {
const candidates = fragment.querySelectorAll(
"mjx-container, .math, .katex, .MathJax"
);
candidates.forEach((node) => {
const replacement = texFromMathElement(node);
if (!replacement) {
return;
}
const textNode = document.createTextNode(replacement);
node.replaceWith(textNode);
});
return fragment;
}
api.modifyClass("component:quote-button", {
pluginId: "ethan-math-quote-fix",
_selectionChanged() {
this._super(...arguments);
try {
const selection = window.getSelection();
if (!selection || selection.rangeCount === 0) {
return;
}
const range = selection.getRangeAt(0);
const fragment = range.cloneContents();
patchMathInFragment(fragment);
const container = document.createElement("div");
container.appendChild(fragment);
// 替换 Discourse 稍后转换为引用 Markdown 的缓冲区。
if (this.quoteState) {
this.quoteState.buffer = container.innerHTML;
}
} catch (e) {
// 保持静默失败,以便正常引用仍然有效。
console.warn("Math quote patch failed:", e);
}
},
});
});
</script>
有几个重要的注意事项:
这是一个实用的补丁,而不是我从 Meta 找到的已发布内容。底层策略是从引用路径穿过选定的 HTML 并到达 toMarkdown 推断出来的。
当我使用 MathJax 提供程序时,它最有可能提供帮助,这是 Discourse Math 中的当前默认设置,也是我通常使用的选项。
脚本可能需要小的选择器调整,这取决于我的渲染数学最终是 mjx-container、.MathJax 还是其他包装器。我会在一个暂存站点上进行测试,该站点将是一个新的标准安装。
对于所有边缘情况,尤其是在混合行内/块选择或嵌套引用内容的情况下,它不太可能完美。
由于 Discourse 现在官方支持带有 CopyTex 的 KaTeX,如果我的主要痛点是复制 LaTeX 源代码而不是渲染保真度,那么另一种方法是切换提供程序。这不会自动解决引用选择问题,但它确实意味着渲染的数学已经内置了更易于复制的路径。
对于我的重度 MathJax 网站 Physics with Ethan,我目前正在考虑:
保持 MathJax,除非有人建议我改用 KaTeX?,
也许从上面(使用暂存站点)向 Horizon 主题添加一个引用选择补丁。
并将其视为本地化的质量改进,直到出现适当的上游解决方案。这个 2020 年的功能主题在 2026 年仍然开放,这表明上游可能仍然很慢。
3 个赞