Skip to content

Commit bf980a7

Browse files
committed
Add custom <tex> tag?
1 parent 0d666a2 commit bf980a7

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ Add the following to your `pubspec.yaml` file:
101101
| `samp` | `section` | `small` | `span`| `strike` | `strong`| `sub` | `sup` | `summary` | `svg`| `table`|
102102
| `tbody` | `td` | `template` | `tfoot` | `th` | `thead` |`time` | `tr` | `tt` | `u` | `ul` |
103103
| `var` | `video` | `math`: | `mrow` | `msup` | `msub` | `mover` | `munder` | `msubsup` | `moverunder` | `mfrac` |
104-
| `mlongdiv` | `msqrt` | `mroot` | `mi` | `mn` | `mo` | | | | | |
104+
| `mlongdiv` | `msqrt` | `mroot` | `mi` | `mn` | `mo` | `tex` (custom tag for this package) | | | | |
105105

106106

107107
## Currently Supported CSS Attributes:
@@ -671,6 +671,14 @@ If the parsing errors, you can use the [onMathError](#onmatherror) API to catch
671671

672672
If you'd like to see more MathML features, feel free to create a PR or file a feature request!
673673

674+
### Tex
675+
676+
If you have a Tex string you'd like to render inside your HTML you can do that!
677+
678+
Use the custom `<tex>` tag inside your HTML, and place your **raw** Tex string inside. The package uses the same logic as MathML rendering above, but doesn't need to parse the string, of course.
679+
680+
The rendering error will also be called if your Tex string fails to render.
681+
674682
### Table
675683

676684
This package renders table elements using the [`flutter_layout_grid`](https://pub.dev/packages/flutter_layout_grid) plugin.

example/lib/main.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class MyHomePage extends StatefulWidget {
2727
_MyHomePageState createState() => new _MyHomePageState();
2828
}
2929

30-
const htmlData = """
30+
const htmlData = r"""
3131
<h1>Header 1</h1>
3232
<h2>Header 2</h2>
3333
<h3>Header 3</h3>
@@ -228,6 +228,8 @@ const htmlData = """
228228
<mo>=</mo>
229229
<mn>1</mn>
230230
</math>
231+
<h3>Tex Support with the custom tex tag:</h3>
232+
<tex>i\hbar\frac{\partial}{\partial t}\Psi(\vec x,t) = -\frac{\hbar}{2m}\nabla^2\Psi(\vec x,t)+ V(\vec x)\Psi(\vec x,t)</tex>
231233
""";
232234

233235
class _MyHomePageState extends State<MyHomePage> {

lib/src/html_elements.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ const REPLACED_ELEMENTS = [
8787
"rt",
8888
"ruby",
8989
"math",
90+
"tex",
9091
];
9192

9293
const LAYOUT_ELEMENTS = [

lib/src/replaced_element.dart

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,12 +272,14 @@ class MathElement extends ReplacedElement {
272272
MathElement({
273273
required this.element,
274274
required this.texStr,
275-
String name = "math",
275+
String name = "[[MathElement]]",
276276
}) : super(name: name, alignment: PlaceholderAlignment.middle, style: Style(display: Display.INLINE));
277277

278278
@override
279279
Widget toWidget(RenderContext context) {
280-
texStr = parseMathRecursive(element, r'');
280+
if (element.localName == "math") {
281+
texStr = parseMathRecursive(element, r'');
282+
}
281283
return Container(
282284
width: MediaQuery.of(context.buildContext).size.width,
283285
child: Math.tex(
@@ -424,6 +426,11 @@ ReplacedElement parseReplacedElement(
424426
element: element,
425427
texStr: r'',
426428
);
429+
case "tex":
430+
return MathElement(
431+
element: element,
432+
texStr: element.text,
433+
);
427434
default:
428435
return EmptyContentElement(name: element.localName == null ? "[[No Name]]" : element.localName!);
429436
}

0 commit comments

Comments
 (0)