Conditionally execute a command if a specific package is loaded

Assuming that \gitVer and \VersionBox are defined by gitver, just add

\providecommand{\gitVer}{}
\providecommand{\VersionBox}[1]{gitver not loaded}

after the conditional loading.

This exploits the fact that \providecommand does nothing if the command is already defined.


Normally you would just use \@ifpackageloaded, but the command is limited to the preamble. To use it in the document body you need \ltx@ifpackageloaded from the ltxcmds package. Since the macro has an @ in the name, you need to wrap it in \makeatletter and \makeatother.

\documentclass{article}
\usepackage{expl3}
\usepackage{ltxcmds}
%\csname sys_if_shell_unrestricted:T\endcsname{\usepackage{gitver}}
\usepackage{hyperxmp}
\usepackage{hyperref}

\makeatletter
% this commented paragraph is conceptual & needs fixing through a solution from this forum
\@ifpackageloaded{gitver}{
  \hypersetup{pdfversionid = \gitVer}
}{
  \hypersetup{pdfversionid = {}}
}
\makeatother

\begin{document}
Hello world!

\meaning\gitVer
% The following again describes my intended requirements in words
% detect if gitver has been loaded & print \versionBox{} if so; otherwise print "gitver not loaded"
\makeatletter
\ltx@ifpackageloaded{gitver}{\versionBox{}}{gitver not loaded}
\makeatother

\end{document}