# Compiling using latexmk from a different directory?

I have 2 questions:

1) If I am in a directory parent, how can I compile a PDF file from a main.tex file that is in directory parent/subfolder/subfolder1/ to that same last location?

I've tried latexmk -pdf subfolder/subfolder/main.tex, and it does work, but it compiles all auxiliary files and the resulting PDF file to the directory from where the script was run (parent). Is there any way to make it output the aux files and the PDF to the directory where the target main.tex is located, while still running this command from parent?

2) My main.tex makes a reference to a file located in a folder on a different directory, namely: ../commands/command.tex . Using \input{../commands/command.tex} and compiling from that same directory works, however once I am in directory parent and try to do the same thing then I get the message ! LaTeX Error: File ../commands/command.tex' not found. How can I successfully import this file and compile it from parent?

Here a simplified version of my main.tex file:

\documentclass{article}

\input{../commands/command.tex}

\begin{document}

$$\label{simple_equation} \alpha = \sqrt{ \beta }$$

\test % my defined command

\end{document}


and my command.tex file:

\newcommand{\test}{dummy text}


Please bear in mind the folder structure is:

parent
subfolder
subfolder1
main.tex
commands
command.tex


And I would like to compile main.tex from directory parent

• Maybe latexmk -pdf -cd subfolder/subfolder/main.tex (note the -cd) does the trick? Sep 17, 2017 at 17:34
• On linux systems this problem can be solved by a #!/bin/bash script than runs pdflatex or latexmk or whatever command you like from any directory (I have done it on mine). I think it works on mac too because it is a linux too (but never had one and I am not sure)... As far as I know windows supports linux commands by a way these days... You could ask there if you are a windows user Sep 17, 2017 at 18:48
• @Daniel, so simple! Thank you, that worked perfectly. koleygr, thanks for the suggestion, I tried this approach after posting this question, but I think Daniel's simple answer is what I was looking for. Sep 18, 2017 at 18:38

As was already mentioned in a comment, the -cd option to latexmk does exactly what you request

latexmk -pdf -cd subfolder/subfolder/main.tex


On linux systems (may be mac too) you can create a #!/bin/bash script (Let's name it myLTXRun.sh and save it on the main.tex's directory):

This script will contain the lines:

#!/bin/bash
COMMAND1="cd /path/of/main/latex/file"
COMMAND2="latexmk main.tex"
echo "Running:"$COMMAND1$COMMAND1
echo "Running:"$COMMAND2$COMMAND2


Then you will give the required permissions to the .sh file with the command (from the directory of the main.tex file):

chmod +775 myLTXRun.sh


Then from any directory you can run the command:

sh /path/to/mainFile/dir/myLTXRun.sh


(Tested on debian GNU/Linux)

On windows 10 you can use exactly the same steps after installing the Windows subsystem for linux: here are instructions

(I haven't test it yet!)

From a command prompt the last command have to be replaced with:

bash -c "sh /path/to/mainFile/dir/myLTXRun.sh"

• The method works for any kind of supported tex/latex etc commands as far as I can understand. I tested it with pdflatex. Sep 17, 2017 at 20:28