Graphics, Figures & TablesCan you embed graphics in LaTeX code?

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
uqan
Posts: 4
Joined: Mon Feb 28, 2011 5:23 am

Can you embed graphics in LaTeX code?

Post by uqan »

Hi all,

I'm using pdflatex to generate PDF files from LaTeX code, and the LaTeX is being generated on the fly by a web page. Because pdflatex is being run by the web server, it only has access to one file (the LaTeX code) so I can't use any of the commands which refer to external image files for security reasons.

I'm wondering whether there's a way to embed the image data within the LaTeX code, so that I can place images into my final PDF file without using any external files.

Does anyone know if this is possible?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

josephwright
Site Moderator
Posts: 814
Joined: Tue Jul 01, 2008 2:19 pm

Can you embed graphics in LaTeX code?

Post by josephwright »

What type of graphic is it? You could draw it in Tikz if it is a vector-based case.
Joseph Wright
qubyte
Posts: 8
Joined: Wed Jan 26, 2011 3:02 pm

Re: Can you embed graphics in LaTeX code?

Post by qubyte »

Another vote for TikZ, so long as you're drawing something like a diagram. It is also possible to convert other (pure) vector graphics into pgf or TikZ code which can be embedded in a LaTeX document. I think you can probably arrange that as a pipe, but you'll lose stuff like blends.

If you're asking about raster graphics like png or jpeg files then I think you may be stuck. A possibility is to use potrace or another tracing utility to turn a raster graphic into a vector graphic and then transform that into pgf or Tikz.

I am a little confused though. Do you mean that the result of pdfLaTeX can read only .tex files and write whatever it likes? LaTeX likes to write a whole bunch of files when it processes and it seems rather odd that it be allowed to do that and not to use other files in the same directory as the source or below. I can see no reason not to allow the upload of graphics files so long as the interface is carefully coded...
uqan
Posts: 4
Joined: Mon Feb 28, 2011 5:23 am

Re: Can you embed graphics in LaTeX code?

Post by uqan »

Thanks for the quick replies! The image is my employer's logo, unfortunately it's a bit too complex for me to reproduce manually but I do have a vector version in EPS format.

I'll have to look into TikZ, thanks for the suggestion.

I suppose in theory it would be possible to stick an image file alongside the .tex file before processing, but because everything happens in the temp directory and all the filenames are randomly generated I don't trust it to work properly every time. For example if the image file could not be created, I suspect pdflatex would still successfully produce a PDF file without the image. Maybe I'm just being overly cautious, but I'd much rather have everything in one input .tex file - less room for error and less error handling code required!
qubyte
Posts: 8
Joined: Wed Jan 26, 2011 3:02 pm

Can you embed graphics in LaTeX code?

Post by qubyte »

If it's a pure vector eps (and not simply a wrapper for a raster image) then you're in good shape. Check out ps2pgf:

http://sourceforge.net/projects/eps2pgf/

You'll be able to put the contents of the file generated into your tex source file. Be warned though, depending on how complex the graphic is this can slow down the compilation!

I'm not sure if this still works, but I once coaxed Inkscape into converting an eps file into TikZ.
uqan
Posts: 4
Joined: Mon Feb 28, 2011 5:23 am

Can you embed graphics in LaTeX code?

Post by uqan »

Thanks for the suggestions! The file is definitely vector-based, but unfortunately eps2pgf doesn't work:

Code: Select all

ERROR: A PostScript error occurred.
ERROR:     Type: Error: /undefined in q
ERROR:     Operand stack (max top 10 items):
ERROR:       (empty)
ERROR: Execution failed due to a PostScript error in the input file.
But Inkscape was able to import the .eps and produce a .tex file using pstricks. Unfortunately pstricks seems to be in a separate installation package so I don't think I'm going to have any luck getting it made available on the production server.

I decided this was going to be too difficult so I relented and tried to make it work the "normal" way, referencing an external image file. Unfortunately that doesn't seem to work either - regardless of what produces the image (I tried Inkscape and epstopdf) I get this error from pdflatex:

Code: Select all

Error: May not be a PDF file (continuing anyway)
Error: PDF file is damaged - attempting to reconstruct xref table...
Error: Couldn't find trailer dictionary
Error: Couldn't read xref table

!pdfTeX error: pdflatex (file ./logoc.pdf): xpdf: reading PDF image failed
 ==> Fatal error occurred, no output PDF file produced!
This was when running pdflatex from the command line when the image was in the current directory. I think maybe my PDF files aren't supposed to have images in them :cry:
Post Reply