111

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 Mar 11 '11 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 May 28 '11 at 19:16
  • That is super-cool. Thanks for telling me about LaTeXiT – vy32 May 28 '11 at 22:25
  • 1
    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
6

The embedall has been made exactly for that:

Just include

\usepackage{embedall}

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 Mar 7 '18 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 Mar 7 '18 at 1:15
  • 1
    It's a pity that it has only 6 upvotes while other answers have many upvotes – Agile_Eagle Oct 17 '18 at 15:57
66

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}
  • 3
    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
  • 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. – Andrey Vihrov Mar 11 '11 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 Mar 11 '11 at 11:54
  • 6
    This worked, but how do I get the embedded file out of my pdf? – Seamus Mar 11 '11 at 12:39
  • 6
    @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
28

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 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
13

You can do this in ConTeXt as well. Example:

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

result

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.

1

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.

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.