A document has a mix of SVG and PNG images. Changing the parameters for `\setupexternalfigure` affects both SVG and PNG images. Consider the following code: \setupexternalfigures[ order={svg,pdf,png,jpg}, location={local,default,global}, directory={images}, maxwidth=\textwidth, maxheight=0.4\textheight ] I'd like to change the `maxheight` option for only PNG images, such as: \setupexternalfigure[ method=png, maxwidth=\textwidth, maxheight=\textheight, ] However, this affects SVG images as well. How do you apply different external figure setups based on image type? Thank you!
Am 08.07.22 um 17:46 schrieb Thangalin via ntg-context:
I'd like to change the `maxheight` option for only PNG images, such as:
Define your own figure class with \definexternalfigure: https://wiki.contextgarden.net/Command/defineexternalfigure It won’t automatically chose the file format (or maybe it works with "order"), but you don’t need to repeat the setting for each instance. Hraban
Thank you for the suggestion Hraban. The source comes from Markdown, which is converted to XML, then typeset using ConTeXt. There's no Markdown-specific mechanism to relate images to a particular external figure definition, unfortunately. All images use the same syntax and are treated the same way. The only part that differs is the file name extension (and header within the file). One possibility would be something like: \setupexternalfigure[ width=1em, height=1em, order={svg,pdf,png,jpg}, location={local,default,global}, ] \defineexternalfigure[png][ width=\textwidth, height=\textheight, ] \unprotect \let\old_externalfigure=\externalfigure \tolerant\def\externalfigure[#1]#,[#2]#,[#3]{% \doifelseinstring{.png}{#1}{% \old_externalfigure[#1][png][#2]% }{% \doifelsefileexists{#1.png}{% \old_externalfigure[#1.png][png][#2]% }{% \old_externalfigure[#1][#2][#3]% }% }% }\protect See: https://tex.stackexchange.com/a/650221/2148 I was wondering if there was a simpler or a solution that's more the ConTeXt-way? Thanks again!
On 2022-07-12 14:11, Thangalin via ntg-context wrote:
Thank you for the suggestion Hraban.
The source comes from Markdown, which is converted to XML, then typeset using ConTeXt. There's no Markdown-specific mechanism to relate images to a particular external figure definition, unfortunately. All images use the same syntax and are treated the same way. The only part that differs is the file name extension (and header within the file).
One possibility would be something like: |\setupexternalfigure[ width=1em, height=1em, order={svg,pdf,png,jpg}, location={local,default,global}, ] \defineexternalfigure[png][ width=\textwidth, height=\textheight, ] \unprotect \let\old_externalfigure=\externalfigure \tolerant\def\externalfigure[#1]#,[#2]#,[#3]{% \doifelseinstring{.png}{#1}{% \old_externalfigure[#1][png][#2]% }{% \doifelsefileexists{#1.png}{% \old_externalfigure[#1.png][png][#2]% }{% \old_externalfigure[#1][#2][#3]% }% }% } \protect | See: https://tex.stackexchange.com/a/650221/2148
I was wondering if there was a simpler or a solution that's more the ConTeXt-way?
Thanks again!
Assuming that Pandoc markdown is being used, you should be able to set class attributes on each image. You can then associate external figure definitions with specific classes, and process each individually. (Pandoc user guide, link_attributes https://pandoc.org/MANUAL%202.html#extension-link_attributes) -- Rik
Thanks, Rik. Not using Pandoc. I was using Pandoc up until 2020: https://dave.autonoma.ca/blog/2019/05/22/typesetting-markdown-part-1/ I've since authored KeenWrite https://github.com/DaveJarvis/keenwrite, which uses flexmark-java https://github.com/vsch/flexmark-java to generate XHTML documents from Markdown. The only additions I've made to the library is to add support for Pandoc's annotation syntax (:::). That said, I'm also a firm believer in not duplicating information. The information (file name extension) is already available in the documents, so adding a classification on top of the extension feels redundant. Thanks for the suggestion! On Tue, Jul 12, 2022 at 12:18 PM Rik Kabel via ntg-context < ntg-context@ntg.nl> wrote:
On 2022-07-12 14:11, Thangalin via ntg-context wrote:
Thank you for the suggestion Hraban.
The source comes from Markdown, which is converted to XML, then typeset using ConTeXt. There's no Markdown-specific mechanism to relate images to a particular external figure definition, unfortunately. All images use the same syntax and are treated the same way. The only part that differs is the file name extension (and header within the file).
One possibility would be something like:
\setupexternalfigure[ width=1em, height=1em, order={svg,pdf,png,jpg}, location={local,default,global}, ] \defineexternalfigure[png][ width=\textwidth, height=\textheight, ] \unprotect \let\old_externalfigure=\externalfigure \tolerant\def\externalfigure[#1]#,[#2]#,[#3]{% \doifelseinstring{.png}{#1}{% \old_externalfigure[#1][png][#2]% }{% \doifelsefileexists{#1.png}{% \old_externalfigure[#1.png][png][#2]% }{% \old_externalfigure[#1][#2][#3]% }% }% }\protect
See: https://tex.stackexchange.com/a/650221/2148
I was wondering if there was a simpler or a solution that's more the ConTeXt-way?
Thanks again!
Assuming that Pandoc markdown is being used, you should be able to set class attributes on each image. You can then associate external figure definitions with specific classes, and process each individually. (Pandoc user guide, link_attributes https://pandoc.org/MANUAL%202.html#extension-link_attributes)
-- Rik
___________________________________________________________________________________ If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / https://www.ntg.nl/mailman/listinfo/ntg-context webpage : https://www.pragma-ade.nl / http://context.aanhet.net archive : https://bitbucket.org/phg/context-mirror/commits/ wiki : https://contextgarden.net
___________________________________________________________________________________
Thangalin via ntg-context schrieb am 12.07.2022 um 20:11:
Thank you for the suggestion Hraban.
The source comes from Markdown, which is converted to XML, then typeset using ConTeXt. There's no Markdown-specific mechanism to relate images to a particular external figure definition, unfortunately. All images use the same syntax and are treated the same way. The only part that differs is the file name extension (and header within the file).
One possibility would be something like: |\setupexternalfigure[ width=1em, height=1em, order={svg,pdf,png,jpg}, location={local,default,global}, ] \defineexternalfigure[png][ width=\textwidth, height=\textheight, ] \unprotect \let\old_externalfigure=\externalfigure \tolerant\def\externalfigure[#1]#,[#2]#,[#3]{% \doifelseinstring{.png}{#1}{% \old_externalfigure[#1][png][#2]% }{% \doifelsefileexists{#1.png}{% \old_externalfigure[#1.png][png][#2]% }{% \old_externalfigure[#1][#2][#3]% }% }% } \protect | See: https://tex.stackexchange.com/a/650221/2148
I was wondering if there was a simpler or a solution that's more the ConTeXt-way?
When you use XML as input you can add additional code to the setups block which uses \externalfigure to load the image. \setupexternalfigures [location={local,global,default}] \defineexternalfigure [jpg] [width=4cm] \defineexternalfigure [png] [width=8cm] \starttexdefinition includegraphics #1 \splitfilename{#1} \processaction [\splitofftype] [ jpg=>{\externalfigure[#1][jpg]}, png=>{\externalfigure[#1][png]}, default=>{\externalfigure[#1]}, unknown=>{\externalfigure[#1]}] \stoptexdefinition %\starttexdefinition includegraphics #1 % % \getfiguredimensions[#1] % % \processaction % [\figurefiletype] % [ jpg=>{\externalfigure[#1][jpg]}, % png=>{\externalfigure[#1][png]}, % default=>{\externalfigure[#1]}, % unknown=>{\externalfigure[#1]}] % %\stoptexdefinition \starttext \includegraphics{hacker.jpg} \includegraphics{mill.png} \includegraphics{cow.pdf} \stoptext Wolfgang
Thank you, Wolfgang. That looks promising. SVG files are converted using MetaPost. I thought the following would work: svg=>{\externalfigure[#1][svg][conversion=mp]} Is there anything else that needs to happen to ensure SVG files are piped through MetaPost? Thanks again!
\setupexternalfigures [location={local,global,default}]
\defineexternalfigure [jpg] [width=4cm] \defineexternalfigure [png] [width=8cm]
\starttexdefinition includegraphics #1
\splitfilename{#1}
\processaction [\splitofftype] [ jpg=>{\externalfigure[#1][jpg]}, png=>{\externalfigure[#1][png]}, default=>{\externalfigure[#1]}, unknown=>{\externalfigure[#1]}]
\stoptexdefinition
\starttext
\includegraphics{hacker.jpg}
\includegraphics{mill.png}
\includegraphics{cow.pdf}
\stoptext
Thangalin via ntg-context schrieb am 18.07.2022 um 19:10:
Thank you, Wolfgang.
That looks promising.
SVG files are converted using MetaPost. I thought the following would work:
svg=>{\externalfigure[#1][svg][conversion=mp]}
You can probably remove the second argument ([svg]) which was used in my example to pass the default values with \defineexternalfigure. Just using \externalfigure[#1][conversion=mp] where you pass the default values with the second argument is enough. Wolfgang
Got it working. Complete solution: \setupexternalfigures[ location={local,global,default}, width=\textwidth ]\defineexternalfigure[svg][width=1cm]\defineexternalfigure[jpg][width=2cm]\defineexternalfigure[png][width=4cm] % Won't be applied because there's no process action.% Default (\textwidth) is used, as defined above.\defineexternalfigure[pdf][width=6cm] \starttexdefinition includegraphics #1 \splitfilename{#1} \processaction[\splitofftype][ jpg=>{\externalfigure[#1][jpg]}, png=>{\externalfigure[#1][png]}, svg=>{\externalfigure[#1][svg][conversion=mp]}, default=>{\externalfigure[#1]}, unknown=>{\externalfigure[#1]} ]\stoptexdefinition \starttext \includegraphics{kitten.jpg} \includegraphics{mill.png} \includegraphics{cow.pdf} \includegraphics{tiger.svg}\stoptext Image files attached. Thanks again!
On 8/1/2022 2:29 AM, Thangalin via ntg-context wrote:
Got it working. Complete solution:
|\setupexternalfigures[ location={local,global,default}, width=\textwidth ] \defineexternalfigure[svg][width=1cm] \defineexternalfigure[jpg][width=2cm] \defineexternalfigure[png][width=4cm] % Won't be applied because there's no process action. % Default (\textwidth) is used, as defined above. \defineexternalfigure[pdf][width=6cm] \starttexdefinition includegraphics #1 \splitfilename{#1} \processaction[\splitofftype][ jpg=>{\externalfigure[#1][jpg]}, png=>{\externalfigure[#1][png]}, svg=>{\externalfigure[#1][svg][conversion=mp]}, default=>{\externalfigure[#1]}, unknown=>{\externalfigure[#1]} ] \stoptexdefinition \starttext \includegraphics{kitten.jpg} \includegraphics{mill.png} \includegraphics{cow.pdf} \includegraphics{tiger.svg} \stoptext | I've added:
\setfigureconversion[svg][mp] So that this works: \starttext \externalfigure[kitten.jpg][width=2cm] \externalfigure[mill.png] [width=4cm] \externalfigure[cow.pdf] [width=6cm] \externalfigure[tiger.svg] [width=1cm] \stoptext But after we release you need to wikify it. Hans ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl -----------------------------------------------------------------
Thangalin via ntg-context
The source comes from Markdown, which is converted to XML, then typeset using ConTeXt. There's no Markdown-specific mechanism to relate images to a particular external figure definition, unfortunately. All images use the same syntax and are treated the same way. The only part that differs is the file name extension (and header within the file).
The below is likely overkill and a good bit of work to setup, but it is also very flexible. Maybe it suits your needs. The Markdown-to-XML conversion can be tuned to include all the relevant information. E.g., you could use pandoc with a custom writer to produce the XML format of your choice. The link below is an example XML writer that can be tuned as needed. https://gist.github.com/tarleb/634b409be0af62ca210cc9e96d41ca8c With that setup, one can modify the `Image` function to produce the output you need. E.g., match on the file name extension use the library function `pandoc.path.split_extension` and then change the element type depending on the extension. This way you'd have all the important information in your XML. More info: https://pandoc.org/custom-writers and https://pandoc.org/lua-filters. Of course, you could also use pandoc to go directly to ConTeXt; that is currently my preferred way of producing PDF from Markdown. Cheers, Albert -- Albert Krewinkel GPG: 8eed e3e2 e8c5 6f18 81fe e836 388d c0b2 1f63 1124
Of course, you could also use pandoc to go directly to ConTeXt; that is
currently my preferred way of producing PDF from Markdown.
Thanks Albert! I'm going to stick with my desktop text editor, KeenWrite https://github.com/DaveJarvis/keenwrite, rather than use shell scripts https://dave.autonoma.ca/blog/2019/05/22/typesetting-markdown-part-1/ and pandoc. Cheers!
participants (6)
-
Albert Krewinkel
-
Hans Hagen
-
Henning Hraban Ramm
-
Rik Kabel
-
Thangalin
-
Wolfgang Schuster