8

I want to test if either inputenc or selinput are loaded. My problem now is to do complex boolean expressions and I have no clue how to do that with LaTeX.

The basic code would be something like this:

\AtBeginDocument{%
  \ifpdftex{% only if pdftex 
    \ifthenelse{%
      \not\isPackageLoaded{inputenc} \and \not\isPackageLoaded{selinput}
    }%
    { raise warning }
    { everything ok }
  }{}
}

however I assume that \not and \and and such are not available directly.

the code for testing is based on this (not fully tested)

\RequirePackage{scrlfile, etoolbox}%

\SetupIfPackageLoaded{inputenc}
\SetupIfPackageLoaded{selinput}

\newcommand\SetupIfPackageLoaded[1]{%
   \providebool{TemplateIfPackageLoaded@#1}
   \setboolean{TemplateIfPackageLoaded@#1}{false}
   \AfterPackage{#1}{%
      \setboolean{TemplateIfPackageLoaded@#1}{true}
   }%
}%

\newcommand\isPackageLoaded[1]{%
  \boolean{TemplateIfPackageLoaded@#1}
}

the code is for a package which I want to use to test errors in a LaTeX template mostly in the preamble.

N.N.
  • 36,163

2 Answers2

7

Pure LaTeX solution

A pure LaTeX kernel solution can use \@ifpackageloaded and \if@tempswa (a scratch conditional):

\ifPDFTeX
  \@tempswafalse % initialize
  \@ifpackageloaded{inputenc}{\@tempswatrue}{}
  \@ifpackageloaded{selinput}{\@tempswatrue}{}
  \if@tempswa
    <OK>
  \else
    <ERROR>
  \fi
\fi

\ifPDFTeX is provided by the iftex package. You can't check that only one of the packages is loaded, since selinput loads inputenc, so this particular test may be reduced to

\ifPDFTeX
  \@ifpackageloaded{inputenc}{<OK>}{<ERROR>}
\fi

but I wanted to illustrate a more general mechanism.

There's only one scratch conditional, but nothing prevents you from defining your own with

\newif\if@tempswb
\newif\if@tempswc

if needed for more complex checks.

Solution with xifthen

Package xyz is loaded when \ver@xyz.sty is defined. So we can define a new test for xifthen:

\usepackage{xifthen}
\newcommand\isnotloaded[1]{%
  \expandafter\isundefined\csname ver@#1.sty\endcsname}

Then

\ifthenelse{\isnotloaded{inputenc}\AND\isnotloaded{selinput}}
  {<ERROR>}
  {<OK>}

should work (of course this is only an example). Consult the documentation of xifthen for more details.

Solution with etoolbox

\usepackage{etoolbox}
\newcommand\ifisloaded[1]{\ifcsdef{ver@#1.sty}}
\ifboolexpr{ test {\ifisloaded{inputenc}} or test {\ifisloaded{selinput}} }
  {<OK>}
  {<ERROR>}

Note

I believe that the solution with etoolbox is the more robust one. Remember also that fontenc hides itself from the list of loaded packages and it's impossible to test for its loading with this method.

egreg
  • 1,121,712
0

It works now, the output of the package is for this error:

Package templatebugs Warning:
(templatebugs) You have not loaded Package: 'inputenc'
(templatebugs) This Package is recommanded. It is used for:
(templatebugs) Character translation to LaTeX internals. You
(templatebugs) can not use umlaute and other language specific
(templatebugs) special characters without this package.
(templatebugs)
(templatebugs) Alternatively use package selinput.
(templatebugs) on input line 282.

based on this code (stripped)

\SetupIfPackageLoaded{inputenc}

\AtBeginDocument{%
  \ifpdftex{% only if pdftex 
   \IfPackageNotLoaded{inputenc}{%
      \WarnBasicPackageNotLoaded{inputenc}{%
 Character translation to LaTeX internals. You \MessageBreak
 can not use umlaute and other language specific \MessageBreak
 special characters without this package. \MessageBreak
 \MessageBreak
 Alternatively use package selinput. \MessageBreak
      }%
   }%
  }{}
}%

the code in question here was no longer required because selinput loads inputenc anyway. But for further work it is worth to know how to handle such bool expressions.

N.N.
  • 36,163