Skip to content
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

Extended parsing support for LaTeX #42

Closed
snorky22 opened this issue Nov 1, 2022 · 10 comments
Closed

Extended parsing support for LaTeX #42

snorky22 opened this issue Nov 1, 2022 · 10 comments
Assignees
Labels
Bug Something isn't working Ready for release An issue where a new functionality or a bug is finished and will be released in the next version. Verified A issue there has been confirmed by the devs

Comments

@snorky22
Copy link

snorky22 commented Nov 1, 2022

Is your feature request related to a problem? Please describe.
I appreciate the LaTeX support. It lets KaTeX or MathJax process inline equations, starting with \(and ending with \). It would be nice to be able to display displaymath equations, i.e. code starting with \[and ending \].

Describe the solution you'd like
When mathis set to true, let the \[ and \] be passed to the HTML code for the LaTeX interpreter to use them.

@BenjaminHoegh
Copy link
Owner

This should already be supported? what result do you get when using displaymath?

@snorky22
Copy link
Author

snorky22 commented Nov 2, 2022

The block math code I have is

  // Block Math
        $state = $this->options['math'] ?? false;
        if ($state !== false) {
            $this->BlockTypes['\\'][] = 'Math';
            $this->BlockTypes['$'][] = 'Math';
        }

It seems that it should use a double dollar $$. Do you think replacing ['$']by ['$$'] would make the thing work for displaymath?

@snorky22
Copy link
Author

snorky22 commented Nov 3, 2022

I have made the previous modification and the math between $$and $$was not rendered.

@BenjaminHoegh
Copy link
Owner

The reason for $this->BlockTypes['\\'][] = 'Math'; is do to how Parsedown works, in this case it should detect \ and then activate the regular expression to see if the following are a match there should be formatted.

So for the sake of $$ we use $this->BlockTypes['$'][] = 'Math'; to find every $ in our text, then running the regular expression, lets say ^\$\$, from the position of the matched dollar sign, if it also match the expression we format it.

As for your issue, will it be possible for you to send a copy of the markdown that curse this issue?

@snorky22
Copy link
Author

snorky22 commented Nov 8, 2022

I have the following versions:

  • Parsedown: version 1.8.0-beta-7
  • ParsedownExtra: version 0.8.1
  • ParsedownExtended: version 1.1

Math enabled simple case

The creation code is

	$Extra = new ParsedownExtended([
	"math" => true,
 	"toc" => true,
    "typographer" => true,
    "sup" => true,
    "sub" => true
	]);

The markdown is

Let us display an differential equation in integral form:

\[x(t)=x_{0}+\int_{0}^{t}f(x(s))ds\]

Now let us put it into text by using the more compact differential form \(\frac{dx}{dt}=f(x)\;\;x(0)=x_{0}\). 

The HTML I get is
simple Math

The inline math is rendered by MathJax, but the delimiters \[ and \] are not passed to the final HTML.

Math with dollars

The creation code is

	$Extra = new ParsedownExtended([
	 "math" => [
        "single_dollar" => true
    ],
 	"toc" => true,
    "typographer" => true,
    "sup" => true,
    "sub" => true
	]);

The markdown is

Let us display an differential equation in integral form:

$$x(t)=x_{0}+\int_{0}^{t}f(x(s))ds$$

Now let us put it into text by using the more compact differential form $\frac{dx}{dt}=f(x)\;\;x(0)=x_{0}$. 

The HTML I get is
Math with dollar delimiters

The double quotes should not be there.

Best regards,
François

@BenjaminHoegh
Copy link
Owner

Just a quick question, if you add a extra \ before \[ do it then work? If so it will be a issue with ParsedownExtended that I need to fix

@snorky22
Copy link
Author

snorky22 commented Nov 9, 2022

I have tried \\[x(t)=x_{0}+\int_{0}^{t}f(x(s))ds\], \\[x(t)=x_{0}+\int_{0}^{t}f(x(s))ds\\] and \\\[x(t)=x_{0}+\int_{0}^{t}f(x(s))ds\\\].
The first one returns [x(t)=x{0}+\int{0}^{t}f(x(s))ds]in the browser, the second one returns the same thing, and the last one returns \[x(t)=x{0}+\int{0}^{t}f(x(s))ds\].
FYI, the HTML code returned from the standard code \[x(t)=x_{0}+\int_{0}^{t}f(x(s))ds\](in markdown) is the HTML

<p>
"[x(t)=x"
<em>{0}+\int</em>
"{O}^{t}f(x(s))ds]"
</p>

@BenjaminHoegh BenjaminHoegh added Bug Something isn't working In Progress In progress Verified A issue there has been confirmed by the devs labels Nov 10, 2022
@BenjaminHoegh
Copy link
Owner

Can confirm that in Parsedown 2.0 this is no longer a issue :)

I will see if I can back forward this to the current version

@snorky22
Copy link
Author

I have cloned your repo and switched to version 2, run composer on it.

Here is a minimal test HTML code

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
    <script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
    <title>Test Parsedown</title>
</head>
<body>
<h1>Mathjax test</h1>
<div>
    \[x(t)=x_{0}+\int_{0}^{t}f(x(s))ds\]
</div>
<h1>Parsedown test</h1>
<?php

require __DIR__.'/vendor/autoload.php';
require 'src/ParsedownExtended.php';
use BenjaminHoegh\ParsedownExtended;
use Erusev\Parsedown\Parsedown;

$Extra = new Parsedown(new ParsedownExtended\ParsedownExtended());
$theHTML = $Extra->toHtml(' \[x(t)=x_{0}+\int_{0}^{t}f(x(s))ds\]');
$theHTML .= '<br>'.$Extra->toHtml(' \(x(t)=x_{0}+\int_{0}^{t}f(x(s))ds\)');
echo '<div>'.$theHTML.'</div>';
?>
</body>
</html>
<?php

Mathjax itself renders the displayMath correctly.

ParsedownExtended renders the inline Math, but not the display math.

By the way, can we use the new ParsedownExtra in this setup?

Cheers

@BenjaminHoegh
Copy link
Owner

Sorry, didn't see your comment, yes you can use ParsedownExtra, the extension will automatic switch to use ParsedownExtra as base

@BenjaminHoegh BenjaminHoegh self-assigned this Dec 31, 2023
@BenjaminHoegh BenjaminHoegh added Ready for release An issue where a new functionality or a bug is finished and will be released in the next version. and removed In Progress In progress labels Jan 1, 2024
BenjaminHoegh added a commit that referenced this issue Jan 1, 2024
- Extended parsing support for LaTeX #42
- Setter for $options #45
- Dynamic properties is deprecated #44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working Ready for release An issue where a new functionality or a bug is finished and will be released in the next version. Verified A issue there has been confirmed by the devs
Projects
None yet
Development

No branches or pull requests

2 participants