4

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

  • 2
    Maybe latexmk -pdf -cd subfolder/subfolder/main.tex (note the -cd) does the trick? – Daniel Sep 17 '17 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 – koleygr Sep 17 '17 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. – Darien Brito Sep 18 '17 at 18:38
5

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

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

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. – koleygr Sep 17 '17 at 20:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.