M2 icon indicating copy to clipboard operation
M2 copied to clipboard

M2 in papers: beautification of `M2-merge`

Open antonleykin opened this issue 5 years ago • 19 comments

@DanGrayson wrote https://github.com/Macaulay2/M2-merge to automate insertion of M2 input/output in LaTeX articles. It would be nice to have the output produced by texMath and automatically processed.

@pzinn, that would be similar to what https://github.com/pzinn/Macaulay2Web does.

antonleykin avatar Mar 01 '21 17:03 antonleykin

Sounds like a good idea. I'll think about it.

pzinn avatar Mar 09 '21 11:03 pzinn

proof of concept: given this (where code is in verbatim)

notes.pdf

the script (written in m2) outputs this:

notes-parsed.pdf

is this what you had in mind?

pzinn avatar Dec 01 '21 15:12 pzinn

Yep, this looks nice!

antonleykin avatar Dec 01 '21 21:12 antonleykin

working on it: https://github.com/pzinn/M2inTeX

pzinn avatar Dec 02 '21 04:12 pzinn

oh, I just realize in the title it referred to M2-merge. I just wrote this from scratch, no relation to M2-merge.

pzinn avatar Dec 02 '21 04:12 pzinn

Would be nice if this worked nicely with the output of installPDF (see book.m2).

mahrud avatar Dec 02 '21 04:12 mahrud

@mahrud I've just realized something: there's no reliable way to match the input and the output of capture. for example, I can easily fool it by creating a multi-line input

f=i->(
i+1)

and then all examples will be shifted... I've tried it as part of a package documentation and sure enough it breaks the documentation. I'm having the same issue with my texparser since it's also based on capture.

pzinn avatar Dec 02 '21 07:12 pzinn

Why not?

i5 : s = "f=i->(\ni+1)\nf(2)"

o5 = f=i->(
     i+1)
     f(2)

i6 : separateM2output last capture s

o6 = {i1 : f=i->(         , i2 : f(2), i3 : }
           i+1)
                            o2 = 3
      o1 = f

      o1 : FunctionClosure

o6 : List

mahrud avatar Dec 02 '21 07:12 mahrud

the problem is not at the level of separateM2output. it's later when one tries to identify the input and the output. try to insert in documentation something like

Text
 test1
Example
 f=i->(
 i+1
 )
Text
 test2
Example
 xxxxxxxxxx

and installPackage, you should have the same problem I had I think?

pzinn avatar Dec 02 '21 11:12 pzinn

Oh I see what you mean. No, that's just a caveat of SimpleDoc, I believe. Try this:

document {Key => "hello", "test1", EXAMPLE "f=i->(\ni+1\n)\n", "test2", EXAMPLE "f(2)"}
help "hello"

The problem is that SimpleDoc doesn't know how to parse that example into multiple individual commands, so it defaults to splitting the lines by indentation groups, which isn't always correct. Try this, as the dual to the example you had:

doc ///
Key
  "bye"
Description
  Text
    "test1"
  Example
    1
      2
     3
    4
  Text
    "test2"
  Example
     5
///
help "bye"

It could probably have a couple more heuristics, like matching parenthesis and brackets, but I think my plan was to eventually skip the separation of the example inputs altogether and just throw it all into capture and separate the output from there. This should always work, I believe, but it'll take some work.

Related issue: https://github.com/Macaulay2/M2/issues/1894#issuecomment-769461615

mahrud avatar Dec 02 '21 12:12 mahrud

Ah, I remembered the problem with implementing this: in my last example above, you can capture example blocks 1..4 and 5 easily and separate the outputs into a list with 5 elements, but the problem is how to tell how many of the output lines need to go in the first example block in the documentation and how many in the second.

The main solution I can think of is to capture in two chunks: capture 1..4, save the state (dictionary, etc), make an example block, then recover the state and capture 5 and make the second example block.

Does that make sense? I don't have a clean solution to this, so for now kindly asking that people correctly indent their examples in SimpleDoc input seems vastly easier :stuck_out_tongue:

mahrud avatar Dec 02 '21 12:12 mahrud

I agree that I intentionally messed up the indentation. It does seem that the clean solution would be to capture successive chunks, but maybe that's too complicated to implement, you would know better than I. I'll think of a workaround for my tex parsing code.

pzinn avatar Dec 02 '21 12:12 pzinn

After multiple rewrites, I think I found a pretty good way to deal with the issues above in https://github.com/pzinn/M2inTeX working pretty well, I'm using it for a course I'm giving now on computerized mathematical exploration...

pzinn avatar Mar 04 '22 07:03 pzinn

Looks pretty good, but I'm not sure I like all those blue rectangles.

DanGrayson avatar Mar 04 '22 19:03 DanGrayson

The nice thing about using the package listings is, you can format the output any way you like. e.g. if you don't like the blue rectangles in the example file, you can remove them by commenting out frame=leftline in the \lstset list of options.

pzinn avatar Mar 04 '22 23:03 pzinn

Can this be a Macaulay2 package?

DanGrayson avatar Mar 04 '22 23:03 DanGrayson

sure can. what would be a good name for it?

pzinn avatar Mar 05 '22 00:03 pzinn

Well, if it's a Macaulay2 package, it doesn't have to have Macaulay2 in its name -- maybe TeXmerge or MergeTeX.

DanGrayson avatar Mar 05 '22 00:03 DanGrayson

I think this can be closed now -- MergeTeX works decently.

pzinn avatar Dec 06 '23 00:12 pzinn