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)
\documentclass{article}
\include{thisif}
\makeatletter %allows you to call \@title
\title{Math Homework \ifkey KEY \fi}
\begin{document}
\begin{center}
\begin{Large}
@title
\end{Large}
\end{center}
Directions:
\ifkey Answer \else Question \fi
\end{document}
thisif.tex (just a header so that both other files are seeing the same conditional)
\newif\ifkey
\keytrue % comment this out to view student version
print.tex (times out or crashes when I try to compile it)
\documentclass{article}
\include{thisif}
\usepackage{shellesc,pdfpages}
\ShellEscape{latexmk -pdf main}
\begin{document}
\ifkey \includepdf[pages=-]{mwe-KEY.pdf} \else \includepdf[pages=-]{mwe.PDF} \fi
\end{document}
\ShellEscapeat 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 (support@overleaf.com) with your non-working project's URL and we'll take a closer look. – Paul Gessler Jul 20 '22 at 19:34-jobname=mwe.pdfto thelatexmkcommand invocation or something like that (readtexdoc latexmkto 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 Jul 21 '22 at 04:02