Math and AI workarounds

There are new suggestions from the openai community regarding the problems I am experiencing. Are you planning to make an update about this? suggestions are as follows.

Suggestion 1;

def parse_stream_to_katex(stream: Stream):
    """
    Takes an OpenAI Stream and replaces ChatGPT LaTeX delimiters
    with KateX ones.
    Yields text, not chunks
    """
    last_text = ""
    for chunk in stream:
        text = chunk.choices[0].delta.content
        if text:
            # Sometimes delimiters like \( can be split over two chunks.
            # If the previous chunk ended in \, prepend that to this chunk
            if last_text.endswith("\\"):
                text = last_text + text

            text = (
                text.replace(r"\[", "$$")
                .replace(r"\]", "$$")
                .replace(r"\(", "$")
                .replace(r"\)", "$")
            )
            last_text = text

            # If the text ends in \, we don't return it, we'll get include it in the next chunk
            if not text.endswith("\\"):
                yield text

Suggestion 2:

// MarkdownLaTeXRenderer.js
import React from 'react';
import ReactMarkdown from 'react-markdown';
import remarkGfm from 'remark-gfm';
import remarkMath from 'remark-math';
import rehypeRaw from 'rehype-raw';
import rehypeKatex from 'rehype-katex';

const MarkdownLaTeXRenderer = ({ content }) => {
    // Replace \[ with $$ and \] with $$ to ensure compatibility
    const processedText = content 
      .replace(/\\\[/g, '$$$')  // Replace all occurrences of \[ with $$
      .replace(/\\\]/g, '$$$') // Replace all occurrences of \] with $$
      .replace(/\\\(/g, '$$$')  // Replace all occurrences of \( with $$
      .replace(/\\\)/g, '$$$'); // Replace all occurrences of \) with $$

    const remarkMathOptions = {
        singleDollarTextMath: false,

};




return (
    <ReactMarkdown
        className="markdown-content"
        children={processedText}
        remarkPlugins={[[remarkMath, remarkMathOptions], remarkGfm]} // Pass options as the second element of the array
        rehypePlugins={[rehypeRaw, rehypeKatex]} // Include rehypeRaw for HTML, rehypeKatex for LaTeX
    />
);

};

export default MarkdownLaTeXRenderer;
’

1 Like

And as an end user… I’m using system prompt for that (OpenAI).

When math is in own line or chapter, change \( and \) → at own lines, before and after, $$ is quite reliable.

But when same should do inline with single $… that fails almost every time.

If that could happen automatic… even better.

1 Like

When I view mathematical formulas on my mobile device, I cannot see the entire formula correctly (the formula is too long). Does anyone know how to solve this issue?
image
Below is the correct display on the PC .
image

Turning phone horizontally? But why it is quoted, and can that be the issue?

Otherwise. I asked from AI of my forum and it answered this (no idea if it is helpful or pure crap).

Solutions for Aligning Long Equations Using MathJax AI

Jagster:

The forum utilizes MathJax for mathematics and equations. Occasionally, an equation is so long that it does not fit on a mobile screen. Is there a way to somehow break the equation into multiple lines?

Asiavastaaja:

MathJax does not directly support automatic line breaking for equations, but you can use some techniques to make long equations fit better on mobile screens. Here are a few tips:

  1. Use the align environment: You can split the equation into multiple lines using the align environment. This allows you to align and distribute parts of the equation across several lines. For example:

    $$
    \begin{align}
    a + b + c + d + e + f + g + h + i + j + k + l + m + n + o + p + q + r + s + t + u + v + w + x + y + z = 0
    \end{align}
    $$
    
  2. Use the split environment: This is another way to break an equation into multiple lines. It is particularly useful when you want to split the equation into parts without needing alignment. For example:

    $$
    \begin{split}
    a + b + c + d + e + f + g + h + i + j + k + l + m + n + o + p + q + r + s + t + u + v + w + x + y + z = 0
    \end{split}
    $$
    
  3. Use the multline environment: This environment is designed for long equations that do not fit on a single line. It breaks the equation into multiple lines without alignment. For example:

    $$
    \begin{multline}
    a + b + c + d + e + f + g + h + i + j + k + l + m + n + o + p + q + r + s + t + u + v + w + x + y + z = 0
    \end{multline}
    $$
    

