public inbox archive for pandoc-discuss@googlegroups.com
 help / color / mirror / Atom feed
* Extracting all equations (dropping the rest)
@ 2022-01-29 22:36 bapt a
       [not found] ` <235047d3-c000-4adb-8d61-e5b905475cc4n-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
  0 siblings, 1 reply; 5+ messages in thread
From: bapt a @ 2022-01-29 22:36 UTC (permalink / raw)
  To: pandoc-discuss


[-- Attachment #1.1: Type: text/plain, Size: 997 bytes --]

Hi,

I'm hoping to extract all equations from a latex document with a Lua 
filter, but I'm not having any luck.

Adapting an existing example I thought this might work,

function Pandoc(doc)
local hblocks = {}
for i,el in pairs(doc.blocks) do
if (el.t == "Math" ) then
table.insert(hblocks, el)
end
end
return pandoc.Pandoc(hblocks, doc.meta)
end

but nothing is returned, unfortunately. I also found this idea, 
https://stackoverflow.com/a/39963980

but it uses json (via Haskell) filters and I'd prefer something more 
portable with Lua.

Many thanks for any pointers,

baptiste






-- 
You received this message because you are subscribed to the Google Groups "pandoc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pandoc-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/235047d3-c000-4adb-8d61-e5b905475cc4n%40googlegroups.com.

[-- Attachment #1.2: Type: text/html, Size: 3249 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Extracting all equations (dropping the rest)
       [not found] ` <235047d3-c000-4adb-8d61-e5b905475cc4n-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
@ 2022-01-30  7:18   ` John MacFarlane
       [not found]     ` <m24k5lsnc7.fsf-d8241O7hbXoP5tpWdHSM3tPlBySK3R6THiGdP5j34PU@public.gmane.org>
  2022-01-30 11:20   ` Albert Krewinkel
  1 sibling, 1 reply; 5+ messages in thread
From: John MacFarlane @ 2022-01-30  7:18 UTC (permalink / raw)
  To: bapt a, pandoc-discuss

bapt a <auguieba-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> writes:

> Hi,
>
> I'm hoping to extract all equations from a latex document with a Lua 
> filter, but I'm not having any luck.
>
> Adapting an existing example I thought this might work,
>
> function Pandoc(doc)
> local hblocks = {}
> for i,el in pairs(doc.blocks) do

I think you want ipairs rather than pairs here...


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Extracting all equations (dropping the rest)
       [not found]     ` <m24k5lsnc7.fsf-d8241O7hbXoP5tpWdHSM3tPlBySK3R6THiGdP5j34PU@public.gmane.org>
@ 2022-01-30  9:04       ` bapt a
  0 siblings, 0 replies; 5+ messages in thread
From: bapt a @ 2022-01-30  9:04 UTC (permalink / raw)
  To: pandoc-discuss


[-- Attachment #1.1: Type: text/plain, Size: 1227 bytes --]

Thanks for the hint. There's another thing wrong with my script in terms of 
selecting Math only.

If I comment out 

   -- if (el.t == "Math" ) then

then I can see all the content in the output, showing me the for loop 
itself is working, but I'm not using the right selector to keep only the 
Math elements. 

Best regards,

baptiste



On Sunday, 30 January 2022 at 08:19:34 UTC+1 John MacFarlane wrote:

> bapt a <augu...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> writes:
>
> > Hi,
> >
> > I'm hoping to extract all equations from a latex document with a Lua 
> > filter, but I'm not having any luck.
> >
> > Adapting an existing example I thought this might work,
> >
> > function Pandoc(doc)
> > local hblocks = {}
> > for i,el in pairs(doc.blocks) do
>
> I think you want ipairs rather than pairs here...
>
>

-- 
You received this message because you are subscribed to the Google Groups "pandoc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pandoc-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/b4600d25-c981-4e42-b333-4d1d6e46f624n%40googlegroups.com.

[-- Attachment #1.2: Type: text/html, Size: 2043 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Extracting all equations (dropping the rest)
       [not found] ` <235047d3-c000-4adb-8d61-e5b905475cc4n-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
  2022-01-30  7:18   ` John MacFarlane
@ 2022-01-30 11:20   ` Albert Krewinkel
       [not found]     ` <87leyxmpgu.fsf-9EawChwDxG8hFhg+JK9F0w@public.gmane.org>
  1 sibling, 1 reply; 5+ messages in thread
From: Albert Krewinkel @ 2022-01-30 11:20 UTC (permalink / raw)
  To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw; +Cc: bapt a

I'd suggest a two-pass approach: first collect all math elements, then
replace the document with what you'd like to keep.

``` lua
_ENV = pandoc
local math_elements = List{}
return {
  -- first document pass
  {Math = function (m) math_elements:insert(m) end},
  -- second document pass
  {Pandoc = function (_) return Pandoc(math_elements:map(Plain)) end},
}

The above is untested and may contain errors, but should at least point
in the general direction.

The problem in the original draft is that Math elements are of type
Inline, so they are nested below blocks. It's why `el.t == "Math"` will
always be false. Fortunately we don't have to do the iteration
ourselves, but can just select Math elements directly, as done above.

