I want to include the source code of my latex generated pdf file in the pdf file itself as printed text. Is there any way to do this?

migrated from stackoverflow.com Jan 29 '14 at 21:38

This question came from our site for professional and enthusiast programmers.

  • Probably -- it would be sort of a quine. LaTeX source files are plain text, and you can write plain text into a PDF. Should not be problematic. Ask on tex.stackexchange. – Jongware Jan 29 '14 at 21:03
  • see package attachfile – user2478 Jan 29 '14 at 21:41
  • 1
    is the purpose of this exercise to have the text of the source code in readable form as part of the pdf output, or to simply attach the file for later reference? (if the former, the attachfile package doesn't seem to cover the territory, nor do the answers to the question cited by @Daniel.) – barbara beeton Jan 29 '14 at 21:49
  • Please post your edit as an answer: the site structure works best for everyone when questions and answers are separated out. – Joseph Wright Feb 2 '14 at 10:28

if the purpose of this exercise is to make the source code visible to readers of the pdf file, say to document self-contained code examples, i've had excellent results with this simple approach.


% your preamble


% your text

  • If that is the purpose, this is beautifully simple and elegant :-) – Daniel Jan 29 '14 at 22:13
  • Yeah thats it thanks alot. I would vote you up if I could. :D – cyber_snipa Jan 30 '14 at 13:18
  • 1
    @cyber_snipa -- i think you might still be able to accept the answer by clicking on a check mark. and since the alleged duplicate question really addresses a different problem, i will vote to reopen. i will also edit the title to add "(print)", and similarly the text, which should make the intent clearer. (you could do that, of course, but i'm being impetuous today.) – barbara beeton Jan 30 '14 at 13:36
  • Sorry for double post, but I got a problem, the lines of the source code are not wraped. How to force that? Sorry if this is a dumb question, I am very new to latex. – cyber_snipa Jan 30 '14 at 13:37
  • @cyber_snipa -- i don't know of any verbatim package that wraps long lines. however, two of the authors of verbatim (rainer schoepf and bernd raichle) should be reachable at the addresses given in the documentation, and i think that asking if a "wrap line, length" option could be provided would be reasonable. (the system i use tex on handles "hard returns" quite happily, so i just keep all my lines short. but this doesn't work for everyone.) – barbara beeton Jan 30 '14 at 14:09

[OPs answer moved from question]

I found a way to do this. You have to use listings as follows.


% optional ˇ
% optional ^





In the \lntinputlisting{} goes the file path. I used \lstset{literate=% ... for special characters that you have to define.I tried to find a way without using literate but nothing worked.


Use the package attachfile and put \expandafter\attachfile\expandafter{\jobname.tex} in your file. This will attach the *.tex-file to the pdf (thanks to Heiko Oberdiek, who answered this question years ago).


here is what you can do if you want to print an example.tex code in a pdf file.



           enter your .tex code here 


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.