17

Friends, sometimes I had to create a self-contained PHP script. That means the PHP script could have any files within it due to some encoding/decoding algorithm, like Base64. For example, this code is a Base64 encoding of this tiny image: Brazil

Of course, in this particular case, PHP can handle that huge encoded string because it has a bundled base64_decode() function.

I was wondering if this idea is possible to achieve somehow in TeX/LaTeX. I need to point out that I'm just curious on this subject, not that I want to embed whatever in code. Besides, as you noted, the encoded string is huge, so that'd surely make people cry blood.

One possible solution I see is if somebody implement an encoding/decoding algorithm in TeX/LaTeX, like \base64_decode, so it would be possible to use something like this: \includegraphics{\base64_decode{\myhugeunreadablestring}}. Sorry for the acid trip ticket, but I'm really curious on how this insane code would work. =)

6
  • 1
    I once had a similar idea, but wanted to write the encoded image to an external file first. However, I never found a way to make TeX write binary content. It would be possible if you have the base64 code in an environment (e.g. filecontents) which writes it to a file where a command line tool turns it to the binary image which is then included as normal. Commented Jun 24, 2011 at 17:57
  • 1
    PDF and EPS code both may contain images. A TeX engine that allows to in-line PDF or EPS could be a possible route. Commented Jun 24, 2011 at 18:26
  • 1
    @Christian: It's surely a possible route. Probably I need to go to the bowels of TeX and see how things are done in that level. Commented Jun 25, 2011 at 10:03
  • @Martin: Interesting approach, I didn't know filecontents at all! I implemented your suggestion and it works perfectly! Commented Jun 25, 2011 at 10:12
  • @Paulo: Great! I posted it as an answer as a little extend version. Commented Jun 25, 2011 at 10:47

2 Answers 2

10

AFAIK it is not possible to make TeX write binary content, so even if you would implement a base64 decode function you couldn't write it to an external file or e.g. into a \pdfobj (PDF object) directly. However, it would be possible if you have the text based base64 code in an environment (e.g. filecontents, see also the filecontents package which extends this environment) which writes it to a file where a command line tool turns it to the binary image which is then included as normal. You could call the command line tool using \write18 if -shell-escape is active or use an external script or Makefile to do this.

9
  • Thanks, Martin! It works perfectly. In case anyone wants to take a look on my code, here is the PHP script, the command line script example and, at last but not least, my LaTeX code. Cheers! Commented Jun 25, 2011 at 11:35
  • 1
    it is apparently possible to make TeX write binary content: see tex.stackexchange.com/q/8729/2707 Commented Jun 25, 2011 at 22:14
  • @Bruno: Thanks. Are you sure that you can write, e.g. an binary JPG or PDF file, decoded by TeX from Base64, to an external file without any issues? It might finally fail on the fact that \write adds a line break! :-( Commented Jun 25, 2011 at 22:57
  • 1
    @Bruno: Such a feature would be cool, but I'm not sure if it is really required. Commented Jul 19, 2011 at 11:35
  • 2
    Another solution might be github.com/zerotoc/pdfinlimg. Unfortunately I don't have any time to test it. If somebody does, I would be interested in the results.
    – SecStone
    Commented Feb 2, 2012 at 12:47
0

As @Martin Scharrer says, writing binary files from LaTeX is difficult (or impossible if the file format can't end with a line break).

PDF files can be written using only basic ASCII characters, however, and I believe the same goes for eps images. You could therefore write your image data as a hex blob to an ASCII PDF file using the /ASCIIHexDecode filter, and then later include back that image.

You must log in to answer this question.

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