10

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?

4
  • 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, 2014 at 21:03
  • see package attachfile
    – user2478
    Jan 29, 2014 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.) Jan 29, 2014 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, 2014 at 10:28

4 Answers 4

15

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.

\documentclass{article}
\usepackage{verbatim}

% your preamble

\begin{document}

% your text

\newpage
\verbatiminput{\jobname.tex}
\end{document}
8
  • 1
    If that is the purpose, this is beautifully simple and elegant :-)
    – Daniel
    Jan 29, 2014 at 22:13
  • Yeah thats it thanks alot. I would vote you up if I could. :D Jan 30, 2014 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.) Jan 30, 2014 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. Jan 30, 2014 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.) Jan 30, 2014 at 14:09
2

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).

2

[OPs answer moved from question]

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

\usepackage{listings}
\lstset{breaklines=true}

% optional ˇ
\lstset{literate=%
{Ć}{{\'C}}1
{ć}{{\'c}}1
{Č}{{\v{C}}}1
{č}{{\v{c}}}1
{Š}{{\v{S}}}1
{š}{{\v{s}}}1
{Đ}{{\DJ}}1
{đ}{{\dj}}1
{Ž}{{\v{Z}}}1
{ž}{{\v{z}}}1
}
% optional ^


\begin{document}

%text

\lstinputlisting{SourceCodeFile.tex}

\end{document}

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.

1

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

\documentclass{article}
\usepackage{verbatim}
% Rest of preamble

\begin{document}
    \begin{verbatim}
        % Enter your .tex code here 
    \end{verbatim}
\end{document}

You must log in to answer this question.

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