4

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.

  • 6
    You have asked 37 questions but only voted on 3 answers. This might be considered rude. Upvoting an answer is a way to show that an answer is useful (and is something distinct from accepting an answer, see the [faq#howtoask] for details). I'd suggest that you go through all your questions and upvote any useful answer to them. – N.N. Nov 20 '11 at 20:58
  • I upvoted the answers. It was not not done intentionally. – Matthias Pospiech Nov 20 '11 at 21:34
  • @MatthiasPospiech: Thumbs up for getting the Suffrage Badge! Way to go! – doncherry Nov 20 '11 at 21:42

1 Answers1

5

\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 }
egreg
  • 1,121,712
  • So the actual error was the use of \ in my bool command. As a side not: This shall be used within the document and be available to the user. Therefore @ifpackageloaded is not a good choice. My command is only an alias: \newcommand{\IfElsePackageLoaded}[3]{\ltx@ifpackageloaded{#1}{#2}{#3}}. However my original question (maybe I actually asked two) was how to implement this using LaTeXs internal temp structures. – Matthias Pospiech Nov 21 '11 at 06:27
  • Then use \ltx@ifpackageloaded, of course. But \let\IfElsePackageLoaded\ltx@ifpackageloaded is more efficient. – egreg Nov 21 '11 at 09:47