-
Notifications
You must be signed in to change notification settings - Fork 13.5k
feat(webui): improve LaTeX rendering with currency detection #16508
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(webui): improve LaTeX rendering with currency detection #16508
Conversation
|
Hey @srogmann! Thank you for this contribution :) Yeah, we will definitely work sth out from these PRs :) |
7eb8914 to
9b31191
Compare
|
@ggerganov could u also please take a look at this and test your edge cases? @watamario15 @squik67 also some feedback & testing from you would be really useful :) |
|
Okay, thanks for reporting @ggerganov. I'd like to wait just a bit longer for @watamario15 and @squik67 to add their feedback and verify if there are more edge cases to be addressed before we introduce any further improvements to this. |
|
Thanks for the tests! I can reproduce the non-rendered formulas. The Example: working and non-working. I'll have a look at it. |
|
From my quick check, this doesn't get rendered: |
|
It's working well on my end with these ↑ commits. Thanks! (I found a small issue where GPT-OSS tends to escape |
|
Oh, it looks like erroring on \[
\boxed{
\begin{aligned}
N_{\text{att}}^{\text{(MHA)}} &=
h \bigl[\, d_{\text{model}}\;d_{k} + d_{\text{model}}\;d_{v}\, \bigr] && (\text{Q,K,V の重み})\\
&\quad+ h(d_{k}+d_{k}+d_{v}) && (\text{バイアス Q,K,V)}\\[4pt]
&\quad+ (h d_{v})\, d_{\text{model}} && (\text{出力射影 }W^{O})\\
&\quad+ d_{\text{model}} && (\text{バイアス }b^{O})
\end{aligned}}
\] |
|
@watamario15 Thanks! The rendering is now better, but the spacers (e.g. |
|
I went ahead and merged #16599 first because it was a quick fix for the immediate issue in #16598 – just converting On the other hand your PR solves a bigger problem that #16599 doesn't touch – distinguishing money from math. Right now, Since your Let me know your thoughts! |
|
@allozaur The quick fix in #16599 helps in a lot of cases. I wil rebase. Your samples in |
6f8bc9c to
688392e
Compare
|
Regarding the spacer example from @watamario15, I added a small fix and unit test. A sub-pattern like |
In real-world scenarios, models can emit mathematical expressions in both MathJax/Markdown and pure LaTeX styles. You should never have to deal with doubled backslashes The developer option “Show raw LLM output” displays the model’s raw output directly, without any Markdown rendering or frontend parsing, for streaming mode :
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall, very good improvements, just needs small polishing up before we merge it 😄
|
Rendering looks perfect on my end. BTW, I see a bunch of 404 logs, is this normal? |
Hmmm... this might be older assets cached by browser not being there. Just to make sure, please clear browsing data and recheck. |
|
I'm on |
@srogmann could u please take a look at that? |
|
You should never have to deal with doubled backslashes \( or \[ in the WebUI Have a look at the example of @watamario15: |
|
@watamario15 I can also reproduce these 404 errors. |
|
@allozaur Regarding the 404 errors: I inserted in In vite.config.ts I added a replacement of the URLs by base64-encoded data. Since I'm not deeply familiar with Svelte or Vite, please review the changes to ensure they are correct. I am sure that there is a better way to set $use-ttf to false ;-). This will increase the size of index.html.gz, but it ensures that llama.cpp remains local (no CDN dependency). |
Quick note on the regex approach
I think that we have two simple, safer options
vite.config.ts (scss additionalData): css: {
preprocessorOptions: {
scss: {
additionalData: `
// override KaTeX defaults
$use-woff2: true;
$use-woff: false;
$use-ttf: false;
$font-folder: "/fonts";
`,
},
},
}Then import KaTeX SCSS (e.g.
vite plugin example: // vite.plugins/strip-katex-fonts.ts
import type { Plugin } from 'vite';
export default function stripKatexFonts(): Plugin {
return {
name: 'strip-katex-fonts',
enforce: 'post',
transform(code, id) {
if (!/node_modules\/katex\/.*\.css($|\?)/.test(id)) return null;
// remove only @font-face blocks that reference .ttf or .woff (preserve woff2)
const cleaned = code.replace(
/@font-face\s*{[^}]*?url\([^)]+\.(?:ttf|woff)(?:\?[^\)]*)?\)[^}]*}/gi,
''
);
return { code: cleaned.replace(/^\s*\n/gm, ''), map: null };
},
};
}Add it to vite config plugins. Tiny test suggestion (quick CI guard)
If you prefer, I can:
Which path do you prefer? Or maybe you would like to do this change on your end? |
|
@allozaur I did try adding That said, your proposed solution 1 (compiling |
|
@srogmann I've pushed changes to your branch with this PR - srogmann#2 |
Code maintenance (variable names, code formatting, string handling) Co-authored-by: Aleksander Grygier <[email protected]>
Moves KaTeX styling to SCSS for better customization and font embedding. This change includes: - Adding `sass` as a dev dependency. - Introducing a custom SCSS file to override KaTeX variables and disable TTF/WOFF fonts, relying solely on WOFF2 for embedding. - Adjusting the Vite configuration to resolve `katex-fonts` alias and inject SCSS variables.
f61bf52 to
d8b0b11
Compare
|
@allozaur Thanks! I have cherry-picked your commits from srogmann#2 into this PR. |


Close #16136
This PR handles LaTeX blocks in (...) and [...], as well as dollar amounts like $100 that should not be interpreted as inline LaTeX.
Examples (e.g. prompt "Think of a grade 4 math question regarding amounts."):
[
\left{ \begin{pmatrix} 1 & 0 \ 0 & 1 \end{pmatrix}, \begin{pmatrix} -1 & 0 \ 0 & -1 \end{pmatrix} \right} = {\pm I}
]
@allozaur : I wrote most of this PR a week ago, only just now noticed issue #16136 and your PR #16496. In math-formulas.ts, I've added examples for Storybook, along with tests for Vitest. Feel free to take a look and let me know your thoughts. Detecting inline LaTeX is inherently tricky, so my approach likely won't cover every edge case, but I hope it's a step in the right direction.