18

A commonly asked question on this site is how to embed images directly in a .tex source file, without the need for the image to exist in a separate file. Examples of such questions (often marked as duplicate) are

Unfortunately, the answers are all quite a bit outdated. In particular,

  • Many users responded (years ago) that "the user should simply not want this" and send multiple files as a zip/tar archive instead. However, in this time and age where automatic generation of .tex file becomes more frequent, and single-file text-only generation is much easier (especially when using predefined frameworks) than multifile and/or binary, that argument no longer holds.
  • A package providing such functionality https://gist.github.com/mikeashley/258731 no longer exists. Another package https://github.com/zerotoc/pdfinlimg provides this functionality but only for bitmap images (png/jpg), not for svg/eps/pdf/other vector formats, and encodes it as HEX which is pretty wasteful compared to base64.

Therefore I would like to reopen this question and ask this again: how can one create a command \includebase64image such that

\includebase64image{png}{iVBORw0KGgokIA...}
\includebase64image{pdf}{weEGE2ewFWE58q...}

yields the result of \includegraphicx{a file with content type #1 and contents #2}, namely the image whose content and type are given, appearing at the current location in the generated PDF file?

0

1 Answer 1

10

enter image description here

If you have a command line base64 decoder (base64 -d here) and allow

pdflatex --shell-escape

to run external commands then you do not need anything other than the standard graphics package.

Here I include a base64 encoded pdf image.

\documentclass{article}
\begin{filecontents*}{\jobname.64}
JVBERi0xLjUKJbXtrvsKMyAwIG9iago8PCAvTGVuZ3RoIDQgMCBSCiAgIC9GaWx0ZXIgL0ZsYXRl
RGVjb2RlCj4+CnN0cmVhbQp4nJWTO1LEMAyG+5xCNYXQy69j7BFoWIpsAdx/Bjlksx4nZE0VWSP/
3y9Z+ZwIY46SAjRBThRyhq8rvL4RXL8nAlFDTQw3jxTZvAxmkCKohSEKclzOaCF5SV6KZ/iY3l8c
IZSyKTSBFmGvdASBkqFZce0H50BrTdTqsNBW8cKapNpeA66qXfFts6rEGKP9w7zfKJGhCaJokNKb
vw9GyStcaD5kHvfgQ3HzsAW/PXSCz2bfw1btIMH9Pb7JCsmA+YbVvsvZ1DmonzMYJeRUx86p+F4Z
GGfMXHXvGRXBEkPNrLdUGP0zwzSwNuxGPTK/k6nu5ZpQUTSRI9ne3dAL7zkVo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IAp0cmFpbGVyCjw8IC9TaXplIDgKICAg
L1Jvb3QgNyAwIFIKICAgL0luZm8gNiAwIFIKPj4Kc3RhcnR4cmVmCjk0OQolJUVPRgo=
\end{filecontents*}
\usepackage{graphicx}

\begin{document}

\immediate\write18{base64 -d \jobname.64 > \jobname-tmp.pdf}

picture is

\fbox{\includegraphics[width=3cm]{\jobname-tmp.pdf}}

\end{document}
3
  • Interesting! Would command line access be necessary here? I was looking at tex.stackexchange.com/questions/8729/… where they seem able to produce binary files without special command line necessities, but I do not really understand what's happening nor manage to reproduce it properly.
    – user53964
    Commented Oct 24, 2014 at 20:20
  • @user53964 writing binary files from classic tex is impossible, with luatex it's OK, with enctex extensions on pdftex it might be possible but you'd have to set up special tcx files that allowed arbitrary bytes to be printed and really it's all very fragile, and I'm not convinced it is possible in general and write18 is rather easy (if allowed) so unless it's an absolute hard requiement that that is not allowed.... Commented Oct 24, 2014 at 20:40
  • Thanks @DavidCarlisle for this answer :-) I use Python for dynamically generating the LaTeX Code for publishing books/workbooks etc. Needed this for latex_code shared via API endpoints. I've created a public Github gist for this - gist.github.com/psjinx/ac0230fefe263918e627 Commented Aug 19, 2015 at 2:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .