From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.io/gmane.text.pandoc/33512 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: "'William Lupton' via pandoc-discuss" Newsgroups: gmane.text.pandoc Subject: Re: Lua filter to change macro for figure caption md -> latex Date: Tue, 12 Dec 2023 10:16:36 +0000 Message-ID: References: <51ca8210-3d60-4d5d-9af2-04c85995deb6n@googlegroups.com> <32dfe8eb-98ac-40ee-92d7-162528add367n@googlegroups.com> <78503784-88bd-4d34-b5f5-a8634d667ba0n@googlegroups.com> Reply-To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="00000000000028c141060c4d5914" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="1415"; mail-complaints-to="usenet@ciao.gmane.io" To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Original-X-From: pandoc-discuss+bncBCS4HJ6WSAHBBE7G4CVQMGQEMSRBZ3Q-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Tue Dec 12 11:16:57 2023 Return-path: Envelope-to: gtp-pandoc-discuss@m.gmane-mx.org Original-Received: from mail-wm1-f60.google.com ([209.85.128.60]) by ciao.gmane.io with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1rCzot-00009X-Qo for gtp-pandoc-discuss@m.gmane-mx.org; Tue, 12 Dec 2023 11:16:55 +0100 Original-Received: by mail-wm1-f60.google.com with SMTP id 5b1f17b1804b1-40c1e063438sf42129165e9.1 for ; Tue, 12 Dec 2023 02:16:55 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1702376215; cv=pass; d=google.com; s=arc-20160816; b=SysGshJy4nd/koBKffchCrYjlK1YPlPFPPgiCLu1ePH70NhV6Rd/pV+Yx6iDetQHJq tdkzoaQlDFVdgdZv0sbEDNu44CRa3KXuuL1ux5wBq3la/ItorsjF8aKimwHAoj3aJgBU OCXBjrdXM1OfrVjdVpRdeEw2DKx0K91WHxH12wsn6AItOZy+KQEO9Sz8vSI4rvgn4EGz 96/gj/i7RNvV8UnhuqAST2j9lHjx+GlHSAvW9p9mu4R6upvCmq9dKgvBtk/SDyFR7N4S u63XLV92a/Xz3h+d6wy0SAwSa4y4fX7wHehvarwdfyI3EcStjoUfQ+mdfz9vrZZyPbMw BU/Q== 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:dkim-signature; bh=Kcm9KsQPpcXczR05EKxWKzdNZYgR4B63KYb00gLV6Gw=; fh=m01AhCNo7xUywHldCVYouaJypLlN7JgtNYbImzBf4N4=; b=dYZNmpOUp7f/mt+52hxBRXvY4nUOeJRe9qxTUl1EW+ejyQOeV6JHmWvrQPbyoGS6eH h5+yXwNXNsLhJ3MYpoApvO4VXyRT57GUYs/zBJAr8y8K1d1H178A5OZ3lSvCQAo8qNxj eLYx5wBW+DhiR0f9vVqN/eFqF47J3CGxGGi8HnrwplZ+2fvwkvYrNSxZZP6E8AzhwvyB TF+u+jaTTdJpZS8M06sjcU7UM7d4h/Chy25/PLUqVjlchRkU1YXElVp8d/8FBBysgsJX XQ22VKTGPTh389iD67QMMSyuupDrLOJp8vXUpkyVjiedIMK282sGJwN7V1yOc2no405Q 8Wbg== ARC-Authentication-Results: i=2; gmr-mx.google.com; dkim=pass header.i=@broadband-forum.org header.s=google header.b=mFPzuQbz; spf=pass (google.com: domain of wlupton-QSt+ys/nuMyEUIsrzH9SikB+6BGkLq7r@public.gmane.org designates 2a00:1450:4864:20::129 as permitted sender) smtp.mailfrom=wlupton-QSt+ys/nuMyEUIsrzH9SikB+6BGkLq7r@public.gmane.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=broadband-forum.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=20230601; t=1702376215; x=1702981015; 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=Kcm9KsQPpcXczR05EKxWKzdNZYgR4B63KYb00gLV6Gw=; b=xzg1LIrbFSMhMeEKBISsfrvJUucZo5oqW09PXsTDu6kM7iTldbOx6RLDGF0YQScKOH UToLQtig6LOG/73EH8+MLO1nQsL9X7RhS2kopH04tptbeW52jRMMpKURffZ37fXi91Ma uVFX28XyIKH5ZUxe4hlNwSSrg8Dta1Kygs1D5QDiVIcA7qOIakxdwUoeJo8+eSBrrG+2 Ho8L/D7959/7NWUIG+4TOs3NE52Cw3+mFZQlUwnLxGTx9YXJ6aLZ4Uk26vTi0apgCG5M DhcBYABAbvW4yxxnLzgXdXjBJek0Mg500h23wChFbABLtB83PImF18fV4ha X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702376215; x=1702981015; 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:from:to:cc:subject:date:message-id :reply-to; bh=Kcm9KsQPpcXczR05EKxWKzdNZYgR4B63KYb00gLV6Gw=; b=MqnVQwSyMf5v2P+YNrbD9dEu7kSiVh84Pq4OG4pwu+4vODWlRIs3ShT2NC9xm4rh13 nKpVMSrEXjqx/gw5WiiHLyF4rfVGziN2zM7EzlcxBw29bijDbDk6YeepxlMeLmz7ZwXl 70fmdl9Ku/yDFIqNsyqHcsTXspa7e5Bky9bn8VWHiI83IoEURhPEMkH5HUIg/Z/NC9yt NnWqU+PVkrD9bXWounPn1HxthzdkofVE9s3TfHhBC/5UQrQQFspdoIxEnUT/w/J8+uq0 PfO/+J47pdT1C X-Gm-Message-State: AOJu0Yzf2x1h+ynacAtA+xLxc409nHNNEB+X5zgViHA2qZH8TbpRfjoT 8etYfEuYlg/rsq8mwj0YySA= X-Google-Smtp-Source: AGHT+IFZPH9RqQJLJuJRGnx6hYZ6OtgemZHGe1iGKzKI5Vw3Ji+o5geRwMN5rBHee0ZtmfHel2SS4Q== X-Received: by 2002:a05:600c:46c6:b0:40c:2ba6:809 with SMTP id q6-20020a05600c46c600b0040c2ba60809mr3055454wmo.157.1702376215115; Tue, 12 Dec 2023 02:16:55 -0800 (PST) X-BeenThere: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Original-Received: by 2002:a5d:6246:0:b0:333:2fd6:cf36 with SMTP id m6-20020a5d6246000000b003332fd6cf36ls690663wrv.2.-pod-prod-05-eu; Tue, 12 Dec 2023 02:16:50 -0800 (PST) X-Received: by 2002:a05:600c:1688:b0:40b:5e1d:839f with SMTP id k8-20020a05600c168800b0040b5e1d839fmr3196330wmn.51.1702376209495; Tue, 12 Dec 2023 02:16:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702376209; cv=none; d=google.com; s=arc-20160816; b=mS/VAC3xlwlbASngAy6psLJ9ud2gpERIVjlEAuAxoK1rEMBs/j1rNIXTefm742FoDv poMIU3nsVtdNYZoDpjUJIaAUfgkmyD0lHQ+wXyFJYnGxt326C/7HAJ2kil1cR9Qgj6Js 0IMu3Poks97JRl6TKwQU7gGcaQfAKctryrZIVVb5r/gY3SLDz5nTWTpUOsRxc4mSYeTm 65uKRv71acKygCaJXPAddDRE6sh02ILWK2gUjAKnNV+ZwPNJHbLd/gL6ASCY1ot2QOIk CsuP8v0yhWNjDQwVjUOji4vfxDNUG5/SQFfJfKSzFbYdthD8B/AO6A1s0m+rPW3YZZrs PX+g== 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=1g7oMYNqGfaJDcLRCaFtl51NNPGiHxB1cEa9rdxxKOM=; fh=m01AhCNo7xUywHldCVYouaJypLlN7JgtNYbImzBf4N4=; b=v0fjJtoUoGcd7TyhnXKeqgGo0SxTMcxdzHlXf9zK0sSQQfDwCmAVz/hyUju4mbzd+M mqwdqKra2964Y1X9XDPPOtCqTTyA4WmyoT7oyN3hxAqKyETvaBaJJDHRP8L2TfVSAZ5z 2gaW4c+3JtsJi8hDFiPnsuh5RWKSrLEkHtLcvL+qoRJNpFuPly/oklLOxYyuhQdJOEqI MEoe0CxQVIBifWRVPU0ckT6ko5EluOH9C6FO1TvnyvyxdbJU6Fq7J3OdROY20Tw+MB6U m4fsVAdAVu2GXrTKeerpLkpQFgk4wF19xPsrpY5WOcp4iKD+5PW1e0V7EGCcuquKkiZw 4i8w== ARC-Authentication-Results: i=1; gmr-mx.google.com; dkim=pass header.i=@broadband-forum.org header.s=google header.b=mFPzuQbz; spf=pass (google.com: domain of wlupton-QSt+ys/nuMyEUIsrzH9SikB+6BGkLq7r@public.gmane.org designates 2a00:1450:4864:20::129 as permitted sender) smtp.mailfrom=wlupton-QSt+ys/nuMyEUIsrzH9SikB+6BGkLq7r@public.gmane.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=broadband-forum.org Original-Received: from mail-lf1-x129.google.com (mail-lf1-x129.google.com. [2a00:1450:4864:20::129]) by gmr-mx.google.com with ESMTPS id m30-20020a05600c3b1e00b0040a25ec1cfesi39191wms.0.2023.12.12.02.16.49 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 12 Dec 2023 02:16:49 -0800 (PST) Received-SPF: pass (google.com: domain of wlupton-QSt+ys/nuMyEUIsrzH9SikB+6BGkLq7r@public.gmane.org designates 2a00:1450:4864:20::129 as permitted sender) client-ip=2a00:1450:4864:20::129; Original-Received: by mail-lf1-x129.google.com with SMTP id 2adb3069b0e04-50d176eb382so5032085e87.2 for ; Tue, 12 Dec 2023 02:16:49 -0800 (PST) X-Received: by 2002:a05:6512:36c4:b0:50b:ffb9:7a4e with SMTP id e4-20020a05651236c400b0050bffb97a4emr2336765lfs.46.1702376208114; Tue, 12 Dec 2023 02:16:48 -0800 (PST) In-Reply-To: <78503784-88bd-4d34-b5f5-a8634d667ba0n-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> X-Original-Sender: wlupton-QSt+ys/nuMyEUIsrzH9SikB+6BGkLq7r@public.gmane.org X-Original-Authentication-Results: gmr-mx.google.com; dkim=pass header.i=@broadband-forum.org header.s=google header.b=mFPzuQbz; spf=pass (google.com: domain of wlupton-QSt+ys/nuMyEUIsrzH9SikB+6BGkLq7r@public.gmane.org designates 2a00:1450:4864:20::129 as permitted sender) smtp.mailfrom=wlupton-QSt+ys/nuMyEUIsrzH9SikB+6BGkLq7r@public.gmane.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=broadband-forum.org X-Original-From: William Lupton 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:33512 Archived-At: --00000000000028c141060c4d5914 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable The caption looks like this (pasted from earlier). 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" } } } } It's of type Caption (https://pandoc.org/lua-filters.html#type-caption) and I believe that you can assume that the long caption is present (because the docs say "The caption ... with an optional short caption") but perhaps you should check. So, in your code, fig.caption.long will be of type Blocks ( https://pandoc.org/lua-filters.html#type-blocks). The caption could contain formatting but if you don't care about this (you could always circle back later) you can just stringify it, i.e., pandoc.utils.stringify(figure.caption.long) ( https://pandoc.org/lua-filters.html#pandoc.utils.stringify). The docs say that you can only stringify an element but it seems to work with Blocks (probably via some type coercion). My logging output was just running pandoc with the (default) HTML output. It wasn't running your filter. On Mon, 11 Dec 2023 at 22:04, 'lukeflo' via pandoc-discuss < pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> wrote: > Dear William, dear Pablo, > > thanks for your replies. It does indeed help me to understand the way suc= h > writers/filters work. But unfortunatley, I can't produce a working filter > on my own. > > @William: I tried using 'fig.caption' instead of 'fig.content.caption[1]'= . > But then I get a different error message: *Error running Lua: attempt to > concatenate a table value (field 'caption')*. > Thus, I'm a little bit confused, since the html output at the end of your > logging output looks like most fields are processed correctly. > > @Pablo: Your filters are really interesting. The float code gave me some > usefule insights. I also tried to adapt your Latex code: > > ``` lua > function Figure(elem) > raw_elem =3D '\\begin{figure*}\n' .. > '\\centering\n' .. > '\\includegraphics[width=3D0.9\\textwidth]{' .. elem.conten= t > .. '}\n' .. > '\\caption{' .. elem.caption .. '}\n' .. > '\\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 > access the values of the different AST fields. How, for example, can I us= e > the values of the different fields of a Pandoc Figure Block > ? Caption is there defin= ed > as key-value pair for > long and short captions, but as mentioned 'fig.caption' oder 'elem.captio= n' > throws the table value error. Might that be due to the key-value form? An= d > 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 example that shows how to access a value and > put it in a framework with latex-code surrounded. The docs are very > detailed, but I don't find an explanation of these basics which makes sen= se > 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 >> publication of scientific journals (https://github.com/estedeahora/guri)= . >> The project has a workflow that takes `docx` and generates the final fil= es >> (`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 i= n >> 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-...-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org) escribi=C3=B3: >> >>> Luke, >>> >>> Filter functions take a single argument (the element in question) so yo= u >>> 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 elemen= t >>> 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 >>>> function with 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: >>>> >>>>> 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 al= ways >>>>> got 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 *pand= oc >>>>> --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 ho= w >>>>> those writer work. Here I just wanted to replace {figure} with the st= arred >>>>> 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 writ= ers. >>>>> >>>>> 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= writer >>>>>> manual, but didn't really understand how the AST works and which val= ues >>>>>> have to be used in such a writer. Furthermore, I'm using a a custom = Latex >>>>>> template for exporting (based on the default.template.latex) which h= as 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 accomplish the substitutio= n. >>>>>> >>>>>> 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 fi= lter >>>>>>> will not act on Pandoc's LaTeX output, but only on Raw LaTeX blocks= that >>>>>>> are in 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 output (body only) and = modify >>>>>>> it with regular expressions or Lua patterns. To replace just a comm= and 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-= lua-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 w= rote: >>>>>>> >>>>>>>> Hi everybody, >>>>>>>> >>>>>>>> I have written a custom latex `.cls' file to establish a typesetti= ng >>>>>>>> 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 b= e >>>>>>>> set >>>>>>>> with `\sidecaption' instead of `\caption' in LaTeX, as well as wit= h >>>>>>>> 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 GitH= ub >>>>>>>> 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 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 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 >>>>>>>> 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 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 >>>>>>>> script >>>>>>>> which 1. helps me to achieve the goal, and 2. improve my >>>>>>>> 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: >>>>>>>> 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 >>>> 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/32dfe8eb-98ac-40ee-92= d7-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-discus...-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >>> >> To view this discussion on the web visit >>> https://groups.google.com/d/msgid/pandoc-discuss/CAEe_xxikobOS_G9x71nxt= z0dr99VVhgBV8in%3DxKzXgh7JMaRcw%40mail.gmail.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/78503784-88bd-4d34-b5f5-= a8634d667ba0n%40googlegroups.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/CAEe_xxj7N7897P6r_kUzwjbG3WDko38_2O178%2BWtZhQXNjuiPA%40mail= .gmail.com. --00000000000028c141060c4d5914 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
The caption looks like this (pasted from = earlier).

