7

I've recently started doing my homework in LaTeX, so I'm still very new to this world. One of the things I don't very much like doing is typing out ^{-1} everytime I want the inverse of something, so I've taken to replacing this with a pre-defined command \newcommand{\inv}{^{-1}}. So far nothing seems to have broken and everything's displaying fine, but I was curious if this is somehow bad practice in LaTeX. Perhaps this results in some miniscule changes I am simply not aware of?

  • I would personally say that it is bad. ^{-1} is directly readable by others and yourself in the future. It is not worth having a new command to save just a few keystrokes(in my opinion). \inv could collide with other things that comes from packages or what you do yourself elsewhere or in the future. – hpekristiansen Nov 10 '20 at 12:39
  • I am not sure that your question can be answered!? There will be a lot of opinions, but maybe no definitive answer. – hpekristiansen Nov 10 '20 at 12:40
  • 13
    To the contrary! It is good practice, if you have a lot of inverses to type, for instance group theory material, with maybe an average of more than one inverse per line. You could also define some editor shortcut, but that depends on the editor. A possible improvement could be \newcommand{\inv}[1][1]{^{-#1}}, so you can type a\inv but also a\inv[2] for a^{-2}. Anyway, as you see, this is mostly a question of opinion. – egreg Nov 10 '20 at 12:45
  • @egreg groups were precisely why I created the command, got tired of writing out ^{-1} everytime. Your proposed improvement is great, I was actually wondering what I'd have to do in case of exponents higher than 1. Is there way to further improve on the command or is that about it? What about what @Gaussler mentions below, about it interfering with other superscripts, is there a way of circumventing that? – Hilbert Jr. Nov 10 '20 at 13:23
  • 1
    @V.Ch. Further extensions only really make sense for specific purposes that depend on your context. For instance, if you want to be able to take \inv of a' as in my answer, you could use the xparse package to allow this. For instance, you could add an optional star so that a\inv* produces a^{\prime -1}. – Gaussler Nov 10 '20 at 13:46
  • @Gaussler how would I go about using xparse? What's the exact command, if you don't mind? – Hilbert Jr. Nov 10 '20 at 16:57
  • 1
    @V.Ch. See my updated answer. ;-) – Gaussler Nov 10 '20 at 19:52
  • I would have the usage be \inv a, not a\inv. Just so it feels more like a function. – Paŭlo Ebermann Nov 10 '20 at 23:44
  • ^{-1} is the worst idiom. For one simple concept you need five characters, three shifts, three little finger presses, and it induces grouping issues. Anything you can come up with is strictly better if you use it frequently enough. – Symbol 1 Nov 11 '20 at 02:56
  • @Symbol1 Then change your keyboard layout so that you have symbols instead of numbers on top. – Gaussler Nov 11 '20 at 06:22

1 Answers1

10

You run into a problem the moment you want to take e.g. the inverse of a', as a'\inv will yield a double superscript error. This issue annoyed me for many years, and eventually, I created the package SemanTeX to solve this and many other problems. It allows you to type all your math semantically, using keyval syntax, and issues like double superscripts never happen. Here is a code example:

\documentclass{article}

\usepackage{semantex}

\NewVariableClass\MyVar[ output=\MyVar, define keys={ {inv}{ upper=-1 }, }, ]

\NewObject\MyVar\va{a} % this means "variable a"

\begin{document}

$ \va[inv] $, $ \va[prime,inv] $, $ \va[prime,spar,inv] $

\end{document}

enter image description here


Following a request from the comments (not to this answer, but to the original question), I provide a macro \inv taking two optional arguments: an optional *, which adds a prime, and an optional argument, which allows you to raise to a negative power other than -1:

\documentclass{article}

\usepackage{xparse}

\NewDocumentCommand\inv{ s O{1} } {% \IfBooleanTF{#1}% {% ^{\prime-#2}% }% {% ^{-#2}% }% }

\begin{document}

$ a\inv $, $ a\inv[2] $, $ a\inv* $, $ a\inv*[2] $

\end{document}

enter image description here

You can in principle also use a ' instead of a * by replacing { s O{1} } by { t{'} O{1} }. I did not use this approach, as I don’t really find a\inv' to be an intuitive syntax for a'⁻¹ (the inversion and prime are in the opposite order of how they are printed). But that is entirely a matter of personal taste.

Gaussler
  • 12,801
  • 1
    The double superscript error is kind of a dealbreaker, but I have to say that your method looks unnecessarily complicated to me (it could be simply because I'm new to LaTeX and this is actually considered normal). – Hilbert Jr. Nov 10 '20 at 13:26
  • @V.Ch. You have a valid point. My keyval-based approach makes the most sense when writing papers with complicated constructions like \mathcal{C}_{/X}^{\mathrm{op}} or \mathcal{D}^{b,\ge0}(X). Then I find it blessing to be able to instead write something like \catC[over=\vX,op] and \der[bounded,positive degree]{\vX}. But that is entirely a matter of taste, of course. ;-) – Gaussler Nov 10 '20 at 13:32
  • Incidentally, is there a difference between a' and a\prime? I've been using the former this whole time, but I'm noticing people using the latter in this thread. – Hilbert Jr. Nov 10 '20 at 14:21
  • 1
    In fact, a' is equivalent to a^{\prime}. Knuth made it this way precisely so that you can type a^{\prime 2} to get a'². You should never write a\prime, however, as this will put a giant prime to the right of your a. – Gaussler Nov 10 '20 at 14:23
  • @Gaussler thanks for the updated answer! Is there a difference between DeclareDocumentCommand and NewDocumentCommand in xparse? – Hilbert Jr. Nov 10 '20 at 21:54
  • @Gaussler I'm trying to get the same thing working with NewDocumentCommand, but it's not working so far \NewDocumentCommand{\inv}{s 0{#1}}{\IfValueTF{#1}{^{\prime-#2}}{^{-#2}}}. – Hilbert Jr. Nov 10 '20 at 22:00
  • 1
    @V.Ch. The Declare variant overrides any previous definitions. I guess I should have used the New variant. Updating the answer with this. – Gaussler Nov 11 '20 at 06:24