Cheers,
Albert

bapt a <auguieba-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> writes:

>    Hi,
>
>    I'm hoping to extract all equations from a latex document with a Lua
>    filter, but I'm not having any luck.
>
>    Adapting an existing example I thought this might work,
>
>    function Pandoc(doc)
>    local hblocks = {}
>    for i,el in pairs(doc.blocks) do
>    if (el.t == "Math" ) then
>    table.insert(hblocks, el)
>    end
>    end
>    return pandoc.Pandoc(hblocks, doc.meta)
>    end
>    but nothing is returned, unfortunately. I also found this idea,
>    https://stackoverflow.com/a/39963980
>    but it uses json (via Haskell) filters and I'd prefer something more
>    portable with Lua.
>    Many thanks for any pointers,
>    baptiste
>
>    --
>    You received this message because you are subscribed to the Google
>    Groups "pandoc-discuss" group.
>    To unsubscribe from this group and stop receiving emails from it, send
>    an email to pandoc-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
>    To view this discussion on the web visit
>    https://groups.google.com/d/msgid/pandoc-discuss/235047d3-c000-4adb-8d6
>    1-e5b905475cc4n%40googlegroups.com.


-- 
Albert Krewinkel
GPG: 8eed e3e2 e8c5 6f18 81fe  e836 388d c0b2 1f63 1124


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Extracting all equations (dropping the rest)
       [not found]     ` <87leyxmpgu.fsf-9EawChwDxG8hFhg+JK9F0w@public.gmane.org>
@ 2022-01-30 18:53       ` bapt a
  0 siblings, 0 replies; 5+ messages in thread
From: bapt a @ 2022-01-30 18:53 UTC (permalink / raw)
  To: pandoc-discuss


[-- Attachment #1.1: Type: text/plain, Size: 2736 bytes --]

Wonderful, works a charm!
I had a hunch it might be something to do with a nesting level but I still 
haven't found a way to debug lua filters to see what's going on, so I 
quickly get stuck .

Many thanks,

baptiste

On Sunday, 30 January 2022 at 12:29:34 UTC+1 Albert Krewinkel wrote:

> I'd suggest a two-pass approach: first collect all math elements, then
> replace the document with what you'd like to keep.
>
> ``` lua
> _ENV = pandoc
> local math_elements = List{}
> return {
> -- first document pass
> {Math = function (m) math_elements:insert(m) end},
> -- second document pass
> {Pandoc = function (_) return Pandoc(math_elements:map(Plain)) end},
> }
>
> The above is untested and may contain errors, but should at least point
> in the general direction.
>
> The problem in the original draft is that Math elements are of type
> Inline, so they are nested below blocks. It's why `el.t == "Math"` will
> always be false. Fortunately we don't have to do the iteration
> ourselves, but can just select Math elements directly, as done above.
>
> Cheers,
> Albert
>
> bapt a <augu...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> writes:
>
> > Hi,
> >
> > I'm hoping to extract all equations from a latex document with a Lua
> > filter, but I'm not having any luck.
> >
> > Adapting an existing example I thought this might work,
> >
> > function Pandoc(doc)
> > local hblocks = {}
> > for i,el in pairs(doc.blocks) do
> > if (el.t == "Math" ) then
> > table.insert(hblocks, el)
> > end
> > end
> > return pandoc.Pandoc(hblocks, doc.meta)
> > end
> > but nothing is returned, unfortunately. I also found this idea,
> > https://stackoverflow.com/a/39963980
> > but it uses json (via Haskell) filters and I'd prefer something more
> > portable with Lua.
> > Many thanks for any pointers,
> > baptiste
> >
> > --
> > You received this message because you are subscribed to the Google
> > Groups "pandoc-discuss" group.
> > To unsubscribe from this group and stop receiving emails from it, send
> > an email to pandoc-discus...-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> > To view this discussion on the web visit
> > https://groups.google.com/d/msgid/pandoc-discuss/235047d3-c000-4adb-8d6
> > 1-e5b905475cc4n%40googlegroups.com.
>
>
> -- 
> Albert Krewinkel
> GPG: 8eed e3e2 e8c5 6f18 81fe e836 388d c0b2 1f63 1124
>

-- 
You received this message because you are subscribed to the Google Groups "pandoc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pandoc-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/f174ba4b-680a-4c5f-859f-c43b62734e1en%40googlegroups.com.

[-- Attachment #1.2: Type: text/html, Size: 4685 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-01-30 18:53 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-29 22:36 Extracting all equations (dropping the rest) bapt a
     [not found] ` <235047d3-c000-4adb-8d61-e5b905475cc4n-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
2022-01-30  7:18   ` John MacFarlane
     [not found]     ` <m24k5lsnc7.fsf-d8241O7hbXoP5tpWdHSM3tPlBySK3R6THiGdP5j34PU@public.gmane.org>
2022-01-30  9:04       ` bapt a
2022-01-30 11:20   ` Albert Krewinkel
     [not found]     ` <87leyxmpgu.fsf-9EawChwDxG8hFhg+JK9F0w@public.gmane.org>
2022-01-30 18:53       ` bapt a

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).