How to conditionally load a package only if shell-escape (write18) is passed
expl3 has a test:
\documentclass{article}
\usepackage{expl3}
\csname sys_if_shell_unrestricted:T\endcsname{\usepackage{gitver}}
\begin{document}
Hello world!
\end{document}
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.
In your case,
\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