I wondered if its possible to embed source code of some software you are writing about into the PDF but not displayed by default. E.g. you have a long script I would be nice if the user/reader could click on a link if he needs details, and see the source code of that script.

Best would be if this can be rendered with syntax highlighting (by listings or minted package).

So far I am only aware of either use ECMAscript (which seems not supported by latex) or by OCG layers, as tooltips. For both I am unsure if they support syntax highlighting.

As a workaround I would do it as in the old days with an appendix and hyperref to it.

Do you have any better suggestion?


Yes, you may embed your source code as a file in the pdf. I'm using usually the package attachfile, but there are some more, the embedfile package e.g. or attachfile2.

Using attachfile I'm printing an icon into the margin and explain in the PDF text that clicking on the symbol will provide the attached file.

  • Please note that there is a difference between attaching and embedding at the PDF file level, see the documentation of the packages.
    – TeXnician
    Aug 20 '18 at 9:01
  • @TeXnician the documentation of attachfile says "This package defines an \attachfile command that lets you attach arbitrary files to a pdf document. These files are embedded right in the pdf file, so they get transmitted along with it." That sounds like there is no difference?
    – lucidbrot
    Aug 20 '18 at 11:14
  • 1
    @lucidbrot You need to read the right documentation (attachfile2/embedfile): "The PDF format ([3]) allows the inclusion of fies inside the PDF document. The included fies can be bound to an annotation on a page. Or they can be recorded in a sorted list of embedded fies. The packages attachfie or attachfie2 follow the fist approach, package embedfie uses the latter method." Of course that does not explain the technical details, but it gives the right hint.
    – TeXnician
    Aug 20 '18 at 11:22
  • Yes this is quite cool. Unfortunately not all PDF readers can handle it, e.g. I tried to open with firefox builtin reader and no action took place. But with other readers I had success, very neat. Thank all of you so much.
    – math
    Aug 20 '18 at 11:38
  • 1
    @lucidbrot If I remember correctly you'll find the PDF specification online which should contain these details. Concerning the linking I think they are quite equivalent.
    – TeXnician
    Aug 20 '18 at 12:17

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.