From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.io/gmane.text.pandoc/33483 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: "'lukeflo' via pandoc-discuss" Newsgroups: gmane.text.pandoc Subject: Re: Lua filter to change macro for figure caption md -> latex Date: Thu, 7 Dec 2023 23:35:48 -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_22146_2089535206.1702020948346" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="16107"; mail-complaints-to="usenet@ciao.gmane.io" To: pandoc-discuss Original-X-From: pandoc-discuss+bncBD7NXUVXXUBBBVMOZOVQMGQELXNCI6I-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Fri Dec 08 08:35:53 2023 Return-path: Envelope-to: gtp-pandoc-discuss@m.gmane-mx.org Original-Received: from mail-oa1-f61.google.com ([209.85.160.61]) by ciao.gmane.io with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1rBVOr-00040i-7k for gtp-pandoc-discuss@m.gmane-mx.org; Fri, 08 Dec 2023 08:35:53 +0100 Original-Received: by mail-oa1-f61.google.com with SMTP id 586e51a60fabf-1fadd34d7e5sf3170063fac.3 for ; Thu, 07 Dec 2023 23:35:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=20230601; t=1702020952; x=1702625752; 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=XJUDyDh/RiCc5oUzIkAZ4H72+2b3FkZslyeo3/UMbLk=; b=Z/6eM5L2X1kwKe62Tah/QGez0Vb0DBcHdEWaib4527LDc1YSy96Bk3kaou7zHQ07Yb YEkBp/An/I8SKqmH92kv0ooNo+cj5AGP+SSkx7kbtojmYYPJOcSy3xm1BFmW6/JWcLGG bVC727DHDfQaRR8lLPDofPelldI+S6g87pQRPYv0bM0FPD95pSKx7vPlqk8Jc90i3iCs dA7tGucViVpxLQ1XdSBcgszk3o3CNTFaIQa8wDgC6YGcR3PK+ZaECD6lNbeUov4QqlM0 lFOpsGqyCOQz+KgKPvrXJKkPgnXjV2t9J2OgCfCM6Nhouw/WklmYyJpjMTZCASApUtOR otug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702020952; x=1702625752; h=list-unsubscribe:list-subscribe:list-archive:list-help:list-post :x-spam-checked-in-group:list-id:mailing-list:precedence:reply-to :x-original-sender:mime-version:subject:references:in-reply-to :message-id:to:from:date:x-beenthere:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=XJUDyDh/RiCc5oUzIkAZ4H72+2b3FkZslyeo3/UMbLk=; b=Xl3x71uTo5X2ukQPKiQ4/Q1lgq5+TJRODtws2V8D/FBESBV32QVUeF3rnDqs7BPF6O bg/GQ2banmtfd165rO/HMxA2jg836NZioCYglcKGoi2sTsh8LP9EZf0cD7ov1ilIWvE5 FylTHl+ZowcGTJTgKd1MqTXZUIaI9rGXy9nPj7kqZ0ppAjB0WOxa4gAQ5Mgmyqo8+Ncb BSBPChbt3vCdijiFSxBmocUd59vmgH+1QgzIDeiak9+SMFjW6fNUpsz4EatM0Ca9VUla Q4Lh6yzGNBc84r5dvvIRtr+7DcWobPb7g+Yv0j23OSSEr1utkzLCw/+0o X-Gm-Message-State: AOJu0Yy5oybted4Kf5C1Y/HTxxR0MSY3F8aWsp9XHHxWRkWchPbuznvI uFF8QKGuvSAAr7ZnHQjqV2M= X-Google-Smtp-Source: AGHT+IH6MiNZvfUw7wszcEzga6oNfcGSxKtHba2OMMVhqT61ynIpJMk35p5Ix543ymkynMOXVSBBMg== X-Received: by 2002:a05:6870:2253:b0:1fb:75a:de73 with SMTP id j19-20020a056870225300b001fb075ade73mr3804595oaf.97.1702020951908; Thu, 07 Dec 2023 23:35:51 -0800 (PST) X-BeenThere: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Original-Received: by 2002:a05:6870:9a21:b0:1fa:1717:fc5a with SMTP id fo33-20020a0568709a2100b001fa1717fc5als2744509oab.1.-pod-prod-08-us; Thu, 07 Dec 2023 23:35:49 -0800 (PST) X-Received: by 2002:a05:6870:2213:b0:1fa:e787:d416 with SMTP id i19-20020a056870221300b001fae787d416mr4559306oaf.3.1702020949005; Thu, 07 Dec 2023 23:35:49 -0800 (PST) In-Reply-To: X-Original-Sender: k.flo.west-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org X-Original-From: lukeflo Precedence: list Mailing-list: list pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org; contact pandoc-discuss+owners-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org List-ID: X-Google-Group-Id: 1007024079513 List-Post: , List-Help: , List-Archive: , List-Unsubscribe: , Xref: news.gmane.io gmane.text.pandoc:33483 Archived-At: ------=_Part_22146_2089535206.1702020948346 Content-Type: multipart/alternative; boundary="----=_Part_22147_509711777.1702020948346" ------=_Part_22147_509711777.1702020948346 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hello Julien, thanks for the reply. Unfortunatley, as mentioned in the stackoverflow=20 post, your suggested LaTeX code won't work. The \caption macro is very complex in the backend and cannot be copied on= =20 the fly via \let, \NewCommandCopy or something similar. Even after doing so= =20 with e.g. \NewCommandCopy{\oldcaption}{\caption} and then setting \RenewDoc= umentCommand{\caption}{o=20 m}{\sidecaption[#1]{#2}} nothing changes and the definition of \caption,=20 checked with \meaning or something similar, stays the same as before (even= =20 \DeclareDocumentCommand doesn't work). In the end, it might be possible to somehow change the \caption macro=20 itself. But the effort might not be worth the result (and its more of a=20 question for TeX.SE). Using a custom writer for building Latex figures and replace the \caption= =20 string inside would be a great solution. I read through the writer manual,= =20 but didn't really understand how the AST works and which values have to be= =20 used in such a writer. Furthermore, I'm using a a custom Latex template for= =20 exporting (based on the default.template.latex) which has to be integrated= =20 with such a writer. Therefore, I really woud appreciate a Lua framework to understand which=20 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= .=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=20 > pandoc.write to generate Pandoc's own LaTeX output (body only) and modify= =20 > it with regular expressions or Lua patterns. To replace just a command na= me=20 > this is fairly easy, though longer than the third solution below. > > A LaTeX solution is to redefine \caption as \sidecaption: > \renewcommand{\caption}{\sidecaption}=20 > > You can keep this enclosed in groups ({...}) to ensure that the=20 > redefinition only applies locally. > > A hybrid Pandoc/LaTeX solution is a Lua filter that insert LaTeX 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-fi= lter-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-= caption'] >> =E2=94=82 if short and not f.caption.short then >> =E2=94=82 f.caption.short =3D pandoc.Inlines(short) >> =E2=94=82 end >> =E2=94=82 return f >> =E2=94=82 end >> =E2=94=82 end >> =E2=94=94=E2=94=80=E2=94=80=E2=94=80=E2=94=80 >> >> That works without any flaws. >> >> But now I need to figure out how to change the LaTeX macro used for the >> caption. The older [approach of pre pandoc version 3.0 posted] by tarleb >> is really intuitive and I could have easily adapted it to my needs. But >> since pandoc 3.0 there is the new [/complex figures/] approach and, so >> far, I couldn't figure out how to change the LaTeX macro used for 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/b565fdd5-8216-4596-a2ed-c75019aad172n%40googlegroups.com. ------=_Part_22147_509711777.1702020948346 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hello Julien,

