From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.io/gmane.text.pandoc/33508 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: "'lukeflo' via pandoc-discuss" Newsgroups: gmane.text.pandoc Subject: Re: Lua filter to change macro for figure caption md -> latex Date: Mon, 11 Dec 2023 14:04:40 -0800 (PST) Message-ID: <78503784-88bd-4d34-b5f5-a8634d667ba0n@googlegroups.com> References: <51ca8210-3d60-4d5d-9af2-04c85995deb6n@googlegroups.com> <32dfe8eb-98ac-40ee-92d7-162528add367n@googlegroups.com> Reply-To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_Part_176_15236605.1702332280625" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="18867"; mail-complaints-to="usenet@ciao.gmane.io" To: pandoc-discuss Original-X-From: pandoc-discuss+bncBD7NXUVXXUBBB6UO32VQMGQEO7F3STY-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Mon Dec 11 23:04:46 2023 Return-path: Envelope-to: gtp-pandoc-discuss@m.gmane-mx.org Original-Received: from mail-ot1-f60.google.com ([209.85.210.60]) by ciao.gmane.io with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1rCoOL-0004ag-Tc for gtp-pandoc-discuss@m.gmane-mx.org; Mon, 11 Dec 2023 23:04:46 +0100 Original-Received: by mail-ot1-f60.google.com with SMTP id 46e09a7af769-6d7e2edfc83sf7859196a34.1 for ; Mon, 11 Dec 2023 14:04:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=20230601; t=1702332284; x=1702937084; darn=m.gmane-mx.org; h=list-unsubscribe:list-subscribe:list-archive:list-help:list-post :list-id:mailing-list:precedence:reply-to:x-original-sender :mime-version:subject:references:in-reply-to:message-id:to:from:date :from:to:cc:subject:date:message-id:reply-to; bh=AAfi4TQcrTzDvjGpwU1KMimxgIomfIFsw/e04CtauYg=; b=WbIDwkLiivGJb5ZApaySfEtdD75SUG8bUvVcGS3Jtfzjnr1o3YGKh79dWuOFXbqbNn Ry7jO4r8WczDNbTPwQMj16Xy4eHJTqqcC0y1t5I0zHF4dvu2IyxDJzIHz9hMjccdmBdB N/hoHjPrL4dodRDcu6QtkrS03zer3jak3+wu3Ysy8lvGZYenKfh9hROt8WX2fIuQYk0u uNNX3+0x/o3NuYe+0Aa5OnoS4pS3BO66Rl4OgYBBOA9K9oaWI/VrVg8fQVBp8tkwrj3H KYJt9in9Y3wC6XXpxr35/Tlj1WV0HjYy0mbs3CHkX2+D0ZUKUqvF83pGKqymc9Cxg452 GaFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702332284; x=1702937084; h=list-unsubscribe:list-subscribe:list-archive:list-help:list-post :x-spam-checked-in-group:list-id:mailing-list:precedence:reply-to :x-original-sender:mime-version:subject:references:in-reply-to :message-id:to:from:date:x-beenthere:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=AAfi4TQcrTzDvjGpwU1KMimxgIomfIFsw/e04CtauYg=; b=sDK4aHDTKUcxzWv4BY5Fl+DuhlNxzqWAkc1AFNZPvRkhuO3ZDXGg8c3zTXin7RMJAB Pn2WGwFPvkjiVbt8ou02RWivclKocYZFj3EdHMUXaIeNf5H4ZSAqd/5j9s5qYq2bUWQc iXbyAVf9Eo6f2oMo9XBHunU2KNrGvEJrrybVtF4W5sMxKanlmRB3hKBT63yTAWDTr5Pm p8paL8TVJlAlh3QfiJtO89D2wIHLevMHEfWjqYUIO8HpItzgZCc1gC8B0oTAr+hWFrsv Nbc9RWKBuKAqaEpy3auFjkIZ/lDudSZXmMrgzFoSVTQOD6hRefTip2glA X-Gm-Message-State: AOJu0YxRKOU4s2ZsC4cAkGZgFEb+uryS52fKzPUmsGLfgSz1vlpyEQ9R jXluVb43qak5NUs/14xgSNk= X-Google-Smtp-Source: AGHT+IHQdCj4gV4eCFmu6tNVttXpv1qYXILyHoPHgLhQRZBTsyEDtK+E8qN/XNeIsoQ/KohUS8600w== X-Received: by 2002:a05:6870:c10c:b0:1fa:f7a7:bcb4 with SMTP id f12-20020a056870c10c00b001faf7a7bcb4mr6381072oad.59.1702332284618; Mon, 11 Dec 2023 14:04:44 -0800 (PST) X-BeenThere: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Original-Received: by 2002:a05:6871:8f88:b0:1fb:2a88:39c9 with SMTP id aa8-20020a0568718f8800b001fb2a8839c9ls660066oac.0.-pod-prod-06-us; Mon, 11 Dec 2023 14:04:41 -0800 (PST) X-Received: by 2002:a05:6870:96a1:b0:1e1:2ebc:b636 with SMTP id o33-20020a05687096a100b001e12ebcb636mr6036189oaq.4.1702332281344; Mon, 11 Dec 2023 14:04:41 -0800 (PST) In-Reply-To: X-Original-Sender: k.flo.west-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org X-Original-From: lukeflo Precedence: list Mailing-list: list pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org; contact pandoc-discuss+owners-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org List-ID: X-Google-Group-Id: 1007024079513 List-Post: , List-Help: , List-Archive: , List-Unsubscribe: , Xref: news.gmane.io gmane.text.pandoc:33508 Archived-At: ------=_Part_176_15236605.1702332280625 Content-Type: multipart/alternative; boundary="----=_Part_177_2142526479.1702332280625" ------=_Part_177_2142526479.1702332280625 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Dear William, dear Pablo, thanks for your replies. It does indeed help me to understand the way such= =20 writers/filters work. But unfortunatley, I can't produce a working filter= =20 on my own. @William: I tried using 'fig.caption' instead of 'fig.content.caption[1]'.= =20 But then I get a different error message: *Error running Lua: attempt to=20 concatenate a table value (field 'caption')*. Thus, I'm a little bit confused, since the html output at the end of your= =20 logging output looks like most fields are processed correctly. @Pablo: Your filters are really interesting. The float code gave me some=20 usefule insights. I also tried to adapt your Latex code: ``` lua function Figure(elem) raw_elem =3D '\\begin{figure*}\n' .. '\\centering\n' ..=20 '\\includegraphics[width=3D0.9\\textwidth]{' .. elem.content = ..=20 '}\n' ..=20 '\\caption{' .. elem.caption .. '}\n' ..=20 '\\end{figure*}' return pandoc.RawBlock('latex', raw_elem) end ``` I run it as filter instead as writer. But get the same error as above. Generally, my biggest problem is that I don't fully understand how to=20 access the values of the different AST fields. How, for example, can I use= =20 the values of the different fields of a Pandoc Figure Block=20 ? Caption is there defined= =20 as key-value pair for=20 long and short captions, but as mentioned 'fig.caption' oder 'elem.caption'= =20 throws the table value error. Might that be due to the key-value form? And= =20 is there a difference accessing these things for a filter instead of a=20 writer? If someone has some informations on that, it would be great; even better=20 with a (short) framework or example that shows how to access a value and=20 put it in a framework with latex-code surrounded. The docs are very=20 detailed, but I don't find an explanation of these basics which makes sense= =20 to me... Thanks again and all the best! Pablo Serrati schrieb am Montag, 11. Dezember 2023 um 15:53:38 UTC+1: > Hello Lukeflo > > I have been working for a few months on a project to enable the=20 > publication of scientific journals (https://github.com/estedeahora/guri).= =20 > The project has a workflow that takes `docx` and generates the final file= s=20 > (`pdf`, `xml` and `html`), using an `markdown` (md) as an intermediate=20 > file. One of the issues I had to address was the floating elements and in= =20 > particular the use of subcaptions for "sources" and "notes". While the=20 > project is still in the development stage, I think some of the filters I= =20 > use may help you (see `files/filters/`).=20 > > So far we are using the proposal in the journal [Quid 16]( > https://publicaciones.sociales.uba.ar/index.php/quid16/issue/current) )= =20 > if you are interested in seeing the results. My plan is to generate a=20 > stable release that can be used by other journals in the coming months. I= =20 > am open to new ideas and contributions to improve the project. > > Best, > > Pablo=20 > > El lun, 11 dic 2023 a la(s) 08:30, 'William Lupton' via pandoc-discuss ( > pandoc-...-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org) escribi=C3=B3: > >> Luke, >> >> Filter functions take a single argument (the element in question) so you= =20 >> need the second form: Figure(fig). >> >> I think the crash is because the caption is in fig.caption (not in=20 >> fig.content[1].caption). See=20 >> https://pandoc.org/lua-filters.html#type-figure. >> >> Perhaps I can put in a plug for https://github.com/pandoc-ext/logging. I= =20 >> think you might find this helpful for gaining insight into the element= =20 >> structure. See example below. >> >> Hope this helps. >> >> Cheers, >> William >> >> -------- >> >> With this filter in rep.lua: >> >> local logging =3D require 'logging' >> >> function Pandoc(pandoc) >> >> if logging.loglevel > 0 then >> >> logging.temp('meta', pandoc.meta) >> >> end >> >> logging.temp('blocks', pandoc.blocks) >> >> end >> >> ...your input gives this: >> >> *% *pandoc luke.md -L rep.lua >> >> (#) blocks Blocks[3] { >> >> [1] Header { >> >> attr: Attr { >> >> attributes: AttributeList {} >> >> classes: List {} >> >> identifier: "headline" >> >> } >> >> content: Inlines[1] { >> >> [1] Str "Headline" >> >> } >> >> level: 1 >> >> } >> >> [2] Para { >> >> content: Inlines[3] { >> >> [1] Str "Some" >> >> [2] Space >> >> [3] Str "text" >> >> } >> >> } >> >> [3] Figure { >> >> attr: Attr { >> >> attributes: AttributeList {} >> >> classes: List {} >> >> identifier: "" >> >> } >> >> caption: { >> >> long: Blocks[1] { >> >> [1] Plain { >> >> content: Inlines[7] { >> >> [1] Str "caption" >> >> [2] Space >> >> [3] Str "to" >> >> [4] Space >> >> [5] Str "an" >> >> [6] Space >> >> [7] Str "image" >> >> } >> >> } >> >> } >> >> } >> >> content: Blocks[1] { >> >> [1] Plain { >> >> content: Inlines[1] { >> >> [1] Image { >> >> attr: Attr { >> >> attributes: AttributeList {} >> >> classes: List {} >> >> identifier: "" >> >> } >> >> caption: Inlines[7] { >> >> [1] Str "caption" >> >> [2] Space >> >> [3] Str "to" >> >> [4] Space >> >> [5] Str "an" >> >> [6] Space >> >> [7] Str "image" >> >> } >> >> src: "counter_plot_new_periods.png" >> >> title: "" >> >> } >> >> } >> >> } >> >> } >> >> } >> >> } >> >>

