# Can you embed an image so it doesn't need to be stored as a separate file?

I'd like to know whether it is possible to embed an image file into LaTeX code, such that the picture will appear in a resulting PDF file without it having to be stored in a separate .eps file during processing.

The reason is that I am generating LaTeX code on the fly (using PHP) and running pdflatex in a chroot environment, so I have to copy any required files before calling pdflatex. Since I am generating lots of different PDFs, they all use different image files. My options are thus:

• Hard-code which .eps files to copy, and change the PHP code to match the LaTeX code each time an image changes (bad, hard to maintain)
• Copy every single .eps file, whether it's used or not (bad, slow)
• Embed the image in the source LaTeX code (ideal)

Everything I can find about \includegraphics seems to suggest images can only be read in from external files. Is there any way an .eps file can somehow be embedded within LaTeX code?

• You can, in theory, use filecontents package for this. But it's gonna be a loooooooooo3E8F63E2B26234CAooooooong .tex file. – percusse Mar 9 '12 at 1:20
• Did you consider (a) using hard links instead of copying; (b) using a tmpfs; (c) re-using the chroot environment for multiple documents? – Andrey Vihrov Mar 9 '12 at 1:31
• If your EPS files are graphics or auto-generated vector drawings, you could use TikZ for them. – Andrey Vihrov Mar 9 '12 at 1:33
• You should submit these as answers instead of comments so I can upvote them :-) @AndreyVihrov: I don't have control over the server environment, so I can't use hard links as the files are hosted on a SAN and you can't hard-link across filesystems, tmpfs still means I have to read all the .eps files, and I can't reuse the chroot environment as it is created fresh for each launch of pdflatex (I assume for security reasons.) – Malvineous Mar 9 '12 at 1:45
• You could also parse the latex document for \includegraphics and then copy the relevant files – Aditya Mar 9 '12 at 1:48

## 1 Answer

Here is a ridiculous example of what I meant in the comments (A reminder; Use LaTeX-> PS -> PDF but not LaTeX -> PDF for the discovery of the file in the directory) :

\documentclass{article}
\usepackage{filecontents,graphicx}

\begin{filecontents*}{myepsfile.eps}
%!PS-Adobe-3.0 EPSF-3.0
%%Creator: Ipelib 70010 (Ipe 7.0.10)
%%CreationDate: D:20120309025807
%%LanguageLevel: 2
%%BoundingBox: 143 511 305 577
%%HiResBoundingBox: 143.8 511.8 304.2 576.2
%%EndComments
%%BeginProlog
%%BeginResource: procset ipe 7.0 70010
/ipe 40 dict def ipe begin
/np { newpath } def
/m { moveto } def
/l { lineto } def
/c { curveto } def
/h { closepath } def
/re { 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto
neg 0 rlineto closepath } def
/d { setdash } def
/w { setlinewidth } def
/J { setlinecap } def
/j { setlinejoin } def
/cm { [ 7 1 roll ] concat } def
/q { gsave } def
/Q { grestore } def
/g { setgray } def
/G { setgray } def
/rg { setrgbcolor } def
/RG { setrgbcolor } def
/S { stroke } def
/f* { eofill } def
/f { fill } def
/ipeMakeFont {
exch findfont
dup length dict begin
{ 1 index /FID ne { def } { pop pop } ifelse } forall
/Encoding exch def
currentdict
end
definefont pop
} def
/ipeFontSize 0 def
/Tf { dup /ipeFontSize exch store selectfont } def
/Td { translate } def
/BT { gsave } def
/ET { grestore } def
/TJ { 0 0 moveto { dup type /stringtype eq
{ show } { ipeFontSize mul -0.001 mul 0 rmoveto } ifelse
} forall } def
<<
/PatternType 1
/PaintType 2
/TilingType 2
/BBox [ 0 0 100 4 ]
/XStep 99
/YStep 4
/PaintProc { pop 0 0 100 1 re fill} bind
>>
[ 0.5 -0.866025 0.866025 0.5 0 0 ]
makepattern
/Pat95 exch def
<<
/PatternType 1
/PaintType 2
/TilingType 2
/BBox [ 0 0 100 4 ]
/XStep 99
/YStep 4
/PaintProc { pop 0 0 100 1 re fill} bind
>>
[ 0.866025 0.5 -0.5 0.866025 0 0 ]
makepattern
/Pat96 exch def
/patg { [/Pattern /DeviceGray ] setcolorspace setcolor } def
/patrg { [/Pattern /DeviceRGB ] setcolorspace setcolor } def
end
%%EndResource
%%EndProlog
%%BeginSetup
ipe begin
%%EndSetup
0 J 1 j
q 192 576 m
192 512 l
256 512 l
256 576 l
h 0.4 w S
Q
q 144 544 m
192 544 l
0.4 w S
q q 192 544 m
185 546.331 l
185 541.669 l
h q f* Q S
Q
Q
Q
q 256 544 m
304 544 l
0.4 w S
q q 304 544 m
297 546.331 l
297 541.669 l
h q f* Q S
Q
Q
Q
showpage
%%Trailer
end
%%EOF
\end{filecontents*}

\begin{document}
\begin{figure}%
\includegraphics[width=\columnwidth]{myepsfile}%
\caption{adsf}%
\label{fig:asdfasd}%
\end{figure}
\end{document}

• Note that you can just use \usepackage{epstopdf} to make it work with also with LaTeX to PDF directly. – user53964 Oct 24 '14 at 20:51