From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.io/gmane.text.pandoc/33454 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Julien Dutant Newsgroups: gmane.text.pandoc Subject: Re: Lua filter to change macro for figure caption md -> latex Date: Tue, 5 Dec 2023 08:09:19 -0800 (PST) Message-ID: References: <51ca8210-3d60-4d5d-9af2-04c85995deb6n@googlegroups.com> Reply-To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_Part_132577_616975624.1701792559348" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="14134"; mail-complaints-to="usenet@ciao.gmane.io" To: pandoc-discuss Original-X-From: pandoc-discuss+bncBC5Y3356IYILDFV5VMDBUBFGBXY5I-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Tue Dec 05 17:09:26 2023 Return-path: Envelope-to: gtp-pandoc-discuss@m.gmane-mx.org Original-Received: from mail-oa1-f55.google.com ([209.85.160.55]) by ciao.gmane.io with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1rAXzA-0003Pe-SI for gtp-pandoc-discuss@m.gmane-mx.org; Tue, 05 Dec 2023 17:09:24 +0100 Original-Received: by mail-oa1-f55.google.com with SMTP id 586e51a60fabf-1fb36840642sf4621645fac.3 for ; Tue, 05 Dec 2023 08:09:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=20230601; t=1701792563; x=1702397363; darn=m.gmane-mx.org; h=list-unsubscribe:list-subscribe:list-archive:list-help:list-post :list-id:mailing-list:precedence:reply-to:x-original-sender :mime-version:subject:references:in-reply-to:message-id:to:from:date :sender:from:to:cc:subject:date:message-id:reply-to; bh=lzf3BWQqOhexSbpzxFRmYZmIx+bsC2bB3u+fDl1xVNw=; b=vVEaem35PFuc8MRXqNjQCboy5jfbk8PXHzDbCPBN5Yq1PfSuneV6YqgBXuGPsVGwbl hqmwxZcmm00oMUCY/wmUzblpkMmc+5a6XSiO4tJlEAhrvLHtYrDoTk+YuNN0stfdjpo0 JHxb2lqRcI+5iayDVKqdMNrHGylLRKNIvro6xoj6vUUJ3ZJ0vjM2hcQFc5TqZrzJ0m3q 5iDosnecnPyzh615UhNK3puFq8yQC8Imb5HcIDJyteLT4LMykznVaNE9ktPXjCCyC6Ga 2gFlByGsrD8HVxt0vMbjOkuwwCMSzXXLDr44HtMr2TIPoDioWgRllv0Y2eca9+JD4ZDD m1+g== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701792563; x=1702397363; darn=m.gmane-mx.org; h=list-unsubscribe:list-subscribe:list-archive:list-help:list-post :list-id:mailing-list:precedence:reply-to:x-original-sender :mime-version:subject:references:in-reply-to:message-id:to:from:date :from:to:cc:subject:date:message-id:reply-to; bh=lzf3BWQqOhexSbpzxFRmYZmIx+bsC2bB3u+fDl1xVNw=; b=lynNSxNa0ntoURGpLzCvwHfbriwLEp2zgd96vVfA4FDhf4KBk54oubd4t1E7s7l9cl ENbQLwbpEqqbGHaScNki3h0KF4NHmKvQq2Ns5CmHQwc8iDJMjKzXsYf9RfznacZzT0Nz Hm48oagK+NA+uKqzaYjHWjK3G0kr1a86njRZCCLW068JfGzXuS0xcgHPrsikm8B6WN3p rusWAMtNPZhF42QP3KGCTOYneKAgt+RtvIM50yh6Nvn1JQcCqneG6MBytLCIeUG0eidX 57DvGeq6gM+bf3tDMkOe7Q/yd1QGOtVLeAZHDgUrMr/iDZI92Rb2lhGdSugudtPV2d8r cIcg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701792563; x=1702397363; h=list-unsubscribe:list-subscribe:list-archive:list-help:list-post :x-spam-checked-in-group:list-id:mailing-list:precedence:reply-to :x-original-sender:mime-version:subject:references:in-reply-to :message-id:to:from:date:x-beenthere:x-gm-message-state:sender:from :to:cc:subject:date:message-id:reply-to; bh=lzf3BWQqOhexSbpzxFRmYZmIx+bsC2bB3u+fDl1xVNw=; b=S/k5fE2Hj+msqdV8vBpWeZvCX2OlcJdGTsPqKn4EiU2Qn1Y8OfSwM8HV098D3S4ZpQ EZq1IzJY3X5Bg4KmU/mEPTaXXGBRXlSBFYHerU2ePRrb/zhw8mTYORmKSp4tX76zGM69 nUBHRnZcx2LXloMGX6faz4N8I2lVTOY8zNSYc482kz8MAHCvxSowhHck9FJv3L70osfx 1qqcZAMm4mlAPe0fN/KPcIS56OFEEGvjFrZms+zkhtDQ2doiIKSOpkZ9MN+2UKr1/cd0 8EPHP4APWwo16Tyiv4/0IdLruI3RD9+fUMhS6KOY6pPUSscsrS Original-Sender: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org X-Gm-Message-State: AOJu0YxZlGpb8CVezCTSb3tUBApg6pyMVXCUy9CNJc4GVjjYP55i0jCv W5RqAqTjkbySI4DUGNZnnRc= X-Google-Smtp-Source: AGHT+IECXAD8wBXBKQZGt2DhgsUXpPRj9Nqa/yUSzOM36Na9I88iIMB/3guEWs8sR41xsiLwZ2ff9g== X-Received: by 2002:a05:6870:8e06:b0:1fa:1d14:2a43 with SMTP id lw6-20020a0568708e0600b001fa1d142a43mr6935529oab.24.1701792563757; Tue, 05 Dec 2023 08:09:23 -0800 (PST) X-BeenThere: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Original-Received: by 2002:a05:6870:218d:b0:1fa:de4d:c421 with SMTP id l13-20020a056870218d00b001fade4dc421ls1294364oae.2.-pod-prod-08-us; Tue, 05 Dec 2023 08:09:20 -0800 (PST) X-Received: by 2002:a05:6870:b51f:b0:1fb:23c8:7bfa with SMTP id v31-20020a056870b51f00b001fb23c87bfamr4225332oap.9.1701792559893; Tue, 05 Dec 2023 08:09:19 -0800 (PST) In-Reply-To: <51ca8210-3d60-4d5d-9af2-04c85995deb6n-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> X-Original-Sender: julien.dutant-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org 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:33454 Archived-At: ------=_Part_132577_616975624.1701792559348 Content-Type: multipart/alternative; boundary="----=_Part_132578_967432792.1701792559349" ------=_Part_132578_967432792.1701792559349 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable =20 Lua filters only change Pandoc's AST representation of your document, i.e.= =20 before it is then converted to LaTeX. A Raw block filter will not act on=20 Pandoc's LaTeX output, but only on Raw LaTeX blocks that are in the=20 markdown itself. A Pandoc solution would be to write a custom Lua *writer*=20 . The writer would use pandoc.write= =20 to generate Pandoc's own LaTeX output (body only) and modify it with=20 regular expressions or Lua patterns. To replace just a command name this is= =20 fairly easy, though longer than the third solution below. A LaTeX solution is to redefine \caption as \sidecaption: \renewcommand{\caption}{\sidecaption}=20 You can keep this enclosed in groups ({...}) to ensure that the=20 redefinition only applies locally. A hybrid Pandoc/LaTeX solution is a Lua filter that insert LaTeX code to=20 redefine \caption around figures: ``` lua if FORMAT:match 'latex' then function Figure (elem) return {=20 pandoc.RawBlock('latex','{\\renewcommand{\\caption}{\\subcaption}' ),=20 elem,=20 pandoc.RawBlock('latex','}')=20 }=20 end=20 end ``` This replaces any 'Figure' block element by a list (succession) of three=20 raw LaTeX blocks. The output should look like: {\renewcommand{\caption}{\subcaption}=20 ... Pandoc's LaTeX for the figure ...=20 }=20 Reposted from=20 https://stackoverflow.com/questions/77504584/pandoc-md-latex-write-lua-filt= er-to-change-latex-macro-used-for-caption/77607636#77607636 On Monday, November 20, 2023 at 7:06:57=E2=80=AFAM UTC+11 lukeflo wrote: > Hi everybody, > > I have written a custom latex `.cls' file to establish a typesetting > workflow for 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-c= aption'] > =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 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:=20 > https://stackoverflow.com/q/77504584/19647155 > > [pandoc repo] > > > [approach of pre pandoc version 3.0 posted] > > > [/complex figures/] > > [here] > --=20 You received this message because you are subscribed to the Google 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/f3fa2d12-6277-47c6-a3fc-b5fea1485600n%40googlegroups.com. ------=_Part_132578_967432792.1701792559349 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

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