thanks for the reply. Unfortu= natley, as mentioned in the stackoverflow post, your suggested LaTeX code w= on't work.

The \caption macro is ve= ry complex in the backend and cannot be copied on the fly via \let, \NewCommandCopy or something similar. Even after doing s= o with e.g. \NewCommandCopy{\oldcaption}{\caption} and then se= tting \RenewDocumentCommand{\caption}{o m}{\sidecaption[#1]{#2}} nothing changes and the definition of \caption, checked wit= h \meaning or something similar, stays the same as before (eve= n \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 b= e used in such a writer. Furthermore, I'm using a a custom Latex template f= or exporting (based on the default.template.latex) which has to be integrat= ed with such a writer.

Therefore, I really woud appreciate a Lua fram= ework to understand which functions have to be edited etc. to accomplish th= e 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=20 act on Pandoc's LaTeX output, but only on Raw LaTeX blocks that are in= =20 the markdown itself.

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

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

\renewcommand{\caption}{\sidecaption}

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

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

``` lua

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

```

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

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


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

I have written a custo= m latex `.cls' file to establish a typesetting
workflow for the scie= ntific journals of my research institute. The texts
should be written in= Markdown and then be processed with `pandoc' to
LaTeX.

I alr= eady have an elaborated pandoc template to produce the LaTeX
preambel et= c. So far its working great.

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

To get the la= tter working I use the following template from a GitHub
discussion in th= e [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 Fi= gure(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 macro used for t= he
caption. The older [approach of pre pandoc version 3.0 posted] by tar= leb
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 tha= t (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 f= unction RawBlock (raw)
=E2=94=82 =C2=A0 =C2=A0 local caption =3D raw.tex= t:match('\\caption')
=E2=94=82 =C2=A0 =C2=A0 if caption then
= =E2=94=82 =C2=A0 =C2=A0 =C2=A0 =C2=A0raw:gsub('\\caption', '\\s= idecaption')
=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 ch= allenge 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 `s= ed' to edit the `.tex' file itself using a regex-substitution, but<= br>that should remain an absolute stopgap, since it makes the whole
work= flow less portable.

Thus, I'm hoping for a hint/a solution in fo= rm 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 s= imilar future tasks.

I appreciate any tipp!

Best,
Lukeflo<= br>
This question is also posted on StackOverFlow: https://stackoverflow.com/q/7= 7504584/19647155

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

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

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

[here] <https://stackoverflow.com/a/712965= 95/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 pand= oc-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org.
To view this discussion on the web visit https://groups.google.com/d= /msgid/pandoc-discuss/b565fdd5-8216-4596-a2ed-c75019aad172n%40googlegroups.= com.
------=_Part_22147_509711777.1702020948346-- ------=_Part_22146_2089535206.1702020948346--