标记格式
这个 tagformat 扩展提供了自定义公式标记和自动公式编号格式的功能。通过在 tagformat
目标 tex
mathjax配置的块。您可以提供的功能列在 标记格式选项 部分。
装入 tagformat 扩展,添加 '[tex]/tagformat'
到 load
数组 loader
块的mathjax配置,并添加 'tagformat'
到 packages
数组 tex
块。
window.MathJax = {
loader: {load: ['[tex]/tagformat']},
tex: {packages: {'[+]': ['tagformat']}}
};
标记格式选项
添加 tagformat 扩展到 packages
数组添加了 tagformat
子对象到 tex
具有以下值的配置块:
tagformat: {
number: (n) => n.toString(),
tag: (tag) => '(' + tag + ')',
id: (id) => 'mjx-eqn:' + id.replace(/\s/g, '_'),
url: (id, base) => base + '#' + encodeURIComponent(id),
}
- number: function (n) {return n.toString()}
一个函数,告诉mathjax要将哪个标记用于等式号
n
. 这可以用来用一系列符号而不是数字来标记方程式,或者用节和小节编号来代替。
- tag: function (n) {return '(' + n + ')'}
一个函数,它告诉mathjax如何格式化一个方程式编号,以便显示为方程式的标记。这是出现在带标记或带编号的公式的边距中的内容。
- id: function (n) {return 'mjx-eqn:' + n.replace(/\\s/g, '_')}
一个函数,告诉mathjax将哪个id用作公式的锚(以便它可以在url引用中使用)。
- url: function (id, base) {return base + '#' + encodeURIComponent(id)}
获取公式ID和基URL并返回链接到它的URL的函数。这个
base
值取自 baseURL 值,以便在页面中创建链接,即使它有<base>
元素,该元素将页的基URL设置为其他位置。
示例:节编号
此示例显示了一种为在 tags
期权在 tex
配置块设置为 'ams'
或 'all'
.
MathJax = {
section: 1,
tex: {
tagformat: {
number: (n) => MathJax.config.section + '.' + n,
id: (tag) => 'eqn-id:' + tag
}
},
startup: {
ready() {
MathJax.startup.defaultReady();
MathJax.startup.input[0].preFilters.add(({math}) => {
if (math.inputData.recompile) {
MathJax.config.section = math.inputData.recompile.section;
}
});
MathJax.startup.input[0].postFilters.add(({math}) => {
if (math.inputData.recompile) {
math.inputData.recompile.section = MathJax.config.section;
}
});
}
}
};
这将自动公式编号安排为以下形式 1.n
,并使用表单的ID eqn-id:1.n
作为 id
网页内标记的属性。它还为Tex输入JAX设置前和后过滤器,以便为包含对后续表达式的前向引用的自动编号公式正确处理节号。此示例使用现代函数表示法(使用 =>
),但您也可以使用 function (n) {{return ...}}
。
可以使用JavaScript通过设置 MathJax.config.section
变量。也可以创建用于控制节号的TeX宏。有一种可能性:
MathJax = {
startup: {
ready() {
const Configuration = MathJax._.input.tex.Configuration.Configuration;
const CommandMap = MathJax._.input.tex.SymbolMap.CommandMap;
new CommandMap('sections', {
nextSection: 'NextSection',
setSection: 'SetSection',
}, {
NextSection(parser, name) {
MathJax.config.section++;
parser.tags.counter = parser.tags.allCounter = 0;
},
SetSection(parser, name) {
const n = parser.GetArgument(name);
MathJax.config.section = parseInt(n);
}
});
Configuration.create(
'sections', {handler: {macro: ['sections']}}
);
MathJax.startup.defaultReady();
}
}
};
当然,您需要将此配置与其他配置选项合并。
这使两个新宏可用: \nextSection
,它将增加节计数器,并且 \setSection{{n}}
,将节编号设置为 n
. 请注意,这些必须在数学分隔符中发出,以便MathJax处理它们。为了防止它们在页面中产生任何输出,可以将它们包含在隐藏元素中。例如,
<span style="display: hidden">\(\nextSection\)</span>
或者类似的东西。