13

I have a LaTeX file that executes an external shell command to get some information; this works on my local machine but fails on Overleaf. That's okay, not a big deal—if I can wrap the code in an if-then conditional that checks if it's executing on Overleaf.

Problem: how can I make my LaTeX code check if being run on Overleaf?

Clarification added 2017-07-31: I already know how to test for shell escapes, and conditionalize my code appropriately. The problem is not whether shell escapes work; the problem is really about testing for Overleaf versus other environments.

6
  • 1
    surely you don't want o know if it's on overleaf but if shell escape is enabled?, which you can test for the integer value of \pdfshellescape or \shellescape or in lua status.shell_escape dependin on the tex flavour in use. Commented Jul 31, 2017 at 7:28
  • No; in fact, shell escapes do work, and I already test for that in my code. But the command involves getting information using git, and this works differently in my local environment versus in Overleaf. I have given up trying to figure out why the command fails. I just want to skip the whole thing when running on Overleaf.
    – mhucka
    Commented Jul 31, 2017 at 15:01
  • oh in that case you could shell escape and look at the environment variables, surely overleaf must have some environment variable set with a distinguished value? Commented Jul 31, 2017 at 15:28
  • That would work, but I have not found anything relevant in their documentation so far. I was hoping someone here would have already done something like this and knew the answer.
    – mhucka
    Commented Jul 31, 2017 at 15:31
  • 1
    even simpler yet---the \jobname for Overleaf seems to be 'output'.
    – ivo Welch
    Commented May 29, 2021 at 6:15

2 Answers 2

12

I think the simplest solution is to determine the home path used by Overleaf and test against that. It's unlikely to match your local home path.

To find Overleaf's home path:

\documentclass{article}

\makeatletter
\begingroup\endlinechar=-1\relax
       \everyeof{\noexpand}%
       \edef\x{\endgroup\def\noexpand\homepath{%
         \@@input|"kpsewhich --var-value=HOME" }}\x
\makeatother

\begin{document}
HOME: \texttt{\homepath}.

\end{document}

Then copy the resulting path and test against that:

\documentclass{article}

\makeatletter
\begingroup\endlinechar=-1\relax
       \everyeof{\noexpand}%
       \edef\x{\endgroup\def\noexpand\homepath{%
         \@@input|"kpsewhich --var-value=HOME" }}\x
\makeatother

\def\overleafhome{/home/whatever}% change as appropriate

\begin{document}
\ifx\homepath\overleafhome
 Overleaf.
\else
 Not Overleaf.
\fi

\end{document}
5
  • 1
    I used this approach. Based on some testing, that path on Overleaf appears to be /home/wl. Thank you very much for this solution.
    – mhucka
    Commented Aug 7, 2017 at 20:00
  • Addendum: I examined other environment variables, per David Carlisle's comments at the top. Unfortunately, no other variable other than HOME seemed safe enough to use (in the sense that I couldn't be sure a collaborator would get the expected behavior if the used the files in their local environment).
    – mhucka
    Commented Aug 7, 2017 at 20:02
  • 2
    Any suggestions on making this work under windows as well? Unfortunately the \ in the windows path causes the error :1: Undefined control sequence. l.1 C:\Users. I tried piping the output of kpsewhich to tr, but couldn't figure out how to send it \ symbols.
    – kgoodrick
    Commented Mar 24, 2020 at 22:06
  • @kgoodrick I have the same problem. Have you been able to resolve It? Commented Mar 19, 2022 at 20:48
  • These days the home path seems to be /tmp. (tex.stackexchange.com/a/598669/9019 is probably easier from latex, as noted by DaiBowen, but I had a harder time checking that in latexmkrc than just $ENV{'home'}.)
    – Danica
    Commented Sep 16 at 8:05
4

Just like some others, I was struggling with Windows compatibility because of paths containing backslashes.
I found a solution by combining ivo Welch's suggestion of looking at the jobname with this:

\def\StripPrefix#1>{}
\def\jobis#1{FF\fi
    \def\predicate{#1}%
    \edef\predicate{\expandafter\StripPrefix\meaning\predicate}%
    \edef\job{\jobname}%
    \ifx\job\predicate
}

\if\jobis{output}%
    Overleaf.
\else
    Not Overleaf.
\fi

It works because overleaf has a fixed jobname of output. Your local build probably uses the .tex filename.

EDIT: alternatively, a bit cleaner:

\def\StripPrefix#1>{}
\def\isOverleaf{\fi
    \def\overleafJobname{output}% overleaf defaults to 'output' as \jobname
    \edef\overleafJobname{\expandafter\StripPrefix\meaning\overleafJobname}%
    \edef\job{\jobname}%
    \ifx\job\overleafJobname
}

\if\isOverleaf%
    Overleaf.
\else
    Not Overleaf.
\fi
2
  • 2
    With recent versions of LaTeX, it's bit easier to use : \ExplSyntaxOn \str_if_eq:VnTF \c_sys_jobname_str {output} {Overleaf} {Not~Overleaf} \ExplSyntaxOff Commented Jul 13, 2022 at 10:13
  • tex.stackexchange.com/a/598669 is even cleaner
    – Dai Bowen
    Commented May 3, 2023 at 12:46

You must log in to answer this question.

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