saminsohag / flutter_packages

9 stars 2 forks source link

Markdown doesn't show $x$ and \(z\) at the same time #13

Closed mahdiyarz closed 3 weeks ago

mahdiyarz commented 3 weeks ago

First of all I need to thank you for the great job.

As you see in the title I need to show both syntax at the same time in one String value. e.g.

$x$ + \(b\) = $y$
saminsohag commented 3 weeks ago

Actually this was an intended behaviour. You can't use both \(..\) and $..$ at same time. But you can achieve both \(..\) and $..$ syntax at the same time using code given bellow

String tex = r'\(x^2\) $x^2$';
tex = tex
          .replaceAllMapped(
              RegExp(
                r"(?<!\\)\$\$(.*?)(?<!\\)\$\$",
                dotAll: true,
              ),
              (match) => "\\[${match[1] ?? ""}\\]")
          .replaceAllMapped(
              RegExp(
                r"(?<!\\)\$(.*?)(?<!\\)\$",
              ),
              (match) => "\\(${match[1] ?? ""}\\)");
      tex = tex.splitMapJoin(
        RegExp(r"\[.*?\]|\(.*?\)"),
        onNonMatch: (p0) {
          return p0.replaceAll("\\\$", "\$");
        },
      );

The pas the tex to TexMarkdown widget.