15

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.

N.N.
  • 36,163
  • 11
    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 http://tex.stackexchange.com/q/655/5701 – N.N. Jun 22 '11 at 19:31

2 Answers2

17

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.
Andrew Stacey
  • 153,724
  • 43
  • 389
  • 751
  • 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 07:55
  • 1
    @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. – Andrew Stacey Jun 23 '11 at 08:04
10
  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...

  • 1
    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) – Andrew Stacey Jun 23 '11 at 07:56
  • Great answer. About publishers not accepting heavily hacked TeX code. Would a text written in the manner of my example generally be considered to heavily hacked? – N.N. Jun 23 '11 at 08:00
  • (ctd) Secondly, whilst \delt is a bit too close to \delta for comfort, in this particular document \delta is not used at all (I just checked) and so mangling the syntax to avoid one near clash is worse than having the near clash (remember that I also have \dVelt, \dVcVelt and so forth; in fact, I have 203 of these things defined, up to \KGvCGvTtcat). Lastly, if you use enough hacked code then the publisher will assume that you know more TeX than they do and let your article pass unchanged (which is far better than letting them change it in any way!). – Andrew Stacey Jun 23 '11 at 08:01
  • 1
    @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. – Andrew Stacey Jun 23 '11 at 08:02
  • @Andrew Stacey So it's common that heavy hacks in TeX or good code is let through because the publisher assumes proficiency? Maybe it's worth obfuscating every text to get it past every nitpicking publisher! – N.N. Jun 23 '11 at 08:04
  • @N.N.: I try to be "nice" to the publisher and use their style/class file, but then I also want to be "nice" to myself and not have to mangle my document to meet their (inane) requirements. I think that I have quite a reasonable set up now, but this isn't the place to describe it - ask me in chat if you want to know more. – Andrew Stacey Jun 23 '11 at 08:07
  • @Andrew Stacey: I'm not trying to dispute your way, I'm just making a comparison. And I'm not a "natural" English speaker. So, read "mnemonic" as my definition of it in the context. I mean something different, a more narrow definition. I fond (although not being cognitive scientist), that keeping "the written order" is better mnemonic than braking it. And you are right for the important part. I will add another rule of mine I omitted. \delt is in a mnemonic clash with \delta no matter used or not. – Karl Karlsson Jun 23 '11 at 10:08
  • 1
    @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
  • @Karl: And I'm not trying to dispute yours! Regarding "mnemonic", I would prefer it if you said: "I find keeping macro names in the right order a better mnemonic than breaking the order, so for example ..." since then you aren't redefining the term "mnemonic" but are explaining that you find certain methods easier than others (which is perfectly valid). Redefining words is dangerous, especially for non-native speakers! (ctd) – Andrew Stacey Jun 23 '11 at 10:25
  • (ctd) I also agree with \delt and \delta, except that if \delta is not used at all and \delt, \belt, \celt, dVcVelt are used then the impact of the clash is minimized. Part of the point of defining these macros is to save time and energy. I could write out 'The category of V-algebra objects in D' every time, but it is so much easier to write \dVcat. If I had to write \catofValgobjsinD then the saving would be significantly smaller. (ctd again!) – Andrew Stacey Jun 23 '11 at 10:27
  • 1
    (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. – Andrew Stacey Jun 23 '11 at 10:29
  • @N.N.: There are publishers using XPP (XML Professional Publisher), AAPP (Arbortext Advanced Print Publisher) and tools like that. They understand some TeX, but are not TeX. You can't hack them. Heavily depend on where in the world is the case, and in what particular field the publication is. To the extreme case, where TeX isn't accepted at all and should be converted to something else. – Karl Karlsson Jun 23 '11 at 10:29
  • 1
    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) – Andrew Stacey Jun 23 '11 at 10:32
  • if the publisher wants to downgrade my article for some silly reason, that's their look-out. No one is going to read that version anyway! – Andrew Stacey Jun 23 '11 at 10:33
  • @Andrew Stacey: My conclusion is - you are quite right for everything you say. And probably your view goes better in the fields of theoretical and other math, but mine goes better in the more experimental natural sciences. Different fields create different mind sets. :) – Karl Karlsson Jun 23 '11 at 10:52