These environments allow you to control the presentation of equations and ensure they are readable even on smaller screens. Remember that MathJax environments often require the amsmath package, which is usually enabled by default in MathJax.

@Jagster These should be good solutions. I originally wanted to set a horizontal scrollbar for the formula, but it was not successful.

.math-tex {
    overflow-x: auto;
    max-width: 100%;
    padding: 5px 0;
    display: block;
}

I’m having trouble copying math formulas from Google Sheets into Meta Discourse. Don’t the formulas seem to be rendering correctly?

NPV=CFt * [1-1(1+r)Tr]- CFo= 84,000 * [1-1(1+13%)713%]- 385,000 = $-13,500

PI = 1 + NPVCFo = 1+ n=1784,000(1,13)n - 385,000385,000 ~ 0,965

I think you need to escape $ and % with \.
NPV=CFt * [1-1(1+r)Tr]- CFo= 84,000 * [1-1(1+13\%)713\%]- 385,000 = \$-13,500

Let’s see here:

NPV=CFt * [1-1(1+r)Tr]- CFo= 84,000 * [1-1(1+13\%)713\%]- 385,000 = \$-13,500
PI = 1 + NPVCFo = 1+ n=1784,000(1,13)n - 385,000385,000 ~ 0,965


It renders OK, but there is no scrollbar to see the whole math. There is, in the preview, though.
In the post, you can only see the other part if you zoom:

In the preview plane whilst drafting a post, when the SVG gets rendered the preview plane scrolls up. Is there a setting that can be changed so the scrolling returns to lowest position?

Having to scroll down every time i edit math annoys my lecturers, but i have been coping

I’ve notice a couple of “deprecate” in the activity.

If someone could please explain what this means for an instance with a large databases of math that renders ok

after a full update/rebuild, the math seems to be rendering fine

For those that haven’t heard, this plugin is now bundled with core. Please see rebuild/revision 2 of 3 of this post to show this.

In the deed! :+1: Sorry for causing concern. You should be good to go now.

:partying_face: This plugin is now bundled with Discourse core as part of Bundling more popular plugins with Discourse core. If you are self-hosting and use the plugin, you need to remove it from your app.yml before your next upgrade.

3 Likes

I have a math character problem in the AI answers. It gives me answers in latex format even though I have typed the required prompt. This creates an image as if there are broken characters. Can you solve this problem. Example;

\( A = \begin{pmatrix} x & -1 \\ z & 2y \end{pmatrix} \)

\[
\boxed{
x = 0, \quad y = 4, \quad z = 4
}
\]

\[
\begin{cases}
A_{11} = B^T_{11} \Rightarrow x = y - z \\
A_{12} = B^T_{12} \Rightarrow -1 = -1 \quad (\text{benar otomatis}) \\
A_{21} = B^T_{21} \Rightarrow z = y \\
A_{22} = B^T_{22} \Rightarrow 2y = 8 \\
\end{cases}
\]

I have previously shared solutions from the openai community

Hi @ozkn

I too have this problem. My recommendation would be

  • sign up for an account with the AI provider
  • tell the AI account to

remember for future answers that Discourse is compatible with MathJax/Katex $…$ $$…$$ inline or multi-line centric formatting.

  • you should find your output more copy-paste’able when logged into your AI account
you may need to keep pushing for the best memory
3 Likes

Just to add to the last point — if you’re mixing math and currency, remember:

  • $...$ = math
  • \$ = literal dollar sign

So:
$x+1$ costs \$5. → renders math for x+1 and shows the price as $5.

That’s really all you need: math with $, money with \$.

2 Likes

another good one to encourage chatGPT to remember is that you can’t render math in details pane titles

none of these hacks are needed anymore, we support this natively.

1 Like