Conversion pages (Mac) from Inline LaTex document using Keyboard Maestro macro

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

  1. Copy only x=4, from $x=4,$ and enter it into the equation input window (then click the Insert button)
  2. Copy only y? from $y?$ and enter it into the equation input window (then click the Insert button)
  3. 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.

Hi @11112, I hope I can offer you some help.

I've created a simple macro for you to start with. You can download it here Test Pages with LaTeX.kmmacros (2.8 KB)

It looks like this:

KM 1 2020-10-08 at 10.11.48

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:

  1. It shows how to extract information using a capture group in a regular expression using the KM Search Using Regular Expression action.
  2. it gives you an idea as to how to manipulate Pages using in-built KM actions.

Hope that helps and have fun!

Thank you very much for the great help. :slight_smile:

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.

Trying it in another way. (I think writing a new article would be a waste of space, so I reply to my original article.)

I wrote the macro in the following way.

  1. Search for $ by "find".
  2. Move the cursor one space to the left of $.
  3. The cursor keeps moving to the right while holding the shift button
  4. Move until a new $ mark appears, and the cursor stops as soon as $ passes

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?

Thank you.

That's correct.

You posted

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

and I understood you to mean you were first doing the x expression and then you were doing the y one, in two separate steps, not as one step.

So, what you want is to be able to have something like

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

in the clipboard and paste that into the equation facility in Pages, but without the $ signs - is that correct?

Yes, that's right. I cannot but admit that the description in my original article was insufficient.

$math$text$math$text

In this way, I want all the $ signs to disappear and the math to be rendered well.

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:

  1. Enter math1 via the equation editor
  2. Paste the text1 into Pages
  3. Enter math2 via the equation editor
  4. 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

I you can supply that then so much the better!

First of all, thank you. I will explain through the picture.

The picture below is the math problem given to me at first

The output after the macro should look like this:

LaTex source is as follows:

$\left[\begin{matrix} 2 & 4 & 1 \ 3 & x & 7\end{matrix}\right]+\left[\begin{matrix} -2 & 5 & 4 \ 1 & 2 y & 3\end{matrix}\right]=\left[\begin{matrix} 0 & 9 & 5 \ 4 & 6 & 10\end{matrix}\right]$

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

(A) $-6$
(B) $-2$
(C) $1$
(D) $2$
(E) $6$

In a nutshell, it may sound a bit abstract, but I want the "$math$" to disappear sequentially, turning it all into the rendered "math".

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,$.


JJW

Oh, thank you!!~

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?

--
JJW

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).

--
JJW

1 Like

Thank you for providing a new perspective.

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.

Thank you sincerely. :slight_smile:

I am really grateful that I can learn some new features through the macros you provided at the beginning.

The way macros work sequentially seems to be harder than I thought before.

Even if it is not resolved soon, I think it is productive enough to ponder these problems.

If you have a good idea or solution, please feel free to tell us. I will post more. Thank you.

1 Like

If I get more time I will be sure to add to this discussion with a view to being more helpful :slightly_smiling_face:

OK - I'm not one to let go easily so I took a little time out to tackle the problem and I have, I hope, a solution for you!

Here is a screen shot of Pages showing the input to my macro and the resulting output from it. I think it looks passable.


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.

Here is a copy of the macro: Pages with LaTeX.kmmacros (33.5 KB)

This is what the macro looks like, but I am not going to explain it as (a) it's complicated and (b) I am pressed for time!

I hope this gives you many ideas should you wish to modify and/or improve it!

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!

2 Likes

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".

--
JJW

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.