From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.io/gmane.text.pandoc/33506 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Pablo Serrati Newsgroups: gmane.text.pandoc Subject: Re: Lua filter to change macro for figure caption md -> latex Date: Mon, 11 Dec 2023 11:53:21 -0300 Message-ID: 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/alternative; boundary="0000000000000c0cdf060c3d1941" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="27681"; mail-complaints-to="usenet@ciao.gmane.io" To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Original-X-From: pandoc-discuss+bncBDGNHXVU7UDRB36E3SVQMGQEJR7SUAA-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Mon Dec 11 15:53:39 2023 Return-path: Envelope-to: gtp-pandoc-discuss@m.gmane-mx.org Original-Received: from mail-qt1-f185.google.com ([209.85.160.185]) by ciao.gmane.io with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1rChf9-000710-6o for gtp-pandoc-discuss@m.gmane-mx.org; Mon, 11 Dec 2023 15:53:39 +0100 Original-Received: by mail-qt1-f185.google.com with SMTP id d75a77b69052e-42599db593fsf59804591cf.0 for ; Mon, 11 Dec 2023 06:53:39 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1702306418; cv=pass; d=google.com; s=arc-20160816; b=toJryJSa67Sp/J1i47+iw7LoA6b52UvJL76DNKP2LdxVTiXj2OX+NLd+P28JKzseEB O9cFJWPRg3VqqvHj1exl3y4AUm+Tv0K0jTUYZ05GwXoChZC19rMffzwbTOvXJ1rIGc4a kWzydcAUjuK6FjkGtptSjkGTa30shM7PIWyunhxyR+8KrlOgTw2N5sesRYut/Y8pzLst vX8i6HUuveyoEH1FRSLW3qHIZR+VjIWcOxxsO9JyccZ38MQEnd8lyTyQp4BVTMILmBia dKTSxR9bJGBph+j5APWRSSKd8JReUjaEEcwo7ZmoR/8fD6x5e4qV2eqEeXt6qkF0Xcz9 sYMw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-unsubscribe:list-subscribe:list-archive:list-help:list-post :list-id:mailing-list:precedence:reply-to:to:subject:message-id:date :from:in-reply-to:references:mime-version:sender:dkim-signature :dkim-signature; bh=x1NWvuBGAA/+w+UlIaBdpUNzz585hvM0NYVJ+qd0YAo=; fh=m01AhCNo7xUywHldCVYouaJypLlN7JgtNYbImzBf4N4=; b=QcUpG4Ez7ZVU47LK/31kjwq61C/1x1o/sb4b82dsjqz5h9cCnATOH1Mj0Grpm4eeA+ BgTF6bI49m2JeDdr96YIbgkZeVqvWNp2pbEP+sHvDSyT64cyEKmwq9e6HrZ3EiE+XOTd Ntgv0kGeVI4Hv+FwEUwF6RwQKO1nY8AkuZXswfH6cxkVqC/xiT+IKCyx9qowuciMpNq2 aIoJefapyM9kT6A5QWhNTAM/Esc92mwE6MEtKKzCG9FOB6kPr98dgaJij2ypsWOsMxf1 6yxmxVrhhtgovHL5rSWj2y0/Cw/BOkAneJW7by91tAYa9fa9FcgJLYtEuYaVCuMUHj6p ax3Q== ARC-Authentication-Results: i=2; gmr-mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=I4MKC8w4; spf=pass (google.com: domain of pabloserrati-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org designates 2607:f8b0:4864:20::112f as permitted sender) smtp.mailfrom=pabloserrati-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=20230601; t=1702306418; x=1702911218; 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-authentication-results:x-original-sender:to:subject :message-id:date:from:in-reply-to:references:mime-version:sender :from:to:cc:subject:date:message-id:reply-to; bh=x1NWvuBGAA/+w+UlIaBdpUNzz585hvM0NYVJ+qd0YAo=; b=OLQH03sy1ZFF6PICyDXNwL9lwBrMr6BfA8aaBCsUxYApkVk34ConHenGc2B76qINhQ Ua+WoIL3AFLGf5ZJv5k3IoB56KnXkg4ifATCmEWP4YmSI5rKy7eq+sZXC1feqQ9yrRqK FROb1ZrF0S/PWe70hEmdVtYAnYmr+GyPdkkPriAg4ZJy3zDeXaC74fYyPNEgId3pnFxj fRSZzgB88A6rsef61kXQ2Bw0LB9q0GMkGVsWq5Ci54GLG69a63Bep7zw0v4S7r+wWfxG URM3C5yD+Xow4ZoXx2uozR/6GF7SmkUoArhoaBkRMrF1A+UyMowa DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702306418; x=1702911218; 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-authentication-results:x-original-sender:to:subject :message-id:date:from:in-reply-to:references:mime-version:from:to:cc :subject:date:message-id:reply-to; bh=x1NWvuBGAA/+w+UlIaBdpUNzz585hvM0NYVJ+qd0YAo=; b=MjJ4l4OGDeHL9f/X2i+c6JmmPbzfIQpfbZ1aVOns88BBGTHAI0y1hEvIFqOMHN2ab1 xipvhUt3tAmFdidETNIM2CiTqu5iIJGGCc2I3gbs5aZvhM+FNwnL/ZmqbpJgS8n3FPsp 0HWUgECJihT0ZobhlmNCQX8PBSkTZMxTul4mLheop0jptUMe7miPzl1fmEbtxNfVjt2A T95orIrIYt3hCkhycxoHk5jNAc+42/i9WUZvGl4HmbVLEpN8upgLswte4y7oWy2WNUvM 5hWDijUQnzlvqEyXwsrXP1DgIJlAYIy4DGCf8iCchylXff43zVp8omioPOeSs0okM8 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702306418; x=1702911218; 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-authentication-results:x-original-sender:to:subject :message-id:date:from:in-reply-to:references:mime-version :x-beenthere:x-gm-message-state:sender:from:to:cc:subject:date :message-id:reply-to; bh=x1NWvuBGAA/+w+UlIaBdpUNzz585hvM0NYVJ+qd0YAo=; b=cGFm7KAH9OpORlZZGRgCffuFeMfL1V+qO7nI1u0rOhYuiHp7/p5XFFYdCQEVMd/ZZ6 nYapwohb630fn8ALaK45vRjxTn8daX/eDvtUFv4lXj+Ys45ZseJINzpn+eBVF8du/e34 c/EnNCHwBA+YcfDKSI5MoNfcBxikqHXUmCkAEynjK3gWibXLq3Vu0YwETRXiubHBmFZy QbcYT0mlynwhB2Y1REoLaD+Il4Ph0dZPzfI6DMBfEzigrroPnzANCJpapgNRDEr1Y13y 7m21MJ Original-Sender: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org X-Gm-Message-State: AOJu0YwwrnjPXaIVKhoLjKaIs+3w7U0VTzvvPm9Fi+x0Mn2MkKu/6mPG obGB7+FFOvlQiF2u4cEtt6k= X-Google-Smtp-Source: AGHT+IHE6AWV4EC2ccS61j/5M5dvdzbU5J8LDkrsHOpLrX0j8oER8v8qDmnf1p4FagpGv9flCWYHPA== X-Received: by 2002:a05:622a:1312:b0:423:74a5:a8e with SMTP id v18-20020a05622a131200b0042374a50a8emr7129103qtk.24.1702306418135; Mon, 11 Dec 2023 06:53:38 -0800 (PST) X-BeenThere: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Original-Received: by 2002:a05:622a:651:b0:423:7e09:49c8 with SMTP id a17-20020a05622a065100b004237e0949c8ls224339qtb.1.-pod-prod-00-us; Mon, 11 Dec 2023 06:53:34 -0800 (PST) X-Received: by 2002:a05:620a:284a:b0:77f:7cf5:3739 with SMTP id h10-20020a05620a284a00b0077f7cf53739mr1051512qkp.19.1702306414568; Mon, 11 Dec 2023 06:53:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702306414; cv=none; d=google.com; s=arc-20160816; b=MsV6a2cikIXriGEytaoaD2ov8hGuEYUqn6SSMo3+WqBRSRs581tPuZlpQgCCuEM1lt 2WD4XXxLBauy5eZN5pKvk8j3dysBW8P6J/3IdegCA1uGl+NMGxGdpebY1v/G583ooXQ4 2QytOEkMa5/vjER6mH3URWp6LNPrxT4NoNyf05e6lH29FU/DgGJN9lU0X6emCeIdoLo/ bnxa9OBpaS1LiHXrFTFJtWo77O8uL8ZAX6WRT+WOhUEYa3yepD2Av3SwSQEyKG78RSRG gOP9fmk82AfvNIFtMdt0ss8sz9ZWvpnyIKgHI7kbrV7OSF/7mrf3wwQYDn84Ula0dCgW ed+Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=VyG9M4oUbuO4o+6cANc5i6uqQE639jZRfs2lNYuNGCA=; fh=m01AhCNo7xUywHldCVYouaJypLlN7JgtNYbImzBf4N4=; b=RuqQEhOUPaqhPPiwrfhZVLwGZ7eWs7lNq/ZLuOwMGeg3CzEz61D3Oyn9/Tz/LsJ1zA vnIPfgTMxYUxqTJnQ1ybTHnzVfwPX4ZKconCEN89sjryENqMDK5jx3mIEV7Rha8LRn6Z aXr5x881rxm0Nt9VU6PaVWaMpFXzfp8A0dBszUzkJrcw16Mz4rm+V8D87UqgMUigUQ3A K7sElOEpbY+YW51yTJH2gInRnl+uw7cpnEoqEwKiCrdiKA1nFV4RJRxWc76gBqFNY3XP xIj4IDdawrPdfhiHntcWaki/UL6Svz3BFKKP3PbDBTy9hTYD7iw0yeyj75jwGPnl2VAV IMqQ== ARC-Authentication-Results: i=1; gmr-mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=I4MKC8w4; spf=pass (google.com: domain of pabloserrati-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org designates 2607:f8b0:4864:20::112f as permitted sender) smtp.mailfrom=pabloserrati-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Original-Received: from mail-yw1-x112f.google.com (mail-yw1-x112f.google.com. [2607:f8b0:4864:20::112f]) by gmr-mx.google.com with ESMTPS id v2-20020a05620a0a8200b0077dcadabdc0si534490qkg.5.2023.12.11.06.53.34 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 11 Dec 2023 06:53:34 -0800 (PST) Received-SPF: pass (google.com: domain of pabloserrati-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org designates 2607:f8b0:4864:20::112f as permitted sender) client-ip=2607:f8b0:4864:20::112f; Original-Received: by mail-yw1-x112f.google.com with SMTP id 00721157ae682-5d400779f16so33729837b3.0 for ; Mon, 11 Dec 2023 06:53:34 -0800 (PST) X-Received: by 2002:a81:9291:0:b0:5d6:d420:cb29 with SMTP id j139-20020a819291000000b005d6d420cb29mr2900529ywg.14.1702306413020; Mon, 11 Dec 2023 06:53:33 -0800 (PST) In-Reply-To: X-Original-Sender: pabloserrati-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org X-Original-Authentication-Results: gmr-mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=I4MKC8w4; spf=pass (google.com: domain of pabloserrati-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org designates 2607:f8b0:4864:20::112f as permitted sender) smtp.mailfrom=pabloserrati-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com 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:33506 Archived-At: --0000000000000c0cdf060c3d1941 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hello Lukeflo I have been working for a few months on a project to enable the publication of scientific journals (https://github.com/estedeahora/guri). The project has a workflow that takes `docx` and generates the final files (`pdf`, `xml` and `html`), using an `markdown` (md) as 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/`). So far we are using the proposal in the journal [Quid 16]( https://publicaciones.sociales.uba.ar/index.php/quid16/issue/current) ) if you are interested in seeing the results. My plan is to generate a stable release that can be used by other journals in the coming months. I am open to new ideas and contributions to improve the project. Best, Pablo El lun, 11 dic 2023 a la(s) 08:30, 'William Lupton' via pandoc-discuss ( pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org) escribi=C3=B3: > Luke, > > Filter functions take a single argument (the element in question) so you > need the second form: Figure(fig). > > I think the crash is because the caption is in fig.caption (not in > fig.content[1].caption). See > https://pandoc.org/lua-filters.html#type-figure. > > Perhaps I can put in a plug for https://github.com/pandoc-ext/logging. I > think you might find this helpful for gaining insight into the element > 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-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> wrote: > >> PS: I know I'm using two very different approaches calling the Figure >> function with arguments. Thats due to the fact that I'm not sure which w= ay >> 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 >>> >>> 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) are >>> relativley complex. Here are two things I tried out so far but always g= ot >>> 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' >>> '\\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 *pandoc >>> --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 understand how >>> those writer work. Here I just wanted to replace {figure} with the star= red >>> version {figure*} (not sidecaption): >>> >>> ``` lua >>> function Writer (doc, opts) >>> local filter =3D { >>> Figure =3D function (fig) >>> local tester =3D '\\begin{figure*}\n' .. >>> 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 >>> (field 'caption')stack traceback: [C]: in ? [C]: in method 'walk' >>> test-writer.lua:32: in function 'Writer'stack traceback: >>> test-writer.lua:32: in function 'Writer'* >>> >>> I'm aware that I might be missing something very basic and maybe even >>> very simple. But I'm kind of getting lost a little bit inside all >>> functions, modules etc. as well as the general framework of such writer= s. >>> >>> Thus, any help explaining my errors and 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.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 stackoverflow >>>> 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{\oldcaption}{\caption} and then >>>> setting \RenewDocumentCommand{\caption}{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 more of = a >>>> question for TeX.SE). >>>> >>>> Using a custom writer for building Latex figures and replace the >>>> \caption string inside would be a great solution. I read through the w= riter >>>> manual, but didn't really understand how the AST works and which value= s >>>> have to be used in such a writer. Furthermore, I'm using a a custom La= tex >>>> template 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 whic= h >>>> 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, >>>>> i.e. before it is then converted to LaTeX. A Raw block filter will no= t act >>>>> on Pandoc's LaTeX output, but only on Raw LaTeX blocks that are in th= e >>>>> 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 output (body only) and mo= dify >>>>> it with regular expressions or Lua patterns. To replace just a comman= d name >>>>> this is fairly easy, though longer than the 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 >>>>> redefinition 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 >>>>> function Figure (elem) return { >>>>> pandoc.RawBlock('latex', >>>>> '{\\renewcommand{\\caption}{\\subcaption}'), >>>>> elem, >>>>> pandoc.RawBlock('latex','}') >>>>> } >>>>> 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} >>>>> ... Pandoc's LaTeX for the figure ... >>>>> } >>>>> >>>>> Reposted from >>>>> https://stackoverflow.com/questions/77504584/pandoc-md-latex-write-lu= a-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 wro= te: >>>>> >>>>>> Hi everybody, >>>>>> >>>>>> I have written a custom latex `.cls' file to establish a typesetting >>>>>> workflow for the scientific journals of my research institute. The >>>>>> texts >>>>>> should be written in Markdown and then be processed with `pandoc' to >>>>>> 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 >>>>>> set >>>>>> with `\sidecaption' instead of `\caption' in LaTeX, as well as with = an >>>>>> optional argument (short-caption) 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 function Figure(f) >>>>>> =E2=94=82 local short =3D >>>>>> 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 >>>>>> 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 needs. >>>>>> But >>>>>> since pandoc 3.0 there is the new [/complex figures/] approach and, = so >>>>>> far, I couldn't figure out how to change the LaTeX macro used for th= e >>>>>> 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 >>>>>> 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 whole >>>>>> workflow less portable. >>>>>> >>>>>> Thus, I'm hoping for a hint/a solution in form of a pandoc-lua scrip= t >>>>>> which 1. helps me to achieve the goal, and 2. improve my understandi= ng >>>>>> of lua and the /complex figures/ approach for similar future tasks. >>>>>> >>>>>> I appreciate any tipp! >>>>>> >>>>>> Best, >>>>>> Lukeflo >>>>>> >>>>>> This question is also posted on StackOverFlow: >>>>>> https://stackoverflow.com/q/77504584/19647155 >>>>>> >>>>>> [pandoc repo] >>>>>> >>>>>> >>>>>> [approach of pre pandoc version 3.0 posted] >>>>>> >>>>>> >>>>>> [/complex figures/] >>>>>> >>>>>> [here] >>>>>> >>>>> -- >> You received this message because you are subscribed to the Google Group= s >> "pandoc-discuss" group. >> To unsubscribe from this group and stop receiving emails from it, send a= n >> 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/32dfe8eb-98ac-40ee-92d7= -162528add367n%40googlegroups.com >> >> . >> > -- > 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/CAEe_xxikobOS_G9x71nxtz0= dr99VVhgBV8in%3DxKzXgh7JMaRcw%40mail.gmail.com > > . > --=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/CACTSqG5L4o7npfcUV1iGPMi5fbUrqgPc%2B5ttPQmZhNTFW7Vsng%40mail= .gmail.com. --0000000000000c0cdf060c3d1941 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hello Lukeflo

I have been working for a few months = on a project to enable the publication of scientific journals (https://github.com/es= tedeahora/guri). The project has a workflow that takes `docx` and gener= ates the final files (`pdf`, `xml` and `html`), using an `markdown` (md) as= an intermediate file. One of the issues I had to address was the floating = elements and in particular the use of subcaptions for "sources" a= nd "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.sociales.uba.ar/in= dex.php/quid16/issue/current) ) if you are interested in seeing the res= ults.=C2=A0My plan is to generate a stable release that can be used by othe= r journals in the coming months. I am open to new ideas and contributions t= o improve the project.

Best,

Pablo=C2=A0

El lun, 11 dic 2023 a la(s) 08:30, '= William Lupton' via pandoc-discuss (pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org) escr= ibi=C3=B3:
Luke,

Fil= ter functions take a single argument (the element in question) so you need = the second form: Figure(fig).

I think the crash is becau= se the caption is in fig.caption (not in fig.content[1].caption). See=C2=A0= https://pandoc.org/lua-filters.html#type-figure.

% <= /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>
PS: I know I'm using two very different approaches = calling the Figure function with arguments. Thats due to the fact that I= 9;m not sure which way is the right one...

lukeflo schrieb am Montag, 11. Dez= ember 2023 um 11:33:21 UTC+1:
So far, I took the markdwon writer example from the pandoc docs to try out the gene= ral function of writers. It works and I think that I understand the general= usage.

But especially figures (in latex writer an= d presuambley in general) are relativley complex. Here are two things I tri= ed 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 function Figure (caption, image, attr)
=C2=A0local figcap =3D '\sidecaption= {' .. caption .. '}'
<= /span>=C2=A0return '\\begin{figure}\n' .. image .. '\n' .. = figcap .. '\n' '\\end{figure}\n'
=C2=A0 =C2=A0 =C2=A0 en= d
=C2=A0 =C2=A0}
=C2=A0 =C2=A0return pandoc.write(doc:walk(filter), &= #39;latex', opts)
end
```
If I run this writ= er 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 = 9;Figure'
.

Furthermore, I tried running th= e following code just to understand how those writer work. Here I just want= ed to replace {figure} with 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 '\\be= gin{figure*}\n' .. fig.content[1].caption[1].attributes[1] .. '\\en= d{figure*}\n'
return pa= ndoc.RawBlock('latex', tester)
=C2=A0 =C2=A0 end
=C2=A0 }
= =C2=A0 return pandoc.write(doc:walk(filter), 'latex', opts)
end<= /div>
```
But also got an error:

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

I'm aware that I might be= missing something very basic and maybe even very simple. But I'm kind = of getting lost a little bit inside all functions, modules etc. as well as = the general framework of such writers.

Thus, any h= elp explaining my errors and maybe suggesting some better code is very appr= eciated!

The test file in both cases is very simpl= e:

``` markdown
---
title: A titl= e
---

# Headline

Some text

![caption to an image](c= ounter_plot_new_periods.png)
```

Tha= nks in advance!
lukeflo schrieb am Freitag, 8. Dezember 2023 um 08:35:48 UT= C+1:
Hello = Julien,

thanks for the reply. Unfortunatley, as me= ntioned in the stackoverflow 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{\oldcaption}{\caption} and then setting \RenewDocumentCommand{\caption}{o m}{\sidecaption[#1]{#2}} nothing = changes and the definition of \caption, checked with \me= aning or something similar, stays the same as before (even \De= clareDocumentCommand 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 <= em>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
}


<= div class=3D"gmail_quote">
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
workfl= ow for the scientific journals of my research institute. The texts
shoul= d be written in Markdown and then be processed with `pandoc' to
LaTe= X.

I already have an elaborated pandoc template to produce the LaTeX=
preambel etc. So far its working great.

But for the figures I ne= ed the caption from the Markdown file to be set
with `\sidecaption' = instead of `\caption' in LaTeX, as well as with an
optional argument= (short-caption) for the image attribution in the list
of figures.
To get the latter working I use the following template from a GitHub
d= iscussion 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 function 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.short =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 mac= ro used for the
caption. The older [approach of pre pandoc version 3.0 p= osted] by tarleb
is really intuitive and I could have easily adapted it = to my needs. But
since pandoc 3.0 there is the new [/complex figures/] a= pproach and, so
far, I couldn't figure out how to change the LaTeX m= acro used for the
captions with this new behaviour.

I tried somet= hing 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 raw.text:match('\\caption')
=E2=94=82 =C2=A0 =C2=A0 if capt= ion then
=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 main challenge for me are my more-or-less non-existing lua skills. Ijust never had to use it for my daily tasks. I thought about using `awk&#= 39;
or `sed' to edit the `.tex' file itself using a regex-substi= tution, but
that should remain an absolute stopgap, since it makes the w= hole
workflow less portable.

Thus, I'm hoping for a hint/a so= lution in form of a pandoc-lua script
which 1. helps me to achieve the g= oal, and 2. improve my understanding
of lua and the /complex figures/ ap= proach for similar future tasks.

I appreciate any tipp!

Best,=
Lukeflo

This question is also posted on StackOverFlow: https://stackoverflow.com/q/77504584/19647155

[pando= c repo]
<https://github.com/jgm= /pandoc/issues/7915#issuecomment-1427113349>

[approach of pre= pandoc version 3.0 posted]
<ht= tps://github.com/jgm/pandoc/issues/7915#issuecomment-1039370851>
=
[/complex figures/] <https://github.com/jgm/pand= oc/releases?page=3D2>

[here] <https://st= ackoverflow.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-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org.
To view this discussion on the web visit https= ://groups.google.com/d/msgid/pandoc-discuss/32dfe8eb-98ac-40ee-92d7-162528a= dd367n%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-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org.
To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/CAEe_xxikobOS_G9x= 71nxtz0dr99VVhgBV8in%3DxKzXgh7JMaRcw%40mail.gmail.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://group= s.google.com/d/msgid/pandoc-discuss/CACTSqG5L4o7npfcUV1iGPMi5fbUrqgPc%2B5tt= PQmZhNTFW7Vsng%40mail.gmail.com.
--0000000000000c0cdf060c3d1941--