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 values 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 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 modify > 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 AM 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]: >> >> ┌──── >> │ PANDOC_VERSION:must_be_at_least '3.1' >> │ >> │ if FORMAT:match 'latex' then >> │ function Figure(f) >> │ local short = f.content[1].content[1].attributes['short-caption'] >> │ if short and not f.caption.short then >> │ f.caption.short = pandoc.Inlines(short) >> │ end >> │ return f >> │ end >> │ end >> └──── >> >> 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]: >> >> ┌──── >> │ if FORMAT:match 'latex' then >> │ function RawBlock (raw) >> │ local caption = raw.text:match('\\caption') >> │ if caption then >> │ raw:gsub('\\caption', '\\sidecaption') >> │ end >> │ return raw >> │ end >> │ end >> └──── >> >> 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-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.