# Best way to avoid repeating expressions

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}

$$(\firstimp ) \leftrightarrow ( \secondimp )$$
$$\lnot (\secondimp )$$ instead of $$\secondimp$$, thus
$$(\firstimp ) \leftrightarrow \lnot( \secondimp )$$

\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.

-
Assuming you are not stuck on plain TeX, I would use \newcommand instead of \def as that will automatically check that you don't overwrite previous definitions. – Peter Grill Jun 22 '11 at 18:57
@Peter Grill Thanks. Your comment lead me to tex.stackexchange.com/q/655/5701 – N.N. Jun 22 '11 at 19:31

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.
-
Great answer. Yes, such TeX abilities are beyond me. Would you suggest any particular place to go to learn of creating such "generator" macros? – N.N. Jun 23 '11 at 7:55
@N.N.: Yes, take a look at the Object Oriented stuff in the PGF manual. If I'd known about that when I wrote my original stuff, I would have used it instead. – Loop Space Jun 23 '11 at 8:04
1. Iterative definitions are natural in mathematics.
2. Mathematics is first TeX is second.
3. Try to construct objective criteria for working with TeX commands.
4. Avoid heavy hacks.

Some explanations:

1. Even the scientific knowledge evolves in iterative way. Is this the best? Who knows - thats what we have now.
2. This has two sides. First, mathematics is the subject, and TeX is just a tool for presenting it. And second, math notation is superior to the TeX's notation, no matter how you improve your TeX notation.
3. My opinion goes here. ---> 3)
4. Publishers generally don't accept heavily hacked TeX code and will require rewriting it.

3)

1. Mnemonic names for TeX commands. Even not simply mnemonic. I find keeping macro names in the written order a better mnemonic than breaking the order. This means the same way acronyms are constructed - something like mnemonic acronyms. (In Andrew Stacey's example \dcat for "The category D" is mnemonic but isn't in the written order. Say \catofd or \catd are some mnemonic examples in written order.)
2. Distant TeX commands must have distant enough names. (In Andrew Stacey's example \delt for "An element of an object of D" is too close to \delta. Say \elofobjofd or \elobjd are some mnemonic distant examples.)
3. Write short definitions in the way they read, not on what they mean. Write complex definitions in what they mean, but as "mnemonic acronyms" the way you pronounce them.
4. Comment every definition, or every group of similar definitions.
5. Do all definitions in the preamble. There are two reasons. First, thats "natural" in math. Many books have Notation list, or whatever is called, where the notation is defined in one place. Also, to make the notation more consistent you must see it all together. And second, there is some speed up if the preamble is precompiled - Precompiling header files; Precompiled preamble with TikZ/Externalize. That's in the writing process, in the end it's better to attach the preamble back to it's place.

I don't have to scroll back and forth or switch between files to see the contents.

Why will you need that? It's possible to open two files side by side. Or even one file in split view. For example TeXnicCenter has split view - two places in the same file. And Stefan Didak's Home Office shows definitely an extreme example. :)

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).

I don't know a better way that isn't heavy hackery...

-
I'm almost in full agreement with this, but have a couple of nitpicks (I hope you don't mind!). "Mnemonic" just means "Aiding or designed to aid the memory" (from the OED) so \dcat and \catd are both designed with the same principle. I chose \dcat over \catd because I have \dcat, \dVcat, \dVcVcat and even longer. The important part is the initial part and so that should come first. If I write \catdVcV then it isn't clear where the cat ends and the dVcV starts. (ctd) – Loop Space Jun 23 '11 at 7:56
@N.N.: I've submitted extremely hacked code to journals and whilst I've had to put it all in one file on occasion (rather than using style files), I've not yet been told that I have to take out the hacks (they've grumbled, but that's different). I've even surreptitiously edited their class file definitions and gotten away with it. – Loop Space Jun 23 '11 at 8:02
@Andrew Stacey: About the TeX hacks. I will tell a joke here - "And what's next? To go to the printer, learn mixing inks and operating the print press? Doing it better than them?". Thats their job, mine is too different. More than 90% of the TeX users were not, are not and don't want to be TeX hackers. And thats the way it should be. My advices are always general, not for a special group or something like that. And there are publishers using systems that understand some TeX, but are not TeX. You can't hack them... – Karl Karlsson Jun 23 '11 at 10:24
(ctd) Lastly(!) \dobj in particular is often the correct order as one can write D-object or object in D. The key point (for me) is that doing this should make the author's life easier. But a corollary of this is that this shouldn't be at the expense of making it more difficult in 6 months' time. – Loop Space Jun 23 '11 at 10:29
Okay, not last. I see your comment on hacks. Again, my overriding principle is that the journal should not interfere with me getting the article written. If it takes a load of hacks to do that, then so be it! I don't recommend an author becoming a TeX hacker to get their articles written, but I find that I can't avoid it and that it makes my life much easier if I can use a reasonable amount of the power of TeX to write them. Ultimately, though, it's more that I don't actually care what the publisher does. I'll put "the official" copy on my website and on the arxiv (ctd) – Loop Space Jun 23 '11 at 10:32