72

The physics package is useful

A lot of my LaTeX documents make heavy use of the physics package. Aside from it overloading several standard commands (e.g. \sin, \abs, etc.), it also makes a few abbreviations very convenient (e.g. \order). It also makes typesetting vector calculus, ordinary/partial/variational derivatives, linear algebra (bra-ket notation and matrices), and other areas much less painful. I think it's safe to say it fills a gap in the market.

The physics package is unpopular

Despite the advantages I've listed from using the package, there are some short comings. It makes use of \xparse which can give several spacing issues (these are usually edge cases but aren't too uncommon), and the syntax can be counter-intuitive. Because of this, whenever I (or others) post a problem that involves the package, a frequent theme is to give physics a wide berth:

(I am sure there are likely more unfavourable reviews of the package out there).

What alternatives are there?

I am curious to know what people think good alternatives are? Off the top of my head:

  • Keep on using physics and hope the macros are improved (unlikely?).
  • Try to re-write the few macros I use most often, but better (I doubt my implementation would be great).
  • There is another equivalent package already which addresses these issues which I haven't found yet.
  • Type it all out in full and abandon all hope of convenient math macros.

While the last option is a bit melodramatic, I think the overarching question of: "Is it preferable to use a supported package which is not ideal/buggy, or should I try and re-invent the wheel?" is one I encounter a fair bit when considering packages. My current ethos is to always use a package/module, and never re-invent the wheel. What would more proficient/experienced users recommend?

oliversm
  • 2,717
  • 16
    You have a very good point. I for myself have decided to write a few macros on my own, and not to use the physics package. Yet I would love to see a package that really does what physics promises to do. –  Jan 23 '19 at 17:12
  • 5
    I've never looked at the code or used it, but the documentation left me feeling this was more "a collection of stuff that seemed like a good idea when we wrote it" rather than a coherent solution to a well defined problem. When I have produced "physics-like" documents, I've just defined a few simple macros to save repetition, and never really felt the need for more than that. – alephzero Jan 23 '19 at 17:26
  • 3
    For everybody interested in a (hopefully eventually) viable alternative to physics: You are welcome to contribute to physics replacement effort. Also it was brought to my attention that the diffcoeff package might fill the gap partly. – Skillmon Jan 24 '19 at 08:33
  • 3
    Additionally to the diffcoeff package, there is also the braket package, which provides macros for Dirac bra-ket notation. – Skillmon Jan 24 '19 at 18:05
  • 4
    In addition, for (partial or univariate) derivatives, you have the long-established package esdiff and the recent derivative package. For the Dirac braket, inner products, norms, &c., you can easily make your own macros with powerful commands DeclarePairedDelimiter,\DeclarePairedDelimiterX and DeclarePairedDelimiterXPP commands in mathtools. I used them to help a friend of mine typing an introductory book on Quantum Mechanics. – Bernard Sep 02 '19 at 22:45
  • 2
    One more recent-ish reason to look for a replacement for physics is that it conflicts with version 3 of siunitx which now uses \qty instead of the previous \SI. And I cannot see myself live without siunitx. Beware if you rely on both packages, do not update siunitx. – Zork Aug 03 '21 at 19:34
  • I doubt the spurious spaces are due to xparse (which is built in to the latex format and doesn't need to be loaded these days) the package source clearly adds loads of spurious white space within its definitions. picking one at random \expectationvalue has 16 !! space tokens in its definition. – David Carlisle Nov 06 '21 at 15:33
  • 1
    Haven't used it but there's the new physics2 package https://ctan.org/pkg/physics2?lang=en – Apoorv Potnis Mar 16 '23 at 05:40

2 Answers2

10

There is now the package physics2 which takes a "modular" approach (command sets are loaded via \usephysicsmodule{<module>}). This allows it to support multiple (but sometimes mutually incompatible) kinds of syntax for, say, braket notation with the ab.braket and braket modules. It also minimizes conflict with other packages or personal macros, which is a common issue with physics.

It even has documentation specifically for those converting from the physics package that explains some of the latter's deficiencies and how this package improves upon it.

mbert
  • 4,171
5

This most likely won't help you and I even don't know if it's 'a good' solution, but I just copied the code I need from physics as a first entry point (mostly \bra, \ket for quantum physics) and then looked at it. The problem(s) with physics are the white spaces caused by \vphantom that is in the code for no reasons whatsoever. Take \ket as example:

\DeclareDocumentCommand\ket{ s m }
{ % Ket
    \IfBooleanTF{#1}
    {\vphantom{#2}\left\lvert\smash{#2}\right\rangle} % No resize
    {\left\lvert{#2}\right\rangle} % Auto sizing
}

just delete that \vphantom and it works perfectly. Same with other definitions of this package.

Edit (9.10.2022): I actually use nowadays (better solution with mathtools package, which has all the things like auto-sizing etc. automatically when one uses the starred version in the document or even optional arguments for a specific size of the brackets):

\DeclarePairedDelimiter{\bra}{\langle}{\rvert}%
\DeclarePairedDelimiter{\ket}{\lvert}{\rangle}%
\DeclarePairedDelimiterX\innerp[2]{\langle}{\rangle}{#1\delimsize\vert\mathopen{}#2}%
\DeclarePairedDelimiterX\braket[2]{\langle}{\rangle}{#1\delimsize\vert\mathopen{}#2}%
\DeclarePairedDelimiterX\braketOP[3]{\langle}{\rangle}{#1\,\delimsize\vert\,\mathopen{}#2\,\delimsize\vert\,\mathopen{}#3}%
\DeclarePairedDelimiterX\ketbra[2]{\lvert}{\rvert}{#1\delimsize\rangle\!\delimsize\langle#2}%
\DeclarePairedDelimiterX\outerp[2]{\lvert}{\rvert}{#1\delimsize\rangle\!\delimsize\langle#2}%
\DeclarePairedDelimiterX\projector[1]{\lvert}{\rvert}{#1\delimsize\rangle\!\delimsize\langle#1}%
  • 1
    It might even be worth you asking a new question and then answering it yourself with this information and the other changes you have made, could be really useful to future people curious about the changes you have made :) – JamesT Mar 15 '23 at 22:32