[Tex/LaTex] Compiling using latexmk from a different directory

inputlatexmkpdftex

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}

\begin{equation}
    \label{simple_equation}
    \alpha = \sqrt{ \beta }
\end{equation}

\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

Best Answer

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

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