Tikz diagrams that load external images

91 views
Skip to first unread message

Sean Fitzpatrick

unread,
Jul 3, 2020, 3:38:55 PM7/3/20
to PreTeXt support
Asking on behalf of Veselin's group at SFU:

They have a few TikZ diagrams where there is an external image.
Example: the classic "man walks away from lamp" related rates problem.

Everything is done in TikZ, except for the image of the man. That is loaded as an external PNG with a line like
\node[inner sep=0pt]  at (3,1.55)  {\includegraphics[width=.03\textwidth]{Man.png}};

When pretext.py processes <latex-image> there's nothing (as far as I know) that does something like the following:
1. check if there is an images/ folder in source
2. make tmp directory for image conversion
3. copy images/ folder into said tmp directory

Implementing could be tricky, since I think it would require authors to be uniform in how they name their external image folder, and where they place it.

Does anyone have tikz images like this? And do you have a workaround?

PS: the surprising part, for me: the missing image means that running xelatex on the .tex file produced by extract-latex-image.xsl exits with errors. But it *does* produce output (the diagram, minus the man!) so the script is able to continue.

PPS: they are using the new pretext-cli.

Rob Beezer

unread,
Jul 3, 2020, 4:01:23 PM7/3/20
to pretext...@googlegroups.com
See the discussion preceding Figure 9.7 at

https://pretextbook.org/examples/sample-article/html/graphics.html

That device is meant for *data*, but perhaps putting the man in the right place
and using the right path on the \includegraphics and using the right path on the
pretext/pretext command line will all work.

Please run interference and test this yourself before sending to SFU? Then we
can see what the pretext-cli group thinks.
> --
> You received this message because you are subscribed to the Google Groups
> "PreTeXt support" group.
> To unsubscribe from this group and stop receiving emails from it, send an email
> to pretext-suppo...@googlegroups.com
> <mailto:pretext-suppo...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/pretext-support/cbb5648f-37e4-4a1c-9624-3ef1fecf532bo%40googlegroups.com
> <https://groups.google.com/d/msgid/pretext-support/cbb5648f-37e4-4a1c-9624-3ef1fecf532bo%40googlegroups.com?utm_medium=email&utm_source=footer>.

Steven Clontz

unread,
Jul 3, 2020, 4:15:01 PM7/3/20
to PreTeXt support
>  I think it would require authors to be uniform in how they name their external image folder, and where they place it. 

I'm okay with only supporting images stored in an `images` subdirectory of the source directory in PreTeXt-CLI. At least, I have yet to hear a compelling use-case for supporting anything more general.

Alex Jordan

unread,
Jul 3, 2020, 4:24:29 PM7/3/20
to pretext...@googlegroups.com
In one of the recent zoom meetings, we discussed this somewhat. I can think of:
  1. externally produced images
  2. latex-image images
  3. asymptote images
  4. webwork images
  5. thumbnail images (scraped from youtube, for example)
  6. captured images from a standalone page, like an image of a GGB applet
    captured from a headless browser rendering
Only item 1 should be in your source. Something from the other items might be
stable enough that you put it in source too. Some standard folder structure that
recognizes the different origins of the images is something that feels right to me.

To unsubscribe from this group and stop receiving emails from it, send an email to pretext-suppo...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pretext-support/7fe24463-9f8e-452f-a42c-4f3a74e18838n%40googlegroups.com.

David Farmer

unread,
Jul 3, 2020, 4:31:16 PM7/3/20
to PreTeXt support

I agree, and I generally like elevating best practices
to be requirements, when that helps the developers.
By "requirement" I mean "you have to know a lot to hack
together a different solution, and you accept the consequences."

In practice, such things have the positive effect of saving
novices from their misconceptions.

Keeping the images in a "images" directory, which is parallel
to the directory containing the PTX source, is a good example.

When I convert a LaTeX book to PreTeXt, you get one file per
chapter, and one file per section. That typically does not
reflect the structure of the original LaTeX source. (I guess
that structure is not actually exploited by the code, so that
good practice is unlikely to become a requirement.)
> pretext-suppo...@googlegroups.com.
> To view this discussion on the web visithttps://groups.google.com/d/msgid/pretext-support/7fe24463-9f8e-452f-a42c-4f3a74e18838n%40go
> oglegroups.com.
>
>

