r/LaTeX•
karper

What's the right™ way to do this with LaTeX?

Consider the following tree:

|
-- file1.tex
|
--dir
  |
  -- file2.tex
  |
  -- image.eps

Here's a barebones version of file1.tex

\documentclass{article}
\usepackage{graphicx}
\begin{document}
\input{dir/file2}
\end{document}

Now, here's the contents of file2.tex

\includegraphics{image}

How should I go about compiling file1.tex? Simply issuing the following command does not work (can't find image-eps-converted-to.pdf):

pdflatex file1

So, what's the proper, LaTeX way to compile file1.tex?


I'm basically trying to work with gnuplot's epslatex terminal. Currently, I produce standalone plots, which I compile to PDF and include them into the main document. Ideally, I would like to keep the eps/tex output in a separate figures directory and include (or input) them into my main document. Currently, this doesn't work. Hence the question.


EDIT:

Here's the answer to my question. Thanks everyone for pointing me in the right direction:

The answer is to use the \graphicspath{} option. Here is a nice writeup about it. It also mentions other options such as modifying TEXINPUTS (which I feared was the only way) or using import.

So, just adding one extra line to file1.tex, it compiles perfectly. Here's the proper version for reference:

\documentclass{article}
\usepackage{graphicx}
  \graphicspath{{dir/}}
\begin{document}
\input{dir/file2}
\end{document}

Posted Apr 16, 2012, 9:47 PM UTC · edited

516
bowling_m_h•

You could use \graphicspath{{.}{dir/}} to tell graphicx to look in ./dir for image.eps.

Edit: Fixed the syntax for \graphicspath{}

4
karperOP•

Perfect!

That seems to be the right™ way of doing things!

You win one unit of LaTeX street cred! :)

2
adimit•

IIRC, you can't compile \includegraphics{*.eps} with pdflatex, but only with latex & dvipdf.

If you want to compile a tex file with some .eps in it, your options are to use latex & dvipdf or to convert the eps into something else.

If you want to convert it, you should probably use ImageMagick. A simple convert image.eps image.png should work, but might yield something rather pixelated.

Of course, if you want to include both eps and {png,pdf,gif…} in one TeX document, you're going to have to convert.

3
bakesale•

I like to leave the .eps and .pdf files both in my figures directory (included with \graphicspath{{figures/}}), so that a co-author can use latex or pdflatex, it works either way. When you do \includegraphics just leave the file extension off, the program will choose the right one. Convert your .eps to .pdf with ps2pdf (or one of many other tools), it is a "pure" conversion, there will be no pixellation.

7
adimit•

Good point! I forgot that you could just convert eps to pdf, since it's actually just Post Script.

1
Kirby54925•

TeX Live provides epstopdf, which does the job exactly.

1
karperOP•

When you do \includegraphics just leave the file extension off

Ah, my bad. I should have left the extension off. Gnuplot's tex/eps output certainly does.

Fixed.

1
karperOP•

Oh, and thanks for the pointer about \graphicspath. That apparently is the answer I was looking for.

1
karperOP•

IIRC, you can't compile \includegraphics{*.eps} with pdflatex, but only with latex & dvipdf.

This is a rather prevalent misconception and not true for the past few years. In fact, you might be tickled to know that both latex and pdflatex actually are mapped to the some command. See below (from my machine, arch linux with texlive-2011):

$ file `which latex`
/usr/bin/latex: symbolic link to `pdftex'
$ file `which pdflatex`
/usr/bin/pdflatex: symbolic link to `pdftex'

So, regardless of whether you call latex or pdflatex, it will actually call pdftex. So, when you include an eps file and compile with pdflatex, it will automagically convert the eps file into pdf before including it. You'll see a temporary file called image-eps-converted-to.pdf created for this purpose.

A simple convert image.eps image.png should work, but might yield something rather pixelated.

Going from pretty vector graphics to raster? Nope! :)

Besides, the format doesn't matter. This compilation fails as I'm doing it currently and it's not because of the image format.

tldr; There's no difference between latex and pdflatex today. You can include all formats.

2
brownck•

You could also just copy all your image files to the directory with file1 too. That's what I did, but adding a /graphicspath is probably the right way.

2
karperOP•

\graphicspath it is! Thanks!

1
AFineTapestry•

I would use \subimport{dir/}{file2}. Then in file2: \includegraphics{image} since subimport changes the cwd.

2
karperOP•

Thanks, see my solution above — the link mentions using the import package to do this.

Having played with all three options (adding a graphicspath, modifying TEXINPUTS and using import/subimport), I guess the import way is how the LaTeX devs intended this to be solved. I can see how it's robust enough to handle several layers of files importing more files from other directories.

Still, for a simple case like this, graphicspath is sufficient and works pretty well.

2
existentialhero•

If you're using a Unix-type operating system (especially a Linux, but probably OS/X as well), rubber is a great way to manage all the compile-this-then-compile-that silliness.

1
karperOP•

I wrote a really neat script that I rely on when compiling my tex files. I should post it here sometime...

1
AFineTapestry•

http://code.google.com/p/latex-makefile/

This makefile also handles the typesetting (and bibtex-ing and whatnot) in a very clean way.

1