=C2=A0 =C2=A0=C2=A0caption: {

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

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

=C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0=C2=A0content: 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 "a= n"

=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}<= /span>

<= span style=3D"font-variant-ligatures:no-common-ligatures">=C2=A0 =C2=A0 =C2= =A0=C2=A0}

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

= =C2=A0


It's of type Caption (https://pandoc.org/l= ua-filters.html#type-caption) and I believe that you can assume that th= e long caption is present (because the docs say "The caption ... with = an optional short caption") but perhaps you should check.
So, in your code, fig.caption.long will be of type Blocks (https://pandoc.org= /lua-filters.html#type-blocks).

The caption co= uld contain formatting but if you don't care about this (you could alwa= ys circle back later) you can just stringify it, i.e.,=C2=A0pandoc.utils.st= ringify(figure.caption.long) (https://pandoc.org/lua-filters.html#pandoc.utils.= stringify). The docs say that you can only stringify an element but it = seems to work with Blocks (probably via some type coercion).

=
My logging output was just running pandoc with the (default) HTM= L output. It wasn't running your filter.

On Mon, 11 Dec= 2023 at 22:04, 'lukeflo' via pandoc-discuss <pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> wr= ote:
Dear W= illiam, dear Pablo,

thanks for your replies. It do= es indeed help me to understand the way such writers/filters work. But unfo= rtunatley, I can't produce a working filter on my own.

@William: I tried using 'fig.caption' instead of 'fig.= content.caption[1]'. But then I get a different error message: Error= running Lua: attempt to concatenate a table value (field 'caption'= ).
Thus, I'm a little bit confused, since the html output= at the end of your logging output looks like most fields are processed cor= rectly.

@Pablo: Your filters are really interestin= g. The float code gave me some usefule insights. I also tried to adapt your= Latex code:

``` lua
function Figure(ele= m)
=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.content .. '}\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('latex', raw_e= lem)
end
```
I run it as filter instead as writer. B= ut get the same error as above.

Generally, my bigg= est problem is that I don't fully understand how to access the values o= f the different AST fields. How, for example, can I use the values of the d= ifferent fields of a Pandoc Figure Block? Caption is there def= ined as key-value pair for long and short captions, but as mentioned &#= 39;fig.caption' oder 'elem.caption' throws the table value erro= r. Might that be due to the key-value form? And is there a difference acces= sing these things for a filter instead of a writer?

If someone has some informations on that, it would be great; even bet= ter with a (short) framework or example that shows how to access a value an= d put it in a framework with latex-code surrounded. The docs are very detai= led, but I don't find an explanation of these basics which makes sense = to me...

Thanks again and all the best!
=
Pablo Serrati schrieb am Montag, 11. Dezember 2023 um 15:53:38 UTC+1:
<= /div>
Hel= lo Lukeflo

I have been working for a few months on a project to enab= le the publication of scientific journals (https://github.com/ested= eahora/guri). The project has a workflow that takes `docx` and generate= s 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 ele= ments and in particular the use of subcaptions for "sources" and = "notes". While the project is still in the development stage, I t= hink some of the filters I use may help you (see `files/filters/`).=C2=A0

So far we are using the proposal in the journal [Qui= d 16](https://publicaciones.social= es.uba.ar/index.php/quid16/issue/current) ) if you are interested in se= eing the results.=C2=A0My 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 con= tributions to improve the project.

Best,
=

Pablo=C2=A0

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 elemen= t 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.con= tent[1].caption). See=C2=A0https://pandoc.org/lua-filte= rs.html#type-figure.

Perhaps I can put in a pl= ug for=C2=A0https://github.com/pandoc-ext/logging. I think yo= u might find this helpful for gaining insight into the element structure. S= ee example below.

Hope this helps.

<= /div>
Cheers,
William

--------
=

With this filter 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> 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 way is the right one...

lukeflo schr= ieb am Montag, 11. Dezember 2023 um 11:33:21 UTC+1:

=
If I run this writer with my custom template from the CLI using=C2=A0<= b>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'
.

Furtherm= ore, I tried running the following code just to understand how those writer= work. Here I just wanted to replace {figure} with the starred version {fig= ure*} (not sidecaption):

``` lua
functio= n Writer (doc, opts)
=C2=A0 local filter =3D {
=C2=A0 =C2=A0 =C2=A0Fi= gure =3D function (fig)
loc= al tester =3D '\\begin{figure*}\n' .. fig.content[1].caption[1].att= ributes[1] .. '\\end{figure*}\n'
return pandoc.RawBlock('latex', tester)
=C2=A0 =C2= =A0 end
=C2=A0 }
=C2=A0 return pandoc.write(doc:walk(filter), 'la= tex', 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-writ= er.lua:32: in function 'Writer'

I'= m aware that I might be missing something very basic and maybe even very si= mple. But I'm kind of getting lost a little bit inside all functions, m= odules etc. as well as the general framework of such writers.
Thus, any help explaining my errors and maybe suggesting some b= etter code is very appreciated!

The test file in b= oth 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. Dezem= ber 2023 um 08:35:48 UTC+1:
Hello Julien,

thanks for the repl= y. Unfortunatley, as mentioned in the stackoverflow post, your suggested La= TeX code won't work.

The \caption= macro is very complex in the backend and cannot be copied on the fly via <= span>\let, \NewCommandCopy or something similar. Even a= fter 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 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-discus...-/JYPxA39Uh5TLH3MbocFFw@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 &= quot;pandoc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an e= mail to pandoc-discus...-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org.

--
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/78503784-88bd-4d34-b5f5-a8634d6= 67ba0n%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 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/CAEe_xxj7N7897P6r_kUzwjbG3WDko38_2O178%= 2BWtZhQXNjuiPA%40mail.gmail.com.
--00000000000028c141060c4d5914--