Steven Clontz

unread,
Jul 3, 2020, 4:35:09 PM7/3/20
to PreTeXt support
I recently did a dumb and accidentally stuck `image-1.svg` generated from a `latex-image` into `source/images`. I believe (Oscar wrote this part) that PreTeXt-CLI first generates diagrams in `output/images`, then copies over `source/images` into `output/images`. You can guess how that went.  https://github.com/PreTeXtBook/pretext-cli/issues/44

This makes me wonder if generated images could be instead be built into a folder like `output/generated-latex-image/1.svg` that's more explicit in its source?

Sean Fitzpatrick

unread,
Jul 3, 2020, 4:56:46 PM7/3/20
to pretext...@googlegroups.com
The directory structure they have, after some (ok, a lot of) initial cleanup from me, and David's conversion,
has parallel folders
images/
ptx/

The TikZ source is within the .ptx files and is including the external image with
\includegraphics[presumably some options]{images/foo.png}

I think that aligns with the advice in the Sample Article, but of course the --include option is missing.

Trying now with a -i pointing to the images folder. And the results are amusing:

The PDF actually works out as expected -- the man is there. But during conversion to SVG, only his head survives!
image-1.svg

Steven Clontz

unread,
Jul 3, 2020, 5:36:16 PM7/3/20
to PreTeXt support
Is the source on GitHub?

Sean Fitzpatrick

unread,
Jul 3, 2020, 6:02:37 PM7/3/20
to pretext...@googlegroups.com
It is, but it's a private repo.
The document is a curated collection of past exam questions. I think they provide it to students but they don't include the answers.

Sean Fitzpatrick

unread,
Jul 4, 2020, 2:16:28 PM7/4/20
to pretext...@googlegroups.com
I should add: if you want access to that repository I think Veselin would be willing to add you, especially if you tell him you're working on getting their images to work with pretext-cli.

Steven Clontz

unread,
Jul 4, 2020, 2:22:13 PM7/4/20
to PreTeXt support
Yeah tell them to add GitHub user StevenClontz as a collaborator and I can take a look.

Rob Beezer

unread,
Jul 4, 2020, 7:58:10 PM7/4/20
to pretext...@googlegroups.com
On 7/3/20 1:56 PM, Sean Fitzpatrick wrote:
> The PDF actually works out as expected -- the man is there. But during
> conversion to SVG, only his head survives!

When I look at the attached SVG, I see the whole man, beer-belly and all. Was
this one suppose to be busted?

"eog" as viewer on Ubuntu.

Rob

Sean Fitzpatrick

unread,
Jul 5, 2020, 2:52:18 PM7/5/20
to PreTeXt support
Huh. There he is. Not sure why he was missing his body on my other machine.

Stephen Brown

unread,
Jun 16, 2025, 3:39:38 PM6/16/25
to PreTeXt support
Hi all,

I'm reviving this old thread, as it seems I have the same issue to try to resolve. I have a TikZ diagram that loads two external image files. I'm wondering where I should put these images (shark.png and swimmer.png) in my directory structure, or if there is a workaround. Here is my figure:

<figure xml:id="fig_shark_attack">
  <caption>An illustration of the shark trying to catch you as you swim to shore.</caption>
  <image>
    <latex-image>
      \begin{tikzpicture}
        \fill[cyan!30] decorate[ragged border]{ (0,2) -- (8,2) } -- (8,0) -- (0,0) -- cycle;
        \fill[yellow!30] decorate[ragged border]{ (8,2) -- (8,0) } -- (9,0) -- (9,2) -- cycle;
        \draw (1.5,1) node {\includegraphics{images/shark.png}};
        \draw (5,1) node {\includegraphics[scale=0.075]{images/swimmer.png}};
        \draw[|-|] (1.5,3) node [above] {\(\SI{0}{m}\)} -- (5,3) node [above] {\(\SI{50}{m}\)};
        \draw[|-|] (5,3) -- (8,3) node [above] {\(\SI{70}{m}\)};
      \end{tikzpicture}
    </latex-image>
  </image>
</figure>

sharkattack.png

Thanks!
Stephen

Rob Beezer

