# How to conditionally load a package only if shell-escape (write18) is passed

The package gitver requires -shell-escape to fetch git metadata from the manuscript's git repository on the local filesystem.

However, it is needless to pass the shell-escape flag when compiling the source outside of a gitVCS repo (such as on my advisor's machine). It is tedious to comment-out and comment-in this line appropriately. It is also hard to change other collaborator's (good) habits of not compiling with -shell-escape enabled. However, gitver complains loudly and errors out/halts if shell-escape is not detected.

Hence, can I change my preamble in such a way that a generic source line (not just to loading gitver package, but the same argument applies to minted and other generic latex commands) is executed only when compiled with shell-escape?

Minimal conceptual example:

\documentclass{article}

%%% Somehow detect shell-escape here
\usepackage{gitver} % <-- load only if compiled with shell-escape
%%% end of test

\begin{document}
Hello world!
\end{document}

• For the given problem you may want to have a look at gitinfo2 instead of gitver since gitinfo2 doesn’t require shell-escape (I think). – Jendrik Stelzner Sep 20 at 1:55

expl3 has a test:

\documentclass{article}
\usepackage{expl3}
\csname sys_if_shell_unrestricted:T\endcsname{\usepackage{gitver}}
\begin{document}
Hello world!
\end{document}

• I thought I had added this to shellescape package (or at least a common name for a number to test) but apparently not:-) – David Carlisle Sep 19 at 12:37
• @DavidCarlisle I checked shellesc first ;-) – Ulrike Fischer Sep 19 at 12:38
• @UlrikeFischer perfect. Works like a charm. Upvoted & accepted. – Krishna Sep 19 at 13:51
• Imho you should make a feature request for the package. It should have a "warn" mode and quit cleanly if shell-escape is not enabled. As you said: it is a pain if such packages then errors. – Ulrike Fischer Sep 19 at 13:56
• @DavidCarlisle Maybe pdftexcmds? – egreg Sep 19 at 14:11

You can use pdftexcmds and define a three-branch test:

\usepackage{pdftexcmds}

\makeatletter
\newcommand{\ShellEscapeURN}[3]{%
\ifcase\pdf@shellescape
\expandafter\@firstofthree  % 0 = no shell escape
\or
\expandafter\@secondofthree % 1 = unrestricted
\or
\expandafter\@thirdofthree  % 2 = restricted
\else
\expandafter\@firstofthree
\fi
{#3}{#1}{#2}%
}
\providecommand{\@firstofthree}[3]{#1}
\providecommand{\@secondofthree}[3]{#2}
\providecommand{\@thirdofthree}[3]{#3}
\makeatother


The three arguments are “code for Unrestricted shell escape”, “code for Restricted shell escape” and “code for No shell escape” respectively.

\ShellEscapeURN{\usepackage{gitver}}{}{}


It might be more convenient to use expl3 so as not to expose the actual values of the integer variable denoting the status of shell escape.

\usepackage{expl3}

\ExplSyntaxOn
\cs_new:Npn \ShellEscapeURN #1 #2 #3
{
\sys_if_shell_unrestricted:TF
{ #1 } % unrestricted shell
{
\sys_if_shell_restricted:TF
{ #2 }
{ #3 }
}
}
\ExplSyntaxOff