Headline

>> >>

Some text

>> >>
>> >> 3D"caption >> >>
caption to an image
>> >>
>> >> On Mon, 11 Dec 2023 at 10:36, 'lukeflo' via pandoc-discuss < >> pandoc-...-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> wrote: >> >>> PS: I know I'm using two very different approaches calling the Figure= =20 >>> function with arguments. Thats due to the fact that I'm not sure which = way=20 >>> is the right one... >>> >>> lukeflo schrieb am Montag, 11. Dezember 2023 um 11:33:21 UTC+1: >>> >>>> So far, I took the markdwon writer example from the pandoc docs=20 >>>> =20 >>>> to try out the general function of writers. It works and I think that = I=20 >>>> understand the general usage. >>>> >>>> But especially figures (in latex writer and presuambley in general) ar= e=20 >>>> relativley complex. Here are two things I tried out so far but always = got=20 >>>> an error: >>>> >>>> ``` lua >>>> function Writer (doc, opts) >>>> local filter =3D { >>>> function Figure (caption, image, attr) >>>> local figcap =3D '\sidecaption{' .. caption .. '}' >>>> return '\\begin{figure}\n' .. image .. '\n' .. figcap .. '\n'=20 >>>> '\\end{figure}\n' >>>> end >>>> } >>>> return pandoc.write(doc:walk(filter), 'latex', opts) >>>> end >>>> ``` >>>> If I run this writer with my custom template from the CLI using *pando= c=20 >>>> --template=3D../custom.template -t test-writer.lua ast-test.txt -o=20 >>>> ast-test.tex* I get=20 >>>> *Error running Lua:test-writer.lua:27: '(' expected near 'Figure'*. >>>> >>>> Furthermore, I tried running the following code just to understand how= =20 >>>> those writer work. Here I just wanted to replace {figure} with the sta= rred=20 >>>> version {figure*} (not sidecaption): >>>> >>>> ``` lua >>>> function Writer (doc, opts) >>>> local filter =3D { >>>> Figure =3D function (fig) >>>> local tester =3D '\\begin{figure*}\n' ..=20 >>>> fig.content[1].caption[1].attributes[1] .. '\\end{figure*}\n' >>>> return pandoc.RawBlock('latex', tester) >>>> end >>>> } >>>> return pandoc.write(doc:walk(filter), 'latex', opts) >>>> end >>>> ``` >>>> But also got an error: >>>> >>>> >>>> >>>> >>>> >>>> >>>> >>>> >>>> *Error running Lua:test-writer.lua:28: attempt to index a nil value=20 >>>> (field 'caption')stack traceback: [C]: in ? [C]: in method 'walk'=20 >>>> test-writer.lua:32: in function 'Writer'stack traceback:=20 >>>> test-writer.lua:32: in function 'Writer'* >>>> >>>> I'm aware that I might be missing something very basic and maybe even= =20 >>>> very simple. But I'm kind of getting lost a little bit inside all=20 >>>> functions, modules etc. as well as the general framework of such write= rs. >>>> >>>> Thus, any help explaining my errors and maybe suggesting some better= =20 >>>> code is very appreciated! >>>> >>>> The test file in both cases is very simple: >>>> >>>> ``` markdown >>>> --- >>>> title: A title >>>> --- >>>> >>>> # Headline >>>> >>>> Some text >>>> >>>> ![caption to an image](counter_plot_new_periods.png) >>>> ``` >>>> >>>> Thanks in advance! >>>> lukeflo schrieb am Freitag, 8. Dezember 2023 um 08:35:48 UTC+1: >>>> >>>>> Hello Julien, >>>>> >>>>> thanks for the reply. Unfortunatley, as mentioned in the stackoverflo= w=20 >>>>> post, your suggested LaTeX code won't work. >>>>> >>>>> The \caption macro is very complex in the backend and cannot be=20 >>>>> copied on the fly via \let, \NewCommandCopy or something similar.=20 >>>>> Even after doing so with e.g. \NewCommandCopy{\oldcaption}{\caption}= =20 >>>>> and then setting \RenewDocumentCommand{\caption}{o=20 >>>>> m}{\sidecaption[#1]{#2}} nothing changes and the definition of=20 >>>>> \caption, checked with \meaning or something similar, stays the same= =20 >>>>> as before (even \DeclareDocumentCommand doesn't work). >>>>> >>>>> In the end, it might be possible to somehow change the \caption macro= =20 >>>>> itself. But the effort might not be worth the result (and its more of= a=20 >>>>> question for TeX.SE). >>>>> >>>>> Using a custom writer for building Latex figures and replace the=20 >>>>> \caption string inside would be a great solution. I read through the = writer=20 >>>>> manual, but didn't really understand how the AST works and which valu= es=20 >>>>> have to be used in such a writer. Furthermore, I'm using a a custom L= atex=20 >>>>> template for exporting (based on the default.template.latex) which ha= s to=20 >>>>> be integrated with such a writer. >>>>> >>>>> Therefore, I really woud appreciate a Lua framework to understand=20 >>>>> which functions have to be edited etc. to accomplish the substitution= . >>>>> >>>>> Best >>>>> >>>>> Julien Dutant schrieb am Dienstag, 5. Dezember 2023 um 17:09:19 UTC+1= : >>>>> >>>>>> Lua filters only change Pandoc's AST representation of your document= ,=20 >>>>>> i.e. before it is then converted to LaTeX. A Raw block filter will n= ot act=20 >>>>>> on Pandoc's LaTeX output, but only on Raw LaTeX blocks that are in t= he=20 >>>>>> markdown itself. >>>>>> >>>>>> A Pandoc solution would be to write a custom Lua *writer*=20 >>>>>> . The writer would use=20 >>>>>> pandoc.write to generate Pandoc's own LaTeX output (body only) and m= odify=20 >>>>>> it with regular expressions or Lua patterns. To replace just a comma= nd name=20 >>>>>> this is fairly easy, though longer than the third solution below. >>>>>> >>>>>> A LaTeX solution is to redefine \caption as \sidecaption: >>>>>> \renewcommand{\caption}{\sidecaption}=20 >>>>>> >>>>>> You can keep this enclosed in groups ({...}) to ensure that the=20 >>>>>> redefinition only applies locally. >>>>>> >>>>>> A hybrid Pandoc/LaTeX solution is a Lua filter that insert LaTeX cod= e=20 >>>>>> to redefine \caption around figures: >>>>>> >>>>>> ``` lua >>>>>> if FORMAT:match 'latex' then >>>>>> function Figure (elem) return {=20 >>>>>> pandoc.RawBlock('latex', >>>>>> '{\\renewcommand{\\caption}{\\subcaption}'),=20 >>>>>> elem,=20 >>>>>> pandoc.RawBlock('latex','}')=20 >>>>>> }=20 >>>>>> end=20 >>>>>> end >>>>>> >>>>>> ``` >>>>>> >>>>>> This replaces any 'Figure' block element by a list (succession) of= =20 >>>>>> three raw LaTeX blocks. The output should look like: >>>>>> {\renewcommand{\caption}{\subcaption}=20 >>>>>> ... Pandoc's LaTeX for the figure ...=20 >>>>>> }=20 >>>>>> >>>>>> Reposted from=20 >>>>>> https://stackoverflow.com/questions/77504584/pandoc-md-latex-write-l= ua-filter-to-change-latex-macro-used-for-caption/77607636#77607636 >>>>>> >>>>>> On Monday, November 20, 2023 at 7:06:57=E2=80=AFAM UTC+11 lukeflo wr= ote: >>>>>> >>>>>>> Hi everybody, >>>>>>> >>>>>>> I have written a custom latex `.cls' file to establish a typesettin= g >>>>>>> workflow for the scientific journals of my research institute. The= =20 >>>>>>> texts >>>>>>> should be written in Markdown and then be processed with `pandoc' t= o >>>>>>> LaTeX. >>>>>>> >>>>>>> I already have an elaborated pandoc template to produce the LaTeX >>>>>>> preambel etc. So far its working great. >>>>>>> >>>>>>> But for the figures I need the caption from the Markdown file to be= =20 >>>>>>> set >>>>>>> with `\sidecaption' instead of `\caption' in LaTeX, as well as with= =20 >>>>>>> an >>>>>>> optional argument (short-caption) for the image attribution in the= =20 >>>>>>> list >>>>>>> of figures. >>>>>>> >>>>>>> To get the latter working I use the following template from a GitHu= b >>>>>>> discussion in the [pandoc repo]: >>>>>>> >>>>>>> =E2=94=8C=E2=94=80=E2=94=80=E2=94=80=E2=94=80 >>>>>>> =E2=94=82 PANDOC_VERSION:must_be_at_least '3.1' >>>>>>> =E2=94=82 >>>>>>> =E2=94=82 if FORMAT:match 'latex' then >>>>>>> =E2=94=82 function Figure(f) >>>>>>> =E2=94=82 local short =3D=20 >>>>>>> f.content[1].content[1].attributes['short-caption'] >>>>>>> =E2=94=82 if short and not f.caption.short then >>>>>>> =E2=94=82 f.caption.short =3D pandoc.Inlines(short) >>>>>>> =E2=94=82 end >>>>>>> =E2=94=82 return f >>>>>>> =E2=94=82 end >>>>>>> =E2=94=82 end >>>>>>> =E2=94=94=E2=94=80=E2=94=80=E2=94=80=E2=94=80 >>>>>>> >>>>>>> That works without any flaws. >>>>>>> >>>>>>> But now I need to figure out how to change the LaTeX macro used for= =20 >>>>>>> the >>>>>>> caption. The older [approach of pre pandoc version 3.0 posted] by= =20 >>>>>>> tarleb >>>>>>> is really intuitive and I could have easily adapted it to my needs.= =20 >>>>>>> But >>>>>>> since pandoc 3.0 there is the new [/complex figures/] approach and,= =20 >>>>>>> so >>>>>>> far, I couldn't figure out how to change the LaTeX macro used for t= he >>>>>>> captions with this new behaviour. >>>>>>> >>>>>>> I tried something like that (Adapted from [here]: >>>>>>> >>>>>>> =E2=94=8C=E2=94=80=E2=94=80=E2=94=80=E2=94=80 >>>>>>> =E2=94=82 if FORMAT:match 'latex' then >>>>>>> =E2=94=82 function RawBlock (raw) >>>>>>> =E2=94=82 local caption =3D raw.text:match('\\caption') >>>>>>> =E2=94=82 if caption then >>>>>>> =E2=94=82 raw:gsub('\\caption', '\\sidecaption') >>>>>>> =E2=94=82 end >>>>>>> =E2=94=82 return raw >>>>>>> =E2=94=82 end >>>>>>> =E2=94=82 end >>>>>>> =E2=94=94=E2=94=80=E2=94=80=E2=94=80=E2=94=80 >>>>>>> >>>>>>> But nothing happened. >>>>>>> >>>>>>> The main challenge for me are my more-or-less non-existing lua=20 >>>>>>> skills. I >>>>>>> just never had to use it for my daily tasks. I thought about using= =20 >>>>>>> `awk' >>>>>>> or `sed' to edit the `.tex' file itself using a regex-substitution,= =20 >>>>>>> but >>>>>>> that should remain an absolute stopgap, since it makes the whole >>>>>>> workflow less portable. >>>>>>> >>>>>>> Thus, I'm hoping for a hint/a solution in form of a pandoc-lua scri= pt >>>>>>> which 1. helps me to achieve the goal, and 2. improve my=20 >>>>>>> understanding >>>>>>> of lua and the /complex figures/ approach for similar future tasks. >>>>>>> >>>>>>> I appreciate any tipp! >>>>>>> >>>>>>> Best, >>>>>>> Lukeflo >>>>>>> >>>>>>> This question is also posted on StackOverFlow:=20 >>>>>>> https://stackoverflow.com/q/77504584/19647155 >>>>>>> >>>>>>> [pandoc repo] >>>>>>> >>>>>>> >>>>>>> [approach of pre pandoc version 3.0 posted] >>>>>>> >>>>>>> >>>>>>> [/complex figures/] >>>>>>> >>>>>>> [here] >>>>>>> >>>>>> --=20 >>> You received this message because you are subscribed to the Google=20 >>> Groups "pandoc-discuss" group. >>> To unsubscribe from this group and stop receiving emails from it, send= =20 >>> an email to pandoc-discus...-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >>> To view this discussion on the web visit=20 >>> https://groups.google.com/d/msgid/pandoc-discuss/32dfe8eb-98ac-40ee-92d= 7-162528add367n%40googlegroups.com=20 >>> >>> . >>> >> --=20 >> You received this message because you are subscribed to the Google Group= s=20 >> "pandoc-discuss" group. >> To unsubscribe from this group and stop receiving emails from it, send a= n=20 >> email to pandoc-discus...-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> > To view this discussion on the web visit=20 >> https://groups.google.com/d/msgid/pandoc-discuss/CAEe_xxikobOS_G9x71nxtz= 0dr99VVhgBV8in%3DxKzXgh7JMaRcw%40mail.gmail.com=20 >> >> . >> > --=20 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 e= mail 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/78503784-88bd-4d34-b5f5-a8634d667ba0n%40googlegroups.com. ------=_Part_177_2142526479.1702332280625 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Dear William, dear Pablo,

