[Tex/LaTex] latex @temps(wa) commands

conditionalsetoolboxprogramming

I would like to know more about the temporary commands of LaTeX, such as \@tempa, \@tempswatrue especially for the following construct, where I wonder if my replacement is correct:

% Provide bool for loaded package
\newcommand\isPackageLoaded[1]{%
  \providebool{\tpl@idstring@#1}
  \IfElsePackageLoaded{#1}{%
    \setboolean{\tpl@idstring@#1}{true}%
  }{%
    \setboolean{\tpl@idstring@#1}{false}
  }%
  \boolean{\tpl@idstring@#1}%
}

replaced by this code

\newcommand\isPackageLoaded[1]{%
  \IfElsePackageLoaded{#1}{%
    \@tempswatrue
  }{%
    \@tempswafalse
  }%
  \boolean{\@tempswa}%
}

EDIT: I should add, that the first code is also wrong:

! Undefined control sequence.
\isPackageLoaded #1->\providebool {\tpl@idstring@
#1} \IfElsePackageLoaded {...

EDIT2: working on the boolean sequence, there are some iften vs etoolbox problems.

Using only ifthen commands it does not work:

\newcommand\isPackageLoaded[1]{%
  \provideboolean{tpl@package@#1}%
  \IfElsePackageLoaded{#1}%
    {\setboolean{tpl@package@#1}{true}}
    {\setboolean{tpl@package@#1}{false}}%
    \boolean{tpl@package@#1}%
}
\begin{document}
\ifthenelse{\isPackageLoaded{lmodern}}{lmodern loaded}{lmodern NOT loaded}
\end{document}

with error:

! Incomplete \iffalse; all text was ignored after line 35.
<inserted text>
\fi

And if I use only etoolbox cs:

\newcommand\isPackageLoaded[1]{%
  \providebool{tpl@package@#1}%
  \IfElsePackageLoaded{#1}%
    {\setbool{tpl@package@#1}{true}}
    {\setbool{tpl@package@#1}{false}}%
    \boolean{tpl@package@#1}%
}

I get the error:

! Missing = inserted for \ifnum.
<to be read again>
\escapechar
l.35 \ifthenelse{\isPackageLoaded{lmodern}}
{lmodern loaded}{lmodern NOT loaded}
I was expecting to see `<', `=', or `>'. Didn't.

It seems that both do not work together. But using only one of them does not work for me.

Best Answer

\providebool expects a string of characters as its argument; so

\providebool{tpl@idstring@#1}

should be the way to go. However LaTeX already provides a command for testing whether a package has been loaded: \@ifpackageloaded.

However, saying each time \@ifpackageloaded{somepackage}{true}{false} might be cumbersome, particularly in case more than one package has to be considered.

\newcommand\isPackageLoaded[1]{%
  \providebool{tpl@p@#1}
  \@ifpackageloaded{#1}
    {\setboolean{tpl@p@#1}{true}}
    {\setboolean{tpl@p@#1}{false}}%
}

Then you can say

\isPackageLoaded{hyperref}
\isPackageLoaded{caption}

to set the booleans to the correct value and, for instance,

\ifboolexpr{ bool {tpl@p@hyperref} and bool {tpl@p@caption} }
   { what to do in case both packages are loaded }
   { what to do otherwise }
Related Question