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?

  • 7
    I'd never thought of doing that, but it's a really good idea!
    – Seamus
    Commented Mar 11, 2011 at 11:19
  • 6
    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
    Commented May 28, 2011 at 19:16
  • That is super-cool. Thanks for telling me about LaTeXiT
    – vy32
    Commented May 28, 2011 at 22:25
  • 1
    If you want to embed the source but keep it secret, use this: tex.stackexchange.com/a/34204/1871.
    – alfC
    Commented Sep 18, 2012 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
    Commented Mar 11, 2013 at 4:13

5 Answers 5


The package embedall has been made exactly for that:

Just include


just before \begin{document} and it will embed all graphics, listings, and the tex source.

side note: The CTAN tag archival has more related tools listed. For instance, mkjobtexmf saves all .sty files. They could be embedded in the PDF, but I am unsure whether that's overkill. The package skb proposes a new kind of workflow, which does not really seem to tackle your issue.

  • 1
    This is now the correct answer! Yea!
    – vy32
    Commented Mar 7, 2018 at 1:03
  • You're welcome! I just added a link to archival, because one could find the related tooling mkjobtexmf interesting, too. Hlpe, you like it, too.
    – koppor
    Commented Mar 7, 2018 at 1:15
  • 1
    It's a pity that it has only 6 upvotes while other answers have many upvotes Commented Oct 17, 2018 at 15:57
  • 1
    It should be noted that embedall relies on the embedfile package, and so it only works with pdfTeX and LuaTeX (added in embedfile v2.7). There is no XeTeX support at the moment. Commented Nov 19, 2019 at 10:56

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:



The document

With the navigator package:



The document
  • 4
    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. Commented Mar 11, 2011 at 11:29
  • 1
    @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. Commented Mar 11, 2011 at 11:48
  • 1
    @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
    Commented Mar 11, 2011 at 11:54
  • 7
    This worked, but how do I get the embedded file out of my pdf?
    – Seamus
    Commented Mar 11, 2011 at 12:39
  • 10
    @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
    Commented Mar 11, 2011 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.

  • 5
    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
    Commented Mar 10, 2011 at 21:23
  • Well, I tried attachfile2 and just wasn't able to get it to work properly because of Unicode issues.
    – vy32
    Commented Mar 11, 2011 at 19:41

You can do this in ConTeXt as well. Example:

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


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.


The question has already been answered, but I would like to add a warning:

Strategies for embedding the source code may run afoul of PDF/X, and possibly also PDF/A. So, if your file must be PDF/X or PDF/A, be careful.

Rationale: PDF/X is intended for reliable print. Any non-printing content, or scripted content, violates the spec. PDF/A is intended for archiving and also (depending on level) accessibility. Non-visible content may violate the accessibility spec.

If I had a "one size fits all" answer, I would provide it.

  • 4
    This answer is wrong. Even the PDF/X standards from 2003 allow embedded files. Yes, JavaScript and links are forbidden but embedding is not. An embedded .iccfile is even one of the key features of a PDF/X, isn't it?
    – tanGIS
    Commented Apr 1, 2020 at 18:46

You must log in to answer this question.

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