unread,
Jun 17, 2025, 12:56:59 PM6/17/25
to pretext...@googlegroups.com
Dear Stephen,

I checked, and the directory of "external" images (etc) *is* being copied into
the temporary directory where LaTeX is run on these images. (Use the
pretext/pretext script with a "-vv" argument and these directories are not
cleaned up and their names are listed at the end of the run).

Guessing since I'd need to see more of your source and your publisher file, but
try these

external/shark.png

external/images/shark.png

Report back? Did one of these do the job?

David Austin and I have talked about doing this sort of thing more carefully,
mostly in the context of providing data files to graphics routines that build
charts, plots, etc, but there has been no work in that direction yet.

Rob
> sharkattack.png
>
> Thanks!
> Stephen
>
> On Sunday, 5 July 2020 at 11:52:18 UTC-7 dsfitz...@gmail.com wrote:
>
> Huh. There he is. Not sure why he was missing his body on my other machine.
>
>
> On Saturday, July 4, 2020 at 5:58:10 PM UTC-6, Rob Beezer wrote:
>
> On 7/3/20 1:56 PM, Sean Fitzpatrick wrote:
> > The PDF actually works out as expected -- the man is there. But during
> > conversion to SVG, only his head survives!
>
> When I look at the attached SVG, I see the whole man, beer-belly and
> all.  Was
> this one suppose to be busted?
>
> "eog" as viewer on Ubuntu.
>
> Rob
>
> --
> You received this message because you are subscribed to the Google Groups
> "PreTeXt support" group.
> To unsubscribe from this group and stop receiving emails from it, send an email
> to pretext-suppo...@googlegroups.com <mailto:pretext-
> support+u...@googlegroups.com>.
> To view this discussion visit https://groups.google.com/d/msgid/pretext-support/
> a53bc8f0-afcd-4886-a23c-ab462c707bc3n%40googlegroups.com <https://
> groups.google.com/d/msgid/pretext-support/a53bc8f0-afcd-4886-a23c-
> ab462c707bc3n%40googlegroups.com?utm_medium=email&utm_source=footer>.

Stephen Brown

unread,
Jun 17, 2025, 1:48:39 PM6/17/25
to PreTeXt support
Hi Rob,

Here is my source directory structure, with the file in question:

I've tried the following with no success:

