Question

I have the following directory structure

project
|-- Makefile
|-- data
    |-- report.tex
    |-- plot_1.eps

One of the rules in the Makefile executes latex data/report.tex. As the working directory is project, I'm getting report.dvi and other output files there only. How do I get those in ./data?

Also, I've included plot_1.eps in the report. But still its expecting the path data/plot_1.eps. Do we need to give the file path relative to the current working directory from where latex is executed? Or location of report.tex?

In the Makefile, I tried

reportdvi: outputparser
        cd data
        latex report.tex
        cd ..

But this didn't change the working directory and the problem persists. What to do?

Was it helpful?

Solution

It's a common 'gotcha' in makefiles. Each command is executed in its own shell, so "cd" only happens within that shell, but subsequent command is run again from make's current directory.

What you want to do is to either put all commands on one line (and you don't need "cd .."):

cd data && latex report.tex

or use \ at the end of the line to tell make to concatenate the lines and pass them all to the shell. Note that you still need ; or && to separate commands.

cd data && \
latex report.tex

OTHER TIPS

The issues of where latex puts its output files and where it expects to find input files has nothing to do with make. You need to find out how to get latex to behave the way you want. Presumably there are flags or similar to control where to put output and where to find included files. Use latex tags on your question.

As for changing directories, each individual line of the recipe is run in a different shell, so you can't change directories in one line and then have that be in effect in the next line: the working directory (in UNIX systems) is a function of the current process and is lost once that process exits.

You want to write something like:

reportdvi: outputparser
         cd data && latex report.tex

(you don't have to cd .., since as I said working directory changes only take effect for the current command)

Having a similar problem I found that, when executing programs after the change of directory, it did nothing for the programs view of its directory "position", then I found that it could be solved like this :

(cd ../../../yourdir && ./yourprogram.exe parameters) > output &

without the paranthesis then "yourprogram.exe" will still have the makefiles current directory as its directory position view.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top