Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
    
In fact, ifthen package, etoolbox package, and expl3 provides calculation of boolean values. –  Leo Liu Sep 22 '11 at 7:43
    
possible duplicate of LaTeX conditional expression –  Leo Liu Sep 22 '11 at 7:45
    
@LeoLiu Not completely: this is a more specific question involving also LaTeX internals. –  egreg Sep 22 '11 at 11:03
    
I now switched to a solution using \ltx@ifpackageloaded from ltxcmds. I can not use \@ifpackageloaded because that is undefined in the document. –  Matthias Pospiech Sep 22 '11 at 13:04
add comment

2 Answers

up vote 7 down vote accepted

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.

share|improve this answer
    
Your answer is an interesting alternative. Nevertheless I would like to know how to evaluate booleans of this type \not\boolean{varA} \and \not\boolean{varB} –  Matthias Pospiech Sep 22 '11 at 5:39
    
@MatthiasPospiech Consider two roads: xifthen and LaTeX3. –  egreg Sep 22 '11 at 7:47
    
@MatthiasPospiech I've added two examples. –  egreg Sep 22 '11 at 10:38
    
Very illuminating explanation of how to handle certain nested conditional expressions! –  Mico Sep 22 '11 at 15:24
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.