8

I have a .sty file that may be loaded using the command \usepackage (in LaTeX) or the command \input (in LaTeX or Plain TeX).

Is there any way that within the .sty file to check which command was used to load the file?

  • 4
    \usepackage will define the csname ver@nameofyoursty.sty. You could test for it. – Ulrike Fischer Nov 17 '18 at 16:22
  • 1
    You can also check the used format (provided your package should behave the same in LaTeX independent on the used method to load it). – Skillmon Nov 17 '18 at 16:56
  • 1
    You might take a look at \@ifpackageloaded. Presumably it is set by \usepackage, not \input. – John Kormylo Nov 17 '18 at 18:32
  • Thanks all, your explanations really solved my problem. – Tisha Shusho Nov 18 '18 at 7:19
8

As TeX is a macro expansion language, you cannot know what the 'context' of a macro is: there simply isn't one. Also, \input (or \@@input) can be used within LaTeX. What you can do is check for the format or some marker macro. For example, in expl3-generic.sty we check \fmtname

\def\tempa{LaTeX2e}
\ifx\fmtname\tempa
  % LaTeX
\else
  % Not LaTeX
\fi

A common approach is to check for the existence of something like \documentclass

\ifx\documentclass\undefined
  % Not LaTeX
\else
  % LaTeX
\fi

although this will also be true when things like miniltx are loaded. So it depends exactly what you need to know. Some packages are fine with miniltx, others perhaps not.

  • I see how this would distinguish plain TeX from LaTeX. But I think OP was asking about distinguishing \usepackage from \input, both within LaTeX. – Teepeemm Jan 22 at 23:32
  • @Teepeemm Which I hope was addressed by my first line: you can't know (you can check for e.g. \ver@<filename> but of course someone might make that manually and still use \input. – Joseph Wright Jan 23 at 7:36
  • But if someone goes to the trouble of \makeatletter/other to manually define \ver@<filename>, they know what they're doing and deserve whatever happens. The comments on the original post also hint at two other ways to check. Could we spell out exactly how to do so? If necessary, we could also spell out how those methods could be foiled. – Teepeemm Jan 23 at 15:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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