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


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?


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.



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

picture is


  • 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 Oct 24 '14 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.... – David Carlisle Oct 24 '14 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 – pankaj28843 Aug 19 '15 at 2:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.