[Tex/LaTex] Best way to avoid repeating expressions

best practices

I'm writing a text where I repeatedly speak of particular expressions which compose more complex expressions which I also repeatedly speak of (more specifically I'm writing a text with logical and set theoretic expressions). If I put my simple expressions in definitions and build up the more complex expressions from these definitions I benefit in the following ways:

  1. If I decide to change a particular expression it's easy to change it and it reflects throughout my text.
  2. It's easier to avoid mistakes of not repeating the exact expression. That is, it's easier to be consistent.
  3. The source might be easier to read and write.

Here's an example:

\documentclass{article}

\def\firstcond{A \land B}
\def\secondcond{C \lor D}
\def\thirdcond{E \land F}
\def\firstimp{\firstcond \to \secondcond}
\def\secondimp{\thirdcond \to P(\secondcond )}

\begin{document}

\begin{equation}
(\firstimp ) \leftrightarrow ( \secondimp )
\end{equation}
\(\lnot (\secondimp )\) instead of \(\secondimp\), thus
\begin{equation}
(\firstimp ) \leftrightarrow \lnot( \secondimp )
\end{equation}

\end{document}

Say that I decide that \firstcond is a bad expression and that it should read some other way. Then I can just change the definition of \firstcond and it reflects throughout my document (I know that this can be done by an editor's search and replace but that doesn't help with point 2 and 3).

What I prefer with this solution is that \def can be put after the preamble so that it can figure right before it's used so that its contents is near its first use in text. This makes it easier when writing because I don't have to scroll back and forth or switch between files to see the contents.

Now, my question. Is this the best way to achieve this kind of writing/way of working? Is there any way to improve on it? Note that I'm not looking for ways to make cross-references because in this case I don't want to burden the reader with the overhead of connecting a label to an expression.

Best Answer

Is this the best way to achieve this kind of writing/way of working?

Yes.

I recently did a fairly similar thing in this paper. My coauthor and I were convinced that whatever notation we chose, we would get a comment from the referee along the lines of "The notation for X is awful. I suggest using Y." So I designed the paper to make it as easy as possible to change all the notation. At one point I even had it set up so that a single change would flip all actions from left to right (so that, for example, f followed by g would be g f instead of f g). So I have every sympathy for your situation and think that the general scheme is the right way to go.

Is there any way to improve on it?

Yes.

Make your macros more obvious what they are. (This may already be true for you, in which case ignore this comment!). It's been said that you should write so that when you look again at the code in 6 months' time, you don't have to puzzle out what everything means. In my example above, I had macros of the form \dcat, \dobj, \delt for "The category D", "An object of D", and "An element of an object of D". It's not hard to remember what they mean, whilst still retaining the flexibility of changing how they are typeset. So my first piece of advice:

  1. Pick a good naming scheme for your macros.

The next is about saving time and energy. In my case, I found myself defining reams of these things, and all the same: I'd have a category and want to define objects, morphisms, elements, and variants thereof. All on the same basic pattern. So I wrote a "generator" macro that would take in a couple of basic parameters and then generate all the macros that I wanted to use (a crude sort of object-oriented programming). Of course, exactly how feasible that is for you depends on your abilities with TeX, but even if you feel it is beyond you right now, it is worth planning ahead a little and anticipating that you might put in place such a scheme and so choosing your names appropriately now. So my second piece of advice:

  1. Be lazy. Build layers in to your system so that it is easy to change the style of how these things are typeset as well as how individual terms appear.
Related Question