1

I wish to create a "library" of commands that I often use, say lib.tex, so that every time I wish to use them, I write \input{lib} and get on with life. The problem, however, arises when my library relies on third-party packages, like \usepackage{expl3}, \usepackage{tabto} and so on. I'm paranoid that, at some point, if I include my library, the document won't compile due to collision of these \usepackage{} statements, e.g. \input{lib}\usepackage{X}, where X is already included inside lib.tex. In C, we have header guards, is there anything LaTeX could offer to mimic this behaviour or make up for it with certain internal mechanisms? Is there anything I could do, so I could care-free \usepackage{} in lib.tex?

  • 6
    Using \usepackage{X} more than once is not a problem: latex will load it only once. Package options can clash, or packages and commands can clash with other packages, but what to do here will depend on the individual case. – Ulrike Fischer Jul 10 '20 at 06:55
  • 1
    Of course when an error does occur for any reason (such as conflicting package options or incorrect loading order) then you will be notified and you can fix it. This will probably not happen very often so you can use \usepackage relatively care-free. The fact that LaTeX allows multiple \usepackage statements for the same package without giving a warning is actually a feature, see https://tex.stackexchange.com/questions/148250/using-usepackage-twice-with-the-same-package-name. – Marijn Jul 10 '20 at 08:44
  • @UlrikeFischer Thank you! I suspected that was the case. To complement the original question, what would you recommend doing to minimize / eliminate the collusion of definitions, i.e. \def and \newcommand[n]{...} statements? – Piotr Tarasov Jul 10 '20 at 08:55
  • Do you know what will happen if you do \def\box{abc}? And why? If not, don't use \def. – Ulrike Fischer Jul 10 '20 at 08:58
  • @UlrikeFischer I read about it in TeXBook a while back, I like it being primitive and all. [at]Fran below gave a sufficient answer to the command collusion follow-up. – Piotr Tarasov Jul 10 '20 at 11:42
  • well if you like primitive commands then why do you load expl3 or tabto and other packages that make life easier? Be consequent and write everything with primitives (btw: \@ifundefined is not a primitive command either). – Ulrike Fischer Jul 10 '20 at 11:50
  • @UlrikeFischer because I'm a hypocrite? I do favor primitive over composite, and use primitives where I can, it gives me a sense of power & purity. – Piotr Tarasov Jul 10 '20 at 13:13
  • it is not purity if you mix latex commands with tex primitives without any need. And it is not power to use commands you barely understand. – Ulrike Fischer Jul 10 '20 at 13:56
  • @UlrikeFischer for me, mixing is closer to purity (because the percentage of primitive commands used is non-zero), and, I understand \def sufficiently to use it (enable its power), which my paper so far demonstrates. – Piotr Tarasov Jul 13 '20 at 04:59

1 Answers1

1

There is \@ifpackageloaded

\documentclass{article}
% \usepackage{color}
\makeatletter
\@ifpackageloaded{color}{\AtBeginDocument{%
{\color{red}Package color already used}\end{document}}}%
{\usepackage{color}}
\makeatother
\begin{document}
Some \color{blue} blue color 
\end{document}

You can also test the options of the package with \@ifpackagewithand the date of the package with \@ifpackagelater. See Test if a package (or package option) is loaded.

Edit:

To avoid commands collisions, you can use \@ifundefined:

\documentclass{article}
\makeatletter
\def\foo{default foo} 
\@ifundefined{foo}{\def\foo{my foo}}{\relax}
\makeatother 
\begin{document}
\foo
\end{document}

But as stated in the comments, \def is a risky command. It is better to use \newcommand or \renewcommand to define or redefine commands.

Fran
  • 80,769
  • Thank you for taking your time to answer!

    If time permits, I'd appreciate if you could address the following complementary issue: how should one (would you recommend to) deal with the collusion of \def{} and \newcommand[n]{...} statements? As the library grows, the \input{} tree becomes more complex, and the risk of defining something multiple times becomes relevant.

    – Piotr Tarasov Jul 10 '20 at 08:52
  • 1
    @PiotrTarasov @ifundefined (see edit answer) but better use \newcommand, and \renewcommand that \def, than can overwrite any command without warning you. – Fran Jul 10 '20 at 09:40
  • Excellent! Thanks again! – Piotr Tarasov Jul 10 '20 at 11:34