Pages (Mac) has native LaTeX support. If you input COMMAND-OPTION-E, a window for inputting an equation appears, and if you put an equation there, the equation image is rendered.

I already have math problems typed in LaTex. E.g,

"In the matrix equation above, if $x=4,$ what is the value of $y?$"

I copy and paste these strings into Pages first, then

Copy only x=4, from $x=4,$ and enter it into the equation input window (then click the Insert button)

Copy only y? from $y?$ and enter it into the equation input window (then click the Insert button)

Continue similarly.

Could this be done into a macro with Keyboard Maestro?
I was successful until 1) using regular expressions, but I am not sure how to continue 2) 3) and so on. Maybe it's because I just learned the keyboard maestro.

I am waiting for advice from KM teachers.
Thank you.

The first action is not strictly necessary, but all it does is it brings Pages to the front.

The macro assumes you have already copied your LaTeX expression (e.g. $x=4,$) in the normal way. It then extracts the part you are interested in - x=4, - and then inserts that as an equation into Pages.

By studying what I have done you may be able to modify it to suit your needs better: as I say, it's really just a starting point.

The main thing is:

It shows how to extract information using a capture group in a regular expression using the KM Search Using Regular Expression action.

it gives you an idea as to how to manipulate Pages using in-built KM actions.

However, when I applied the macro you sent, only First Match "x=4," came out and the macro ends.
As stated in the original article, I want the Second Match "y?" to appear sequentially. I searched the internet and asked me to use "For each", but I am not familiar with it yet, so I cannot use it well. Thanks for the advice.

This way, you can safely copy only the equation from $Equation$ and paste it into the equation window on the pages. But in this case, it is difficult for me to complete step 4). The cursor does not stop and continues to move to the end of the document. Is there any action that can make 4) possible?

But it's not, is it, for what I suggested (namely paste the whole thing without dollar signs into the equation editor in Pages) will not render properly.

What you actually need for the text to be pasted into Pages unchanged, but the maths expressions to be entered via the equation editor.

I'm just trying to clarify what it is you're trying to achieve. To take your example

$math1$text1$math2$text2

Is this what you want to happen:

Enter math1 via the equation editor

Paste the text1 into Pages

Enter math2 via the equation editor

Paste the text2 into Pages

To be absolutely certain, it would be better if you posted actual examples of

(a) the text that you start with, and
(b) what it looks like in Pages after it's been properly entered

Tangent to your question but clarifying the method in LaTeX ... punctuation marks (comma and question mark) should be rendered outside of the equation limits not inside them. So $x = 4$, not $x= 4,$.

I’ve now looked at your example and I have figured out a way to achieve what you want.

Well - that’s the good news!

The bad news is that it is so complicated there must be different, simpler ways of solving the same problem. It’s just that I currently don’t have enough spare time and brain power to go further.

So, I’m hoping some kind (and canny) soul will step in and have a shot at helping you and we can both learn a bit more.

I might hazard a guess that the KM conversion will need a loop that pulls apart a sequences of (regular text + LaTeX) + (regular text + LaTeX) + ... The command will paste the regular text into Pages. At each LaTeX, the command will have to open the equation editor in Pages, enter the text, and exit the editor (invoking it). Repeat this until the end of the text buffer.

When this is to be done on more than one set of inputs, I have to wonder why not just use a LaTeX compiler directly?

That's a bird's-eye view of what's needed: I've already worked out how to identify where each LaTeX element is by locating the dollar signs (and therefore where each text element is) and written a macro to do that but the iterating loop that then extracts these elements turns out to be very fiddly. But that's my take on it and I was wondering if there might be another, simpler approach that someone can suggest. It seems to me that while KM can process text sequentially, it's not necessarily something that KM makes easy to do!

I'm not really familiar with LaTeX so your suggestion to use a LaTeX compiler sounds to me like that could be the more straightforward approach.

This is a discussion worth a thread on its own. Suffice to say that, the (highly) recommended on-line compilers are OverLeaf or ShareLaTeX, and the macOS local installation is (so easy, a caveman can do it) found at MacTex.

I think perhaps that the OP is trying to get out of investing time to learn LaTeX and instead wants a parsing tool to be able to copy+paste someone else's LaTeX work to his favorite WYSIWG text editor (Pages).

But, that is also a subject for its own thread (not here).

I have also considered using LaTex compilers like "Lyx" or "Overleaf" before, but I have come to the conclusion that these compilers are more suitable for writing "Professional" math or scientific papers.

The document I wanted to create had to be in a more free form with pictures, text, and equations, so I considered "Pages".

Perhaps even with a direct LaTex compiler, various images, texts, and formulas can be freely implemented like a textbook that elementary school children see, so I will study more.

To use the macro, you must copy the raw source text into the clipboard and then trigger the macro; I've set it to trigger on [hyper]-; but of course you can set it to whatever you like.

The macro will then activate Pages, to make sure it's at the front and then do it's stuff.

EDIT: I was just playing around with this and discovered an omission such that if there was plain text after the last LaTeX element it would not be output to Pages, so I've updated the macro to fix this.

This just goes to show that you'll need to test this on as many examples as possible!

FWIW, in such cases, I use Curio with LaTeXiT (although Curio supports LaTeX equations as well). Curio has a much wider range of options for types of content that can be added to the page and a broader definition of "free form layout".

Thank you very much for telling me a very fantastic editor.

I visited the site you mentioned, downloaded the program, and tried it for hours. And, unfortunately, I have identified a few trivial but important facts that the program does not fully support LaTex.

In particular, there was something I wasn't happy with about inline LaTex. It is more of a problem with my LaTex code itself than a problem with the program.

Anyway, I came back to Pages. It was a short but intense tour.
Thank you.