1

I am defining my own latex package mypackage.sty. How to define a local command such that: mycommand

  • If mycommand is already defined elsewhere it will be locally redefined just inside the package
  • If mycommand is not already defined, it will be locally defined just inside the package, and unavailable from outside of the package

Same question with lengths that would be locally defined using \newlength and \setlength.

  • 1
    What exactly do you mean with inside and outside the package? The usual solution would be to use internal names like \vincentspackage@mycommand. But those would usually also be available anywhere else unless you put your package code into a group (which is usually not done). – moewe Apr 17 at 13:27
  • 1
    there is no local scope related to the package loading, just prefix your names with some internal prefix, allocations such as \newlength have to be global. – David Carlisle Apr 17 at 13:30
  • This is indeed a problem, and the source of many package conflicts that mutually (re)define commands. There is not really a good solution for this as far as I know, just be careful with your command names and hope for the best :) – Marijn Apr 17 at 13:33
  • It can cause real mayhem when two loaded packages try to use the same macro name for different things. Avoid it like the plague. Take @moewe's advice...choose macro names so unique that no other package would use that name. That and use @ as part of the name helps, too. – Steven B. Segletes Apr 17 at 13:34
4

You are probably thinking of a compiled or interpreted language where a function definition provides a local scope that hides the implementation from calling code. That idiom does not apply to a macro expansion language like TeX.

If your package defines a user command \foo in terms of some helper commands that are only relevant to internal package code, say

\newcommand\foo{zzz \internalfoohelper zzz}

Then when \foo is used in a document it just expands to zzz \internalfoohelper zzz so \internalfoohelper needs to be defined at that point you can not just define it locally in the package code.

The usual solution is to pick some unique prefix, so all internal commands for longtable start \LT@... for example.

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.