thanks for your r= eplies. It does indeed help me to understand the way such writers/filters w= ork. But unfortunatley, I can't produce a working filter on my own.

@William: I tried using 'fig.caption' instead of 'fig.c= ontent.caption[1]'. But then I get a different error message: Error runn= ing Lua: attempt to concatenate a table value (field 'caption').
<= div>Thus, I'm a little bit confused, since the html output at the end of yo= ur logging output looks like most fields are processed correctly.
@Pablo: Your filters are really interesting. The float co= de gave me some usefule insights. I also tried to adapt your Latex code:

``` lua
function Figure(elem)
=C2= =A0 raw_elem =3D '\\begin{figure*}\n' ..
=C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 '\\centering\n' ..
=C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 '\\includegraphics[width=3D0.9\\textwidth]{' .. elem.c= ontent .. '}\n' ..
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 '= \\caption{' .. elem.caption .. '}\n' ..
=C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 '\\end{figure*}'
=C2=A0 return pandoc.RawBlock('l= atex', raw_elem)
end
```
I run it as filter instea= d as writer. But get the same error as above.

Ge= nerally, my biggest problem is that I don't fully understand how to access = the values of the different AST fields. How, for example, can I use the val= ues of the different fields of a Pandoc Figure Block? Caption is there defined as key-value pair= for long and short captions, but as mentioned 'fig.caption' oder 'elem.cap= tion' throws the table value error. Might that be due to the key-value form= ? And is there a difference accessing these things for a filter instead of = a writer?

