# Is there some way to embed LaTeX source code in a PDF file?

Several times now I have inadvertently lost the .tex file that generated a PDF file with pdflatex. Given what I know of the internal structure of PDF files, it should be possible to embed a text file containing the original LaTeX file into the PDF file so that it can be extracted later.

I am aware of no technology for doing this, but perhaps you are. Does anybody have something like this?

-
I'd never thought of doing that, but it's a really good idea! –  Seamus Mar 11 '11 at 11:19
LaTeXiT, a small utility for MacOS does this. It is a tool to typeset formulas or other stuff (I use it mostly for small TikZ-Figures) in LaTeX and to directly copy the resulting (cropped) PDF into other applications, such as Keynote or Powerpoint. The cool thing is that it embeds the source code into the PDF, so if you, some day later, copy it back into LaTeXiT, you can edit the image. –  Daniel May 28 '11 at 19:16
That is super-cool. Thanks for telling me about LaTeXiT –  vy32 May 28 '11 at 22:25
If you want to embed the source but keep it secret, use this: tex.stackexchange.com/a/34204/1871. –  alfC Sep 18 '12 at 3:19
LaTeXiT appears to do this by storing metadata inside the PDF file. That's different, but it's still super-cool. –  vy32 Mar 11 '13 at 4:13

I would recommend the embedfile or the navigator package (the latter has the additional advantage of working together with dvipdfmx, see this question of mine). Both are quite similar in usage:

With the embedfile package:

\documentclass{article}

\usepackage{embedfile}
\embedfile{\jobname.tex}

\begin{document}
The document
\end{document}


With the navigator package:

\documentclass{article}

\usepackage{navigator}
\embeddedfile{sourcecode}{./\jobname.tex}

\begin{document}
The document
\end{document}

-
Very nice! This seems better than attachfile since it doesn't insert any objects on the page. Note, however, that you need ./\jobname.tex for xdvipdfmx, as explained in the question I linked to earlier. –  Andrey Vihrov Mar 11 '11 at 11:29
@diabonas: It doesn't work here with xdvipdfmx version 0.7.8, TeX Live 2010, though. My TeX Live comes from Gentoo, so it might have different configuration than the one used by the official TeX Live installer. –  Andrey Vihrov Mar 11 '11 at 11:48
@Andrey That's quite strange - the same version of the program producing different results? You have convinced me, however, to add the ./ to my answer to be sure that it works :-) –  diabonas Mar 11 '11 at 11:54
This worked, but how do I get the embedded file out of my pdf? –  Seamus Mar 11 '11 at 12:39
@Seamus Adobe Reader shows the files embedded/attached to the PDF file in the attachment tab - in the most recent version (Adobe Reader X), you open it by clicking the paper clip icon in the left side bar. As an alternative, you can use pdftk to do the extraction on the command line: pdftk file.pdf unpack_files output ~/folder/ –  diabonas Mar 11 '11 at 13:26

You can attach arbitrary files to a PDF document using the attachfile2 package.

The above package works only with pdfTeX and dvips. For a XeTeX/xdvipdfmx solution, see this question.

-
I was just drafting much the same answer. The OP might also want to look at the original attachfile package for details of the basic options (basically use \attachfile!). –  Joseph Wright Mar 10 '11 at 21:23
Well, I tried attachfile2 and just wasn't able to get it to work properly because of Unicode issues. –  vy32 Mar 11 '11 at 19:41

You can do this in ConTeXt as well. Example:

\setupinteraction [state=start]
\starttext
Foo \attachment[file=attachfile.tex, title=Some file, author=Me]
\stoptext


The file key contains the file name, the title key is used (probably depending on the reader) e.g. for a tool tip (see screenshot). Interaction needs to be activated, otherwise the attachment mechanism don't work.

-