A Pandoc solution would be to write a custom Lua writer= . The writer would use pandoc.write to generate Pandoc's own LaTeX output (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 f<= span>unction Figure (elem) return {=C2=A0
=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 pandoc.RawBlock('latex','{\\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 }
=C2=A0=C2=A0 e= nd
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
}

Reposted from https://stackoverflow.com/que= stions/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 wrote:
Hi everybody,

I have written a custom latex `.cls&= #39; file to establish a typesetting
workflow for the scientific journal= s of my research institute. The texts
should be written in Markdown and = then be processed with `pandoc' to
LaTeX.

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

But for the figures I need the caption from the Markd= own file to be set
with `\sidecaption' instead of `\caption' in = LaTeX, as well as with an
optional argument (short-caption) for the imag= e attribution in the list
of figures.

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

=E2=94=8C=E2=94=80=E2=94=80=E2=94=80=E2=94=80
=E2=94=82 PANDOC= _VERSION:must_be_at_least '3.1'
=E2=94=82
=E2=94=82 if FORMAT= :match 'latex' then
=E2=94=82 =C2=A0 function Figure(f)
=E2= =94=82 =C2=A0 =C2=A0 local short =3D f.content[1].content[1].attributes[= 9;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.In= lines(short)
=E2=94=82 =C2=A0 =C2=A0 end
=E2=94=82 =C2=A0 =C2=A0 retu= rn f
=E2=94=82 =C2=A0 end
=E2=94=82 end
=E2=94=94=E2=94=80=E2=94= =80=E2=94=80=E2=94=80

That works without any flaws.

But now I= need to figure out how to change the LaTeX macro used for the
caption. = The older [approach of pre pandoc version 3.0 posted] by tarleb
is reall= y intuitive and I could have easily adapted it to my needs. But
since pa= ndoc 3.0 there is the new [/complex figures/] approach and, so
far, I co= uldn't figure out how to change the LaTeX macro used for the
caption= s with this new behaviour.

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

=E2=94=8C=E2=94=80=E2=94=80=E2=94=80=E2=94=80
=E2=94=82 = if FORMAT:match 'latex' then
=E2=94=82 =C2=A0 function RawBlock = (raw)
=E2=94=82 =C2=A0 =C2=A0 local caption =3D raw.text:match('\\ca= ption')
=E2=94=82 =C2=A0 =C2=A0 if caption then
=E2=94=82 =C2=A0 = =C2=A0 =C2=A0 =C2=A0raw:gsub('\\caption', '\\sidecaption')<= br>=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. I
just never had to use it for= my daily tasks. I thought about using `awk'
or `sed' to edit th= e `.tex' file itself using a regex-substitution, but
that should rem= ain 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 underst= anding
of lua and the /complex figures/ approach for similar future task= s.

I appreciate any tipp!

Best,
Lukeflo

This questi= on is also posted on StackOverFlow: https://stackoverflow.com/q/77504584/19647155<= /a>

[pandoc repo]
<
https://g= ithub.com/jgm/pandoc/issues/7915#issuecomment-1427113349>

[ap= proach of pre pandoc version 3.0 posted]
<https://github.com/jgm/pandoc/issues/7915#issuecomment-1039370851>

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

[here] <https://stackoverflow.com/a/71296595/19647155&g= t;

--
You received this message because you are subscribed to the Google Groups &= quot;pandoc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an e= mail to pand= oc-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org.
To view this discussion on the web visit https://groups.google.com/d= /msgid/pandoc-discuss/f3fa2d12-6277-47c6-a3fc-b5fea1485600n%40googlegroups.= com.
------=_Part_132578_967432792.1701792559349-- ------=_Part_132577_616975624.1701792559348--