If someone has some informations= on that, it would be great; even better with a (short) framework or exampl= e that shows how to access a value and put it in a framework with latex-cod= e surrounded. The docs are very detailed, but I don't find an explanation o= f these basics which makes sense to me...

Thanks= again and all the best!

<= div dir=3D"auto" class=3D"gmail_attr">Pablo Serrati schrieb am Montag, 11. = Dezember 2023 um 15:53:38 UTC+1:
Hello Lukeflo

I have been worki= ng for a few months on a project to enable the publication of scientific jo= urnals (https://github.com/e= stedeahora/guri). The project has a workflow that takes `docx` and gene= rates the final files (`pdf`, `xml` and `html`), using an `markdown` (md) a= s an intermediate file. One of the issues I had to address was the floating= elements and in particular the use of subcaptions for "sources" = and "notes". While the project is still in the development stage,= I think some of the filters I use may help you (see `files/filters/`).=C2= =A0

So far we are using the proposal in the journal= [Quid 16](https://publicaciones.soci= ales.uba.ar/index.php/quid16/issue/current) ) if you are interested in = seeing the results.=C2=A0My plan is to generate a stable release that can b= e used by other journals in the coming months. I am open to new ideas and c= ontributions to improve the project.

Best,

Pablo=C2=A0

El lun, 11 dic 2023 a la(s) 08:30, 'William Lupton' via pand= oc-discuss (pandoc-...@googlegro= ups.com) escribi=C3=B3:
Luke,

Filter functions take a sing= le argument (the element in question) so you need the second form: Figure(f= ig).

I think the crash is because the caption is in fig.= caption (not in fig.content[1].caption). See=C2=A0https://pandoc.org/lua-filt= ers.html#type-figure.

Perhaps I can put in a p= lug for=C2=A0https://git= hub.com/pandoc-ext/logging. I think you might find this helpful for gai= ning insight into the element structure. See example below.

<= /div>
Hope this helps.

Cheers,
Willi= am

--------

With this fil= ter in rep.lua:

local logging =3D require &= #39;logging'

function Pandoc(pandoc)

=C2=A0 =C2=A0 = if logging.loglevel > 0 then

=C2=A0 =C2=A0 =C2=A0 = =C2=A0 logging.temp('meta', pandoc.meta)

=C2=A0 =C2=A0 = end

=C2=A0 =C2=A0 = logging.temp('blocks', pandoc.blocks)

end

<= div>
...your input gives this:

% <= /b>pandoc= luke.md -L rep.lua

(#) blocks Blocks[3] {

=C2=A0 [1] Hea= der {

=C2=A0 =C2=A0 = attr: Attr {

=C2=A0 =C2=A0 =C2=A0 = attributes: AttributeList {}

=C2=A0 =C2=A0 =C2=A0 = classes: List {}

=C2=A0 =C2=A0 =C2=A0 = identifier: "headline"

=C2=A0 =C2=A0 = }

=C2=A0 =C2=A0 = content: Inlines[1] {

=C2=A0 =C2=A0 =C2=A0 = [1] Str "Headline"

=C2=A0 =C2=A0 = }

=C2=A0 =C2=A0 = level: 1

=C2=A0 }

=C2=A0 [2] Par= a {

=C2=A0 =C2=A0 = content: Inlines[3] {

=C2=A0 =C2=A0 =C2=A0 = [1] Str "Some"

=C2=A0 =C2=A0 =C2=A0 = [2] Space

=C2=A0 =C2=A0 =C2=A0 = [3] Str "text"

=C2=A0 =C2=A0 = }

=C2=A0 }

=C2=A0 [3] Fig= ure {

=C2=A0 =C2=A0 = attr: Attr {

=C2=A0 =C2=A0 =C2=A0 = attributes: AttributeList {}

=C2=A0 =C2=A0 =C2=A0 = classes: List {}

=C2=A0 =C2=A0 =C2=A0 = identifier: ""

=C2=A0 =C2=A0 = }

=C2=A0 =C2=A0 = caption: {

=C2=A0 =C2=A0 =C2=A0 = long: Blocks[1] {

=C2=A0 =C2=A0 =C2=A0 = =C2=A0 [1] Plain {

=C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 content: Inlines[7] {

=C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 [1] Str "caption"

=C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 [2] Space

=C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 [3] Str "to"

=C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 [4] Space

=C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 [5] Str "an"

=C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 [6] Space

=C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 [7] Str "image"

=C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 }

=C2=A0 =C2=A0 =C2=A0 = =C2=A0 }

=C2=A0 =C2=A0 =C2=A0 = }

=C2=A0 =C2=A0 = }

=C2=A0 =C2=A0 = content: Blocks[1] {

=C2=A0 =C2=A0 =C2=A0 = [1] Plain {

=C2=A0 =C2=A0 =C2=A0 = =C2=A0 content: Inlines[1] {

=C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 [1] Image {

=C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 attr: Attr {

=C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 attributes: AttributeList {}

=C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 classes: List {}

=C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 identifier: ""

=C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 }

=C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 caption: Inlines[7] {

=C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 [1] Str "caption"

=C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 [2] Space

=C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 [3] Str "to"

=C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 [4] Space

=C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 [5] Str "an"

=C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 [6] Space

=C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 [7] Str "image"

=C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 }

=C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 src: "counter_plot_new_periods.png"

=C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 title: ""

=C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 }

=C2=A0 =C2=A0 =C2=A0 = =C2=A0 }

=C2=A0 =C2=A0 =C2=A0 = }

=C2=A0 =C2=A0 = }

=C2=A0 }

}

<h1 id=3D"headline&= quot;>Headline</h1>

<p>Some text</p>= ;

<figure>

<img src=3D"counter= _plot_new_periods.png" alt=3D"caption to an image" />

<figcaption aria-hidden= =3D"true">caption to an image</figcaption>

</figure>

<= /div>
O= n Mon, 11 Dec 2023 at 10:36, 'lukeflo' via pandoc-discuss <pandoc-...-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> w= rote:
PS: I know= I'm using two very different approaches calling the Figure function wi= th arguments. Thats due to the fact that I'm not sure which way is the = right one...

lukeflo schrieb am Montag, 11. Dezember 2023 um 11:33:21 UTC+1:<= br>
So far, I t= ook the markdwon writer examp= le from the pandoc docs to try out the general function of writers. It = works and I think that I understand the general usage.

=
But especially figures (in latex writer and presuambley in general) ar= e relativley complex. Here are two things I tried out so far but always got= an error:

``` lua
function Writer (= doc, opts)
=C2=A0 =C2=A0local filter =3D {
=C2=A0 =C2=A0 =C2=A0 funct= ion Figure (caption, image, attr)
= =C2=A0local figcap =3D '\sidecaption{' .. caption .. '}&= #39;
=C2=A0return '\\be= gin{figure}\n' .. image .. '\n' .. figcap .. '\n' '= \\end{figure}\n'
=C2=A0 =C2=A0 =C2=A0 end
=C2=A0 =C2=A0}
=C2= =A0 =C2=A0return pandoc.write(doc:walk(filter), 'latex', opts)
e= nd
```
If I run this writer with my custom template= from the CLI using=C2=A0pandoc --template=3D../custom.template -t test-= writer.lua ast-test.txt -o ast-test.tex I get Error running Lua:
= test-writer.lua:27: '(' expected near 'Figure'
.

Furthermore, I tried running the following code just to u= nderstand how those writer work. Here I just wanted to replace {figure} wit= h the starred version {figure*} (not sidecaption):

``` lua
function Writer (doc, opts)
=C2=A0 local filter =3D {=
=C2=A0 =C2=A0 =C2=A0Figure =3D function (fig)
local tester =3D '\\begin{figure*}\n' .. fig= .content[1].caption[1].attributes[1] .. '\\end{figure*}\n'
return pandoc.RawBlock('latex&#= 39;, tester)
=C2=A0 =C2=A0 end
=C2=A0 }
=C2=A0 return pandoc.write= (doc:walk(filter), 'latex', opts)
end
```
Bu= t also got an error:

Error running Lua:
test= -writer.lua:28: attempt to index a nil value (field 'caption')
s= tack traceback:
[C]: in ? [C]: in method 'walk'= ;
test-writer.lua:32: in fu= nction 'Writer'
stack traceback:
test-writer.lua:32: in function 'Writer'
=

I'm aware that I might be missing something very ba= sic and maybe even very simple. But I'm kind of getting lost a little b= it inside all functions, modules etc. as well as the general framework of s= uch writers.

Thus, any help explaining my errors a= nd maybe suggesting some better code is very appreciated!

The test file in both cases is very simple:

``` markdown
---
title: A title
---

# Headline=

Some text

![caption to an image](counter_plot_new_periods.pn= g)
```

Thanks in advance!
<= div class=3D"gmail_quote">
lukeflo sc= hrieb am Freitag, 8. Dezember 2023 um 08:35:48 UTC+1:
Hello Julien,

thanks for the reply. Unfortunatley, as mentioned in the stackoverfl= ow post, your suggested LaTeX code won't work.

The \caption macro is very complex in the backend and cannot = be copied on the fly via \let, \NewCommandCopy or= something similar. Even after doing so with e.g. \NewCommandCopy{\ol= dcaption}{\caption} and then setting \RenewDocumentCommand{\ca= ption}{o m}{\sidecaption[#1]{#2}} nothing changes and the definition= of \caption, checked with \meaning or something = similar, stays the same as before (even \DeclareDocumentCommand doesn't work).

In the end, it might be possible to somehow change the \caption macro itself. But the effort might not be worth the result (and its mo= re of a question for TeX.SE).

Using a custom writer for building Latex figures and replace the \captio= n string inside would be a great solution. I read through the writer manual= , but didn't really understand how the AST works and which values have = to be used in such a writer. Furthermore, I'm using a a custom Latex te= mplate for exporting (based on the default.template.latex) which has to be = integrated with such a writer.

Therefore, I really woud appreciate a = Lua framework to understand which functions have to be edited etc. to accom= plish the substitution.

Best


Julien Dutant schrieb am Diensta= g, 5. Dezember 2023 um 17:09:19 UTC+1:

Lua filters only change Pandoc's AST representation of your document= , i.e. before it is then converted to LaTeX. A Raw block filter will not=20 act on Pandoc's LaTeX output, but only on Raw LaTeX blocks that are in= =20 the markdown itself.

A Pandoc solution would be to write a custom Lua writer. The writer would use pandoc.write to generate Pandoc's own LaTeX outpu= t (body only) and modify it with regular expressions or Lua patterns. To=20 replace just a command name this is fairly easy, though longer than the=20 third solution below.

A LaTeX solution is to redefine \caption as \sidecaption:

\renewcommand{\caption}{\sidecaption}

You can keep this enclosed in groups ({...}) to ensure that the redefini= tion only applies locally.

A hybrid Pandoc/LaTeX solution is a Lua filter that insert LaTeX code to= redefine \caption around figures:

``` lua

if FORMAT:match 'latex'= then
= =C2=A0=C2=A0=C2=A0 function Figure (e= lem) return {=C2=A0
=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 pandoc.RawBlock('latex= 9;,'{\\renewcommand{\\caption}{\\subcaption}'),= =C2=A0
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0 elem,=C2=A0
=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 pandoc.RawBlock('latex= ','}')=C2=A0
= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 }
<= span>=C2=A0=C2=A0 end
end

```

This replaces any 'Figure' block element by a list (succession) = of three raw LaTeX blocks. The output should look like:

{\renewcommand{\caption}{\subcaption}=C2=A0
=
... Pandoc's LaTeX for the figure ...=C2=A0
}


On Monday, November= 20, 2023 at 7:06:57=E2=80=AFAM UTC+11 lukeflo wrote:
Hi everybody,

I have written a= custom latex `.cls' file to establish a typesetting
workflow for th= e scientific journals of my research institute. The texts
should be writ= ten in Markdown and then be processed with `pandoc' to
LaTeX.
I already have an elaborated pandoc template to produce the LaTeX
pream= bel etc. So far its working great.

But for the figures I need the ca= ption from the Markdown file to be set
with `\sidecaption' instead o= f `\caption' in LaTeX, as well as with an
optional argument (short-c= aption) for the image attribution in the list
of figures.

To get = the latter working I use the following template from a GitHub
discussion= in the [pandoc repo]:

=E2=94=8C=E2=94=80=E2=94=80=E2=94=80=E2=94=80=
=E2=94=82 PANDOC_VERSION:must_be_at_least '3.1'
=E2=94=82=E2=94=82 if FORMAT:match 'latex' then
=E2=94=82 =C2=A0 functio= n Figure(f)
=E2=94=82 =C2=A0 =C2=A0 local short =3D f.content[1].content= [1].attributes['short-caption']
=E2=94=82 =C2=A0 =C2=A0 if short= and not f.caption.short then
=E2=94=82 =C2=A0 =C2=A0 =C2=A0 f.caption.s= hort =3D pandoc.Inlines(short)
=E2=94=82 =C2=A0 =C2=A0 end
=E2=94=82 = =C2=A0 =C2=A0 return f
=E2=94=82 =C2=A0 end
=E2=94=82 end
=E2=94= =94=E2=94=80=E2=94=80=E2=94=80=E2=94=80

That works without any flaws= .

But now I need to figure out how to change the LaTeX macro used fo= r the
caption. The older [approach of pre pandoc version 3.0 posted] by = tarleb
is really intuitive and I could have easily adapted it to my need= s. But
since pandoc 3.0 there is the new [/complex figures/] approach an= d, so
far, I couldn't figure out how to change the LaTeX macro used = for the
captions with this new behaviour.

I tried something like = that (Adapted from [here]:

=E2=94=8C=E2=94=80=E2=94=80=E2=94=80=E2= =94=80
=E2=94=82 if FORMAT:match 'latex' then
=E2=94=82 =C2= =A0 function RawBlock (raw)
=E2=94=82 =C2=A0 =C2=A0 local caption =3D ra= w.text:match('\\caption')
=E2=94=82 =C2=A0 =C2=A0 if caption the= n
=E2=94=82 =C2=A0 =C2=A0 =C2=A0 =C2=A0raw:gsub('\\caption', = 9;\\sidecaption')
=E2=94=82 =C2=A0 =C2=A0 end
=E2=94=82 =C2=A0 = =C2=A0 return raw
=E2=94=82 =C2=A0 end
=E2=94=82 end
=E2=94=94=E2= =94=80=E2=94=80=E2=94=80=E2=94=80

But nothing happened.

The m= ain challenge for me are my more-or-less non-existing lua skills. I
just= never had to use it for my daily tasks. I thought about using `awk'or `sed' to edit the `.tex' file itself using a regex-substitution= , but
that should remain an absolute stopgap, since it makes the wholeworkflow less portable.

Thus, I'm hoping for a hint/a solution= in form of a pandoc-lua script
which 1. helps me to achieve the goal, a= nd 2. improve my understanding
of lua and the /complex figures/ approach= for similar future tasks.

I appreciate any tipp!

Best,
Lu= keflo

This question is also posted on StackOverFlow: https://stackoverflow.c= om/q/77504584/19647155

[pandoc repo]
<https://github.com/jgm/pandoc/issues/7915#issuecomment-142711= 3349>

[approach of pre pandoc version 3.0 posted]
<https://github.com/jgm/pandoc/issues/7915#iss= uecomment-1039370851>

[/complex figures/] <https://github.com/= jgm/pandoc/releases?page=3D2>

[here] <https://stackoverflow.com/a/= 71296595/19647155>

--
You received this message because you are subscribed to the Google Groups &= quot;pandoc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an e= mail to pandoc-discus...@googleg= roups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/32dfe8eb-9= 8ac-40ee-92d7-162528add367n%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups &= quot;pandoc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an e= mail to pandoc-discus...@googleg= roups.com.

--
You received this message because you are subscribed to the Google Groups &= quot;pandoc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an e= mail to pand= oc-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org.
To view this discussion on the web visit https://groups.google.com/d= /msgid/pandoc-discuss/78503784-88bd-4d34-b5f5-a8634d667ba0n%40googlegroups.= com.
------=_Part_177_2142526479.1702332280625-- ------=_Part_176_15236605.1702332280625--