sasozivanovic / memoize

A cross-format package for externalization of graphics and memoization of compilation results in general
LaTeX Project Public License v1.3c
18 stars 3 forks source link

Feature request: share objects that appear multiple times #24

Open JasonGross opened 5 months ago

JasonGross commented 5 months ago

It seems that memoize will re-inlcude memos that have already been included if I have duplicate tikz code in multiple places. (I haven't checked this carefully, so I may be wrong, but one of my pdfs was ~40MB, and when I removed some \input commands that were duplicated in multiple places, leaving only one copy, it shrunk down to ~27MB) If I'm right that memoize doesn't already do this, it would be nice to include the memoized object only once, save it in some box, and reuse the box multiple times if the object appears in multiple places.

sasozivanovic commented 1 month ago

First, sorry for a really late response. I was away for most of the summer, and have only now resumed work on Memoize.

Saving some space is always a good idea, and I already knew that PDFs containing externs are quite bloated, so I investigated a bit. And now I'm fairly confused ...

To load an extern, Memoize uses low-level PDF graphics inclusion commands, in the following spirit:

\pdfximage{file.pdf}%
\pdfrefximage\pdflastximage

My plan was to save \pdflastximage in a macro identified by the extern file name, so that another inclusion of the same extern would simply use \pdfrefximage\thatmacro without loading the extern twice via \pdfximage. I've got the code written, it's only a couple of lines, but before I committed anything, I did some testing. And I realized that the size of the PDF (almost) didn't change upon including the same extern twice even before the change!

I then did further tests, simplifying the situation by taking Memoize out of the picture. I repeated the above code in a test document twice, the PDF stays almost the same size (and I'm testing with a file.pdf of 9MB). If I instead include a PNG image twice, the document size doubles. How about that?

\pdfximage{file.png}%
\pdfrefximage\pdflastximage

Could it be that pdfTeX (& LuaTeX, which behaves the same) automatically do the caching you have suggested, at least for PDFs? Can you test this?

Also, can you retest your original problem? Is the issue still there? If so, can you please tell which TeX distro&version are you using? And post an example document here?