This is a followup to this question: Overleaf dynamic PDF filenames. The question and answer seem relevant but I don't know how to apply them to my situation.

Hint: ShellEscape, as needed by said solution, does work in Overleaf (the linked solution was written specifically regarding Overleaf), but when I try to implement it with a conditional, the compilation either crashes or times out, see below.

I use Overleaf for all of my LaTeX. When I create assignments, I use a \newif to embed the key in the same document as the assignment. It's easy enough to comment out \keytrue to toggle between the key and the assignment while editing and while downloading the PDFs. The annoying thing is that I end up with files with names like Math_Homework.pdf and Math_Homework (1).pdf. Then I have to figure out which one is the key and manually rename it in my computer's document management interface.

I really wish there was a way to have a different file name for the key than the assignment. A stretch goal would be to generate the key and the assignment at the same time with the appropriate names. Ideally, my PDFs would automatically come out to something like Math_Homework.PDF and Math_Homework_KEY.PDF

Minimum non-working example: main.tex, thisif.tex, and print.tex

main.tex (this compiles on its own)

\makeatletter %allows you to call \@title

\title{Math Homework \ifkey KEY \fi}



\ifkey Answer \else Question \fi


thisif.tex (just a header so that both other files are seeing the same conditional)

\keytrue % comment this out to view student version

print.tex (times out or crashes when I try to compile it)

\ShellEscape{latexmk -pdf main}

\ifkey \includepdf[pages=-]{mwe-KEY.pdf} \else \includepdf[pages=-]{mwe.PDF} \fi
  • 4
    (I'm on support staff at Overleaf.) The code you've posted here doesn't cause a timeout or "crashed" compile for me, so something else must be going on. (But I don't think this is the same code you're actually trying to use, because the code here doesn't use \ShellEscape at all.) If you're unable to find the cause, and don't want to share your complete code here on TeX Stack Exchange, you can write to us at Overleaf Support ([email protected]) with your non-working project's URL and we'll take a closer look. Commented Jul 20, 2022 at 19:34
  • 1
  • 1
    I think you can add -jobname=mwe.pdf to the latexmk command invocation or something like that (read texdoc latexmk to see how it's used) -- you can try running it directly in the shell and see what it does before entering it into the TeX file.
    – user202729
    Commented Jul 21, 2022 at 4:02

1 Answer 1


Based on comments by @user202729, I changed print.tex to solve the problem.



\ifkey \ShellEscape{latexmk -pdf -jobname=mwe-KEY main.tex} \else \ShellEscape{latexmk -pdf -jobname=mwe main.tex} \fi


\ifkey \includepdf[pages=-]{mwe-KEY.pdf} \else \includepdf[pages=-]{mwe.pdf} \fi


Now if I compile twice (once with \keytrue in thisif.tex, once with it commented out), Overleaf caches both PDFs and I am able to download each of them with the desired names.

Thanks for the helpful comments!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .