From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.io/gmane.text.pandoc/33503 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: Mon, 11 Dec 2023 11:30:14 +0000 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="000000000000a429f9060c3a42af" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="37716"; mail-complaints-to="usenet@ciao.gmane.io" To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Original-X-From: pandoc-discuss+bncBCS4HJ6WSAHBBVPF3OVQMGQE56BGK5I-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Mon Dec 11 12:30:35 2023 Return-path: Envelope-to: gtp-pandoc-discuss@m.gmane-mx.org Original-Received: from mail-lf1-f63.google.com ([209.85.167.63]) by ciao.gmane.io with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1rCeUc-0009fo-KI for gtp-pandoc-discuss@m.gmane-mx.org; Mon, 11 Dec 2023 12:30:34 +0100 Original-Received: by mail-lf1-f63.google.com with SMTP id 2adb3069b0e04-50bec76ae18sf3903238e87.3 for ; Mon, 11 Dec 2023 03:30:34 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1702294234; cv=pass; d=google.com; s=arc-20160816; b=t1Iw/EgAF7O2ZtM4HHi9wuxpgxwkFy7V45hh4BsQWd3nk74BKXO17/UHzexQP7tfn3 zMTfzxFekX3m/lGBFddlij4u69LncBqg7rSAmLBMlt40QnMkX/K/cpNADTe76ShFOb2s wDrRwbQzapWhZlA4sWWAs55mFiWzNnbnBscIAJ3nXPtm8N1DYUwfOBajsgJYzKar5uve njOsAHPiuejGx5zzgpXqMnZghjma1fzZsWDQ6O/CBRd7Fegq7MLQQullVs1qkyArQv7/ TObSTZIBT6ZicG+c+jEAh9cZDa4N+EfJDOKRWR/BaKpgFeIpwA6rI/qKbhiO4KazV/PM xY/g== 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=WW0567iMJXU2BAr/H+FBLbfcU82sX3HtrvRFAILitM8=; fh=m01AhCNo7xUywHldCVYouaJypLlN7JgtNYbImzBf4N4=; b=q7Je23ByKALGWzRqlVB4yzYxGjSPQhT9yz6O2vmDRiQGinVdKBTKq/yUZGsTM9O5xf Z/uK0hGwixS6vAR+uUSbSB58VX7Ip6snnjpYFoPIvuybOBGyDgTuK8i/gnK7AEpuM0lf YYcjAZzUTdFp1g/jGlZ6lNn6w/ffLLPZLO9DegxOyu2JxPeE2yOd+WxTsLXzOD8Vfwxw o+CgoumPDEm+80awuxmCilezRVn0pR9OU19P0u4EoqDlFUSjeg2DvdQQBAHuQ8L7HVsd K1A3XJkffXuEQ8fwjv5fkCxeWgZmirAV48qZkaj0yofuBMrtpue2tRvVPXD3zkGyhLZ8 JLqg== ARC-Authentication-Results: i=2; gmr-mx.google.com; dkim=pass header.i=@broadband-forum.org header.s=google header.b="Nw/DO0Uz"; spf=pass (google.com: domain of wlupton-QSt+ys/nuMyEUIsrzH9SikB+6BGkLq7r@public.gmane.org designates 2a00:1450:4864:20::632 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=1702294234; x=1702899034; 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=WW0567iMJXU2BAr/H+FBLbfcU82sX3HtrvRFAILitM8=; b=dISsptzBfz4RX1Hcd0km7Nal73pJL48V3Jxj5lCKs4X6kUiP6OjKpiGVRpgE9+LhY3 RtoGPgIrhX80ALY+Uj9sCfMECz67ydDYJoHn6KSGFeBt3QKhbZ56Fz9/8mcNxXMnXOer LuhXwJDRljvR76siZUuFn/Hduh5/P8AnbZjkvfd23VzsYvv9t3wJD5vz09vshVhm/ucT ekia9UbmcTuMMS+SKrjM1OOtVab3Co0A1sqkAO9FaRyevpkweF/BUqGX/mYwbhHWpBUy ax+DEuny9pYz+MYclnljFn/qoO/YoyLp4wGLRZs+nAPSAPzmx08wC0xBsfO X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702294234; x=1702899034; 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=WW0567iMJXU2BAr/H+FBLbfcU82sX3HtrvRFAILitM8=; b=Dsak6Wx+CEPs80SfYVY6cKVN2EVB5LrbPPq3/LB6r/TlxukM3sOEhzd1lVzPGCyUe9 zumDjLhRsaxr+pyMQn638mrwQyXogYm7CQbhIoezmCAARIpNv+/2ov4q0corRBfdGQ62 qGysfxIAp3QWTtRjwGMPuO3oqvuUpVChKpm0G8yZwnNrdJ+uGfygYovnsaL88oxYt9MV vC4FVbNrcaNwvsLlhtJmw3xlMXKE6LPRMThgZa/Jjd5wNs6Hchg7melMmpge6PmT6nnT xSZcDF5/ivBVl X-Gm-Message-State: AOJu0Yy+MdRFqiNnFQhYIPCwEhst6KtfSSZQZJiVzzBWhcBz9//KNHzc UzCPxY17CnFThYf8piv6KAo= X-Google-Smtp-Source: AGHT+IGU8iULmOxIa7CB7RZomjV/ZkjJhwG1VIvdTYzzpbY3lqo4vDwLHrZQJT/1w04oKWJ5gWCDnQ== X-Received: by 2002:a05:6512:104d:b0:50b:f380:5fad with SMTP id c13-20020a056512104d00b0050bf3805fadmr2496074lfb.87.1702294233407; Mon, 11 Dec 2023 03:30:33 -0800 (PST) X-BeenThere: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Original-Received: by 2002:a05:6512:3d1c:b0:50b:f483:50fa with SMTP id d28-20020a0565123d1c00b0050bf48350fals1113446lfv.0.-pod-prod-09-eu; Mon, 11 Dec 2023 03:30:27 -0800 (PST) X-Received: by 2002:ac2:4423:0:b0:50b:ccbf:6fb8 with SMTP id w3-20020ac24423000000b0050bccbf6fb8mr1767550lfl.56.1702294227149; Mon, 11 Dec 2023 03:30:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702294227; cv=none; d=google.com; s=arc-20160816; b=Enz8UlYcUasyysP4HID7kGS1FPe0ir8hYZdFjuNg7Mulh9tKAtXzUa7tLj8w7OdU7k zgYWDxb2gEw0Hq4BfWTYPf2dmsGSJBvy0mhOl9YWb7ibPewo7zekQ4pU6cAD/kOCpNg1 omDgNuIzYi/vsl7kuT4HQRxUbNuI0dcBn0TYm6v84w/CBJQ5oeaXJD5aL6Mbiyuiww5Q s9a0K/TvMbkHulzDV8IKHNhTmAkE5pFdejQtDFDKeZuawwlfCmf8BfW1K9rB5eDKRrB+ uXMtWnUs8mJ4Cswyny9LXeh1D5C7/fTjbOsXsgat90KkV3LBa3Y2docnmlIFiDucl98W 3aRg== 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=t/AkCaRM8VXTAQsBin6/SzArhDeCzaPkagVzHVY8GD8=; fh=m01AhCNo7xUywHldCVYouaJypLlN7JgtNYbImzBf4N4=; b=CGp81UUuzkRcS/mIbbXxWgu+1HOCXNW57fLenwvr/5QXtHzHlG2UOEKo0AVXvcBUTF u2TWn84yNXXC517uFK2Ilrs0DphveU0kgCv38ELzw/BRpLv3n0CNwKesvaLQHtGeNF27 Xg+29b734F2aX9FAgZt3FlYeHCHvW3hpnQzP3dS/i+7e/uyaAt4k28ApKK+anHLI9tXQ mVMirHNtlQHCXoX9bceWW1fIh3J2BdA8fAKqIacpS9/Lt49A30eVulC2FrBFiO0ERwn1 WanTpZwUoq/830WfihGVe23e6awKakCD/xTcARsg9bxC1MieeU+Pweowq/qRG5DqL/4D t6qA== ARC-Authentication-Results: i=1; gmr-mx.google.com; dkim=pass header.i=@broadband-forum.org header.s=google header.b="Nw/DO0Uz"; spf=pass (google.com: domain of wlupton-QSt+ys/nuMyEUIsrzH9SikB+6BGkLq7r@public.gmane.org designates 2a00:1450:4864:20::632 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-ej1-x632.google.com (mail-ej1-x632.google.com. [2a00:1450:4864:20::632]) by gmr-mx.google.com with ESMTPS id e15-20020a05651236cf00b0050c0f4fbbd4si345200lfs.8.2023.12.11.03.30.26 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 11 Dec 2023 03:30:27 -0800 (PST) Received-SPF: pass (google.com: domain of wlupton-QSt+ys/nuMyEUIsrzH9SikB+6BGkLq7r@public.gmane.org designates 2a00:1450:4864:20::632 as permitted sender) client-ip=2a00:1450:4864:20::632; Original-Received: by mail-ej1-x632.google.com with SMTP id a640c23a62f3a-a1e35c2807fso576781766b.3 for ; Mon, 11 Dec 2023 03:30:26 -0800 (PST) X-Received: by 2002:a17:906:dfcb:b0:a19:a1ba:da5c with SMTP id jt11-20020a170906dfcb00b00a19a1bada5cmr1789929ejc.131.1702294225949; Mon, 11 Dec 2023 03:30:25 -0800 (PST) In-Reply-To: <32dfe8eb-98ac-40ee-92d7-162528add367n-/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="Nw/DO0Uz"; spf=pass (google.com: domain of wlupton-QSt+ys/nuMyEUIsrzH9SikB+6BGkLq7r@public.gmane.org designates 2a00:1450:4864:20::632 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:33503 Archived-At: --000000000000a429f9060c3a42af Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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-figur= e . 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 wa= y > 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 go= t >> 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 starr= ed >> 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 writers= . >> >> Thus, any help explaining my errors and maybe suggesting some better cod= e >> 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 wr= iter >>> 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 Lat= ex >>> 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 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, >>>> i.e. before it is then converted to LaTeX. A Raw block filter 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 mod= ify >>>> it with regular expressions or Lua patterns. To replace just a command= 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 wrot= e: >>>> >>>>> 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 s= et >>>>> with `\sidecaption' instead of `\caption' in LaTeX, as well as with a= n >>>>> optional argument (short-caption) for the image attribution in the li= st >>>>> 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['sho= rt-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 t= he >>>>> 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. B= ut >>>>> since pandoc 3.0 there is the new [/complex figures/] approach and, s= o >>>>> 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, b= ut >>>>> 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 understandin= g >>>>> 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-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 > > . > --=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_xxikobOS_G9x71nxtz0dr99VVhgBV8in%3DxKzXgh7JMaRcw%40mail= .gmail.com. --000000000000a429f9060c3a42af Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
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= =C2=A0https://p= andoc.org/lua-filters.html#type-figure.

Perhap= s I can put in a plug for=C2=A0https://github.com/pandoc-ext/logging. I think you might find thi= s helpful for gaining insight into the element structure. See example below= .

Hope this helps.

Cheers= ,
William

--------

<= div>With this filter in rep.lua:

local logging =3D require '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


...your input give= s this:

% pandoc luke.md -L rep.lu= a

(#) blocks Blocks[3] {

=C2=A0 [1] He= ader {

=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] Pa= ra {

=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] Fi= gure {

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

=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">Headline</h1>

<p>Some text</p>

<figure>

<img src=3D"counter_plot_new_periods.png" alt=3D&qu= ot;caption to an image" />

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

</figure>


<= div dir=3D"ltr" class=3D"gmail_attr">On Mon, 11 Dec 2023 at 10:36, 'luk= eflo' via pandoc-discuss <pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> wrote:
PS: I know I'm using two very d= ifferent approaches calling the Figure function with arguments. Thats due t= o the fact that I'm not sure which way is the right one...

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


<= /div>
``` lua
function Writer (doc, opts)
=C2=A0 =C2= =A0local filter =3D {
=C2=A0 =C2=A0 =C2=A0 function Figure (caption, ima= ge, attr)
=C2=A0local figca= p =3D '\sidecaption{' .. caption .. '}'
=C2=A0return '\\begin{figure}\n' .. im= age .. '\n' .. figcap .. '\n' '\\end{figure}\n'
= =C2=A0 =C2=A0 =C2=A0 end
=C2=A0 =C2=A0}
=C2=A0 =C2=A0return pandoc.wr= ite(doc:walk(filter), 'latex', opts)
end
```
=
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-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 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_xxikobOS_G9x71nxtz0dr99VVhgBV8in%3= DxKzXgh7JMaRcw%40mail.gmail.com.
--000000000000a429f9060c3a42af--