output/web/external/shark.png
output/web/external/images/shark.png
output/external/shark.png
output/external/images/shark.png
external/shark.png
external/images/shark.png
source/external/shark.png
source/external/images/shark.png
source/activities_math122/external/shark.png  (location of the .ptx with this #latex-image)
source/activities_math122/ external/images/shark.png

I'm using VSCode on Windows and I'm not sure what you mean by the pretext/pretext script with a "-vv" argument. I've tried building via the terminal as well. 

Thanks for the help!

Stephen

David Austin

unread,
Jun 17, 2025, 2:31:29 PM6/17/25
to pretext...@googlegroups.com
I can confirm that building TikZ images in the sample-article with the pretext/pretext script copies the contents of the external directory (@external="media") into the "external" directory inside the tmp directory where the build happens.  It doesn't do this when building prefigure diagrams though. :-(

Stephen:  In your publication file (publication/publication.xml), there is a #source element that looks like this:

<source>
    <directories external="../assets" generated="../generated-assets" />
    <!-- Set which marked elements are included in this version: -->
    <!-- See
    https://pretextbook.org/doc/guide/html/publication-file-source.html#publication-file-source-version -->
    <!-- <version include="videos labs"/> -->
  </source>

Note the @external attribute on the #directories element.  The contents of this directory are copied into the build directory so you could include shark.png in that directory.  I don't see an assets directory in your repository, but if you include assets/images/shark.png and assets/images/swimmer.png, your TikZ code should work if you use \includegraphics{external/images/shark.png}.  assets is a sibling of source in the directory structure.

(I'm really hoping the swimmer escapes in your problem.)

To unsubscribe from this group and stop receiving emails from it, send an email to pretext-suppo...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/pretext-support/a486eb3d-2c66-45b0-8d8c-abfd94d960a8n%40googlegroups.com.

Stephen Brown

unread,
Jun 18, 2025, 5:50:50 PM6/18/25
to PreTeXt support
That's done the trick! Thank you David!

(Unfortunately, the swimmer does not escape. But by question 9, they realize they can surf a wave instead to make it to safety.)

Stephen

Rob Beezer

unread,
Jun 19, 2025, 4:50:16 PM6/19/25
to pretext...@googlegroups.com
Dear Stephen,

I guess I had not realized those images were not in the external directory.
Anyway, thanks to David A for getting this one going.

* It is a bit of an accident that this works at all, and could be improved.
(*Will* be improved.)

* For anyone reading this later, we may eventually have a *different* directory
to support this sort of thing, so check around for that.

* Stephen - would you be willing to allow us permission to add your example
into the sample article (with credit)? It'd give us something to work with once
we shore things up.

Rob


On 6/18/25 14:50, Stephen Brown wrote:
> That's done the trick! Thank you David!
>
> (Unfortunately, the swimmer does not escape. But by question 9, they realize
> they can surf a wave instead to make it to safety.)
>
> Stephen
>
> On Tuesday, 17 June 2025 at 11:31:29 UTC-7 david.a...@gmail.com wrote:
>
> I can confirm that building TikZ images in the sample-article with the
> pretext/pretext script copies the contents of the external directory
> (@external="media") into the "external" directory inside the tmp directory
> where the build happens.  It doesn't do this when building prefigure
> diagrams though. :-(
>
> Stephen:  In your publication file (publication/publication.xml), there is a
> #source element that looks like this:
>
> <source>
>     <directories external="../assets" generated="../generated-assets" />
>     <!-- Set which marked elements are included in this version: -->
>     <!-- See
> https://pretextbook.org/doc/guide/html/publication-file-
> source.html#publication-file-source-version <https://pretextbook.org/doc/
> guide/html/publication-file-source.html#publication-file-source-version> -->
>     <!-- <version include="videos labs"/> -->
>   </source>
>
> Note the @external attribute on the #directories element.  The contents of
> this directory are copied into the build directory so you could include
> shark.png in that directory.  I don't see an assets directory in your
> repository, but if you include assets/images/shark.png and assets/images/
> swimmer.png, your TikZ code should work if you use
> \includegraphics{external/images/shark.png}.  assets is a sibling of source
> in the directory structure.
>
> (I'm really hoping the swimmer escapes in your problem.)
>
> On Tue, Jun 17, 2025 at 1:48 PM Stephen Brown <stephen....@gmail.com> wrote:
>
> Hi Rob,
>
> Here is my source directory structure, with the file in question:
> https://github.com/scbrown-okanagan/exploring-calculus-with-maple/blob/
> main/source/activities_math122/act_shark_attack.ptx <https://github.com/
> scbrown-okanagan/exploring-calculus-with-maple/blob/main/source/
> activities_math122/act_shark_attack.ptx>
> pretext-support/ <https://groups.google.com/d/msgid/pretext-support/>
> > a53bc8f0-afcd-4886-a23c-ab462c707bc3n%40googlegroups.com
> <http://40googlegroups.com> <https://
> > groups.google.com/d/msgid/pretext-support/a53bc8f0-afcd-4886-
> a23c- <http://groups.google.com/d/msgid/pretext-support/a53bc8f0-
> afcd-4886-a23c->
> > ab462c707bc3n%40googlegroups.com?
> utm_medium=email&utm_source=footer <http://40googlegroups.com?
> utm_medium=email&utm_source=footer>>.
>
> --
> You received this message because you are subscribed to the Google
> Groups "PreTeXt support" group.
>
> To unsubscribe from this group and stop receiving emails from it, send
> an email to pretext-suppo...@googlegroups.com.
> To view this discussion visit https://groups.google.com/d/msgid/pretext-
> support/a486eb3d-2c66-45b0-8d8c-abfd94d960a8n%40googlegroups.com
> <https://groups.google.com/d/msgid/pretext-support/
> a486eb3d-2c66-45b0-8d8c-abfd94d960a8n%40googlegroups.com?
> utm_medium=email&utm_source=footer>.
>
> --
> You received this message because you are subscribed to the Google Groups
> "PreTeXt support" group.
> To unsubscribe from this group and stop receiving emails from it, send an email
> support/7353d2e5-c263-430a-9a03-f3c57a159e29n%40googlegroups.com <https://
> groups.google.com/d/msgid/pretext-support/7353d2e5-c263-430a-9a03-
> f3c57a159e29n%40googlegroups.com?utm_medium=email&utm_source=footer>.

Rob Beezer

unread,
Jun 23, 2025, 12:09:41 PM6/23/25
to pretext...@googlegroups.com
On 6/17/25 2:31 PM, David Austin wrote:
> I can confirm that building TikZ images in the sample-article with the pretext/
> pretext script copies the contents of the external directory (@external="media")
> into the "external" directory inside the tmp directory where the build happens.
> It doesn't do this when building prefigure diagrams though. :-(
It does now. ;-)

Stephen Brown

unread,
Jun 23, 2025, 4:35:07 PM6/23/25
to PreTeXt support
Hi Rob,
Thanks for the help! And yes, you may use my example in the sample article.

Lines 28-42:

Stephen

Rob Beezer

unread,
Jun 26, 2025, 11:55:02 AM6/26/25
to pretext...@googlegroups.com
Thanks, Stephen! I might integrate that pretty soon, using some new code for
this purpose. Watch this space for more.

Rob
> --
> You received this message because you are subscribed to the Google Groups
> "PreTeXt support" group.
> To unsubscribe from this group and stop receiving emails from it, send an email
> to pretext-suppo...@googlegroups.com <mailto:pretext-
> support+u...@googlegroups.com>.
> To view this discussion visit https://groups.google.com/d/msgid/pretext-support/
> a33908f0-35cd-4de6-977a-849cd2f15f40n%40googlegroups.com <https://
> groups.google.com/d/msgid/pretext-support/
> a33908f0-35cd-4de6-977a-849cd2f15f40n%40googlegroups.com?
> utm_medium=email&utm_source=footer>.

Rob Beezer

unread,
Jun 26, 2025, 11:55:33 AM6/26/25
to pretext...@googlegroups.com
On 6/23/25 12:09 PM, 'Rob Beezer' via PreTeXt support wrote:
> It does now.  ;-)
And maybe it doesn't anymore. ;-)

I've set everything up to explicitly allow an author to provide a "data"
directory in their source. These would be files you need for things like being
included into a TiKZ image (Stephen Brown's original post) or like files of data
used to create an image that is a plot.

Some hints begin with this paragraph:

Paragraph
https://pretextbook.org/examples/sample-article/html/section-graphics.html#section-graphics-5-10

Rob


Rob Beezer

unread,
Jun 26, 2025, 12:16:38 PM6/26/25
to pretext...@googlegroups.com
On 6/26/25 11:55 AM, 'Rob Beezer' via PreTeXt support wrote:
> I've set everything up to explicitly allow an author to provide a "data"
> directory in their source.  These would be files you need for things like being
> included into a TiKZ image (Stephen Brown's original post) or like files of data
> used to create an image that is a plot.
And I think I've gotten out in front of the CLI, unintentionally.

@Oscar - can we chat about this at Monday Drop-In? Help me remember? ;-)

Rob

Rob Beezer

unread,
Jun 28, 2025, 7:02:14 PM6/28/25
to pretext...@googlegroups.com
On 6/23/25 13:35, Stephen Brown wrote:
> Hi Rob,
> Thanks for the help! And yes, you may use my example in the sample article.

Thanks again, Stephen. I've been going so many directions the past few days, I
forgot to attribute this one to you. I'll go do that now, and it will appear
soon. See

Figure 10.10
https://pretextbook.org/examples/sample-article/html/section-graphics.html#figure-shark-attack

The usual fiddle getting somebody else's LaTeX in place, but it didn't take much
- mostly moving the wavy border definition right into the TikZ proper.

https://github.com/PreTeXtBook/pretext/commit/57f8ed219b8699a0aad7735364307caab08d0d26

Rob

David Austin

unread,
Jul 9, 2025, 10:45:29 AM7/9/25
to pretext...@googlegroups.com
It's taken me a while to get back to this, but this feature works well and is now integrated into PreFigure.  Thanks for your work getting this going.


--
You received this message because you are subscribed to the Google Groups "PreTeXt support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pretext-suppo...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages