如何修改MathJax中\mathfrak命令的花体字体?
如何修改MathJax中
\mathfrak命令的字体 你遇到的font-mathjax.js加载错误是因为MathJax 2.x版本不存在这个扩展,无需尝试该方法。针对你的需求,以下两种可行方案可以替换\mathfrak的渲染字体:
方案一:CSS样式覆盖(推荐)
MathJax渲染\mathfrak时,会为对应元素添加MJX-tex-frak类,直接通过CSS覆盖该类的字体属性即可,无需修改MathJax核心配置,排版对齐更稳定。
修改步骤:
- 确保你的自定义Fraktur字体已正确加载(你当前的字体链接已满足)
- 在页面的
<style>标签中添加以下样式:
.MJX-tex-frak { font-family: 'Fraktur', serif !important; font-weight: 600 !important; }
修改后的完整代码:
<!DOCTYPE html> <html lang="fr"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://fonts.cdnfonts.com/css/latin-modern-roman?styles=545,547,551,553,544,546,550,549,548,543,542,552" rel="stylesheet"> <link href="https://db.onlinewebfonts.com/c/50a215f3bc37263edc9265072a4022d8?family=Fraktur" rel="stylesheet"> <style> * { font-family: 'LMRoman10'; } kbd { background-color: #DDDDDD; font-family: monospace; font-weight: 200; color: black; padding: 2px 4px; text-align: center; border-radius: 2px; } /* 新增:覆盖MathJax Fraktur字体样式 */ .MJX-tex-frak { font-family: 'Fraktur', serif !important; font-weight: 600 !important; } </style> <script type="text/x-mathjax-config"> MathJax.Hub.Config({ "HTML-CSS": { preferredFont: "TeX", availableFonts: ["STIX", "TeX"], linebreaks: { automatic: true }, EqnChunk: (MathJax.Hub.Browser.isMobile ? 10 : 50) }, tex2jax: { inlineMath: [["$", "$"], ["\\(", "\\)"]], displayMath: [["$$", "$$"], ["\\[", "\\]"]], processEscapes: true, ignoreClass: "tex2jax_ignore|dno" }, TeX: { extensions: ["begingroup.js"], noUndefined: { attributes: { mathcolor: "red", mathbackground: "#FFEEEE", mathsize: "90%" } }, Macros: { href: "{}" }, }, messageStyle: "none" }); </script> <script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS_HTML-full"></script> </head> <body> <h1>Customizing <kbd>\mathfrak</kbd> in MathJax</h1> <h2>The normal and default result</h2> By default, the font used by the <kbd>\mathfrak</kbd> command looks like this<br> $\mathfrak{ABCDEFGHIJKLMNOPQRSTUVWXYZ~~abcdefghijklmnopqrstuvwxyz~~0123456789}$ <h2>The expected result</h2> However, I want to change the font for <kbd>\mathfrak</kbd> to this one<br> <span style="font-family:Fraktur;font-weight:600">ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz 0123456789</span> <h2>Example: Writing a mathematical expression</h2> For example, I want to write this \[z = \mathfrak{Re}(z) + i\mathfrak{Im}(z)\] Where $\mathfrak{Re}$ and $\mathfrak{Im}$ should render to something like this <span style="font-family:Fraktur;font-weight:600;">Re Im</span> </body> </html>
方案二:重新定义\mathfrak命令
通过MathJax的宏定义功能,将\mathfrak替换为带自定义样式的HTML元素。这种方法可能存在排版对齐问题(HTML元素与数学公式基线不一致),需自行调整。
修改步骤:
在MathJax配置的TeX.Macros中添加\mathfrak的自定义定义:
Macros: { href: "{}", mathfrak: ["<span style='font-family:Fraktur;font-weight:600;vertical-align: baseline;'>#1</span>", 1] }
注意事项:
- 添加
vertical-align: baseline可缓解排版对齐问题,但仍可能因字体大小差异出现偏移 - 若公式中存在复杂嵌套,该方法可能导致渲染异常
内容的提问来源于stack exchange,提问作者Pierrot Ballay-Legrand
相关产品推荐
相关产品推荐

