I am using Linux mint cinnamon. Let's say I have .tex file in the sub-directory ~/MyTexfiles/Integration. When I double click on the .tex file to open it, it opens in vim since vim is my default editor, and when I go to compile this file the .pdf file and the auxiliary files appear in the home directory. But if I open a new terminal inside ~/MyTexfiles/Integration, and then open and run the .tex file, the .pdf and other files appear inside this sub-directory. This second result is a desirable result for me. I compile from the command line using the following

:!latexmk -pdf %:r.tex<CR>

Is there any change that I can make to this command so that after opening .tex files by double-clicking, all resulting files after compilation still appear in the sub-directory? I couldn't find a way to do this, any help will be appreciated!

1 Answer 1


You can pass latexmk an extra -cd argument to work from the directory where the *.tex file is located:

:!latexmk -cd -pdf %:r.tex<CR>

From the latexmk(1) man page:

-cd: Change to the directory containing the main source file before processing it. Then all the generated files (.aux, .log, .dvi, .pdf, etc) will be relative to the source file.

This option is particularly useful when latexmk is invoked from a GUI configured to invoke latexmk with a full pathname for the source file.

This option works by setting the $do_cd configuration variable to one; you can set that variable if you want to configure latexmk to have the effect of the -cd option without specifying it on the command line. See the documentation for that variable.

  • 1
    I wonder if the first instance had cwd of ~?
    – D. Ben Knoble
    Commented May 17, 2020 at 13:40
  • A related question, when I open by double-clicking :pwd still shows /home/mahbub. Is there any setting in the terminal that I can change so that the file in truly opened in /home/mahbub/MyTexfiles/Integration', i.e., :pwd shows /home/mahbub/MyTexfiles/Integration?
    – mahbubweb
    Commented May 30, 2020 at 17:21
  • 1
    @MahbubAlam Look at :help 'autochdir'.
    – filbranden
    Commented May 30, 2020 at 17:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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