22

I am on a "semantic" kick recently. For example, while in the past I might have written the direct sum of two vector spaces as $V\oplus W$, I now define \newcommand{\directsum}{\oplus} and write $V\directsum W$. I really like this on an aesthetic level (it makes sense to code things this way) and moreover it's easier to read the code since it says what it means.

What I am running into now is perhaps an issue common to all markup languages. If I want to make semantic code that fully reflects the meaning of what I'm writing, and shunt as much formatting into the preamble as possible, then it seems like I have to decide a very large number of things in advance, and having done so, I would actually lose flexibility (or at any rate make things more complicated to change).

For example, suppose I want to make a command for the function "foo". Let's say some people prefer to use parentheses, and write foo(x), while other people prefer to skip the parentheses, and write foo x.

Here's the problem: if I define in my preamble

\newcommand{\foo}{\mathrm{foo}}

and write \foo(x) all over in my document, then even if someone changes the definition to use \operatorname instead, they will have to go through and remove all of my parentheses. This can't be proper LaTeX; we're supposed to be able to avoid that.

So I decide to instead make a definition that includes an argument:

\newcommand{\foo}[1]{\mathrm(#1)}

This seems much better; now, I'll write \foo{x} in my document, and someone who doesn't want the parentheses just has to remove them once, in my definition. But the issue now is that this greatly restricts my ability to control the formatting of the parentheses.

For example, if I'm in a displayed equation and want to write \foo{\frac{1}{2}}, I'm out of luck; the parentheses will be too small for the fraction. If I try to fix this by including \left and \right in the definition, then conversely sometimes the parentheses will be larger than I want.

It seems to me that the only remaining option is to make my definition of \foo have starred versions / extra options allowing me to control this, e.g.

\newcommand{\foo}[2][]{\mathrm{foo}#1(#2#1)}

or

\makeatletter
\def\foo{\@ifstar\@foo\@@foo}
\def\@foo#1{\mathrm{foo}\left(#1\right)}
\def\@@foo#1{\mathrm{foo}(#1)}
\makeatother

This then seems to defeat the whole goal of being semantic. It's no longer the case that \foo{x} will produce a properly formatted foo(x) no matter what x is; sometimes I'll need to change some other, auxiliary argument of \foo to get the formatting I want.


Okay, sorry for such a long prelude, but this has been an issue that's been gnawing at me for a while and I wanted to show my thoughts so far. My question is:

Should I try to incorporate every way I might want to modify the formatting of \foo into its definition as an optional argument? Or should I leave all of my ad-hoc formatting decisions in the body of the document, future editors be damned?

I realize this is somewhat subjective, but it is a definite issue that I face, and I imagine many others do as well, so I hope this won't be closed. If there are suggestions for how to edit this question to make it more appropriate, they would be most welcome.

  • 4
    If I posted "You cant win." as an answer, would you accept it? (Nice question anyway so +1:-) – David Carlisle Mar 20 '13 at 22:44
  • 1
    Haha maybe :) I'd still be curious how you resolve the issue in practice. In what situations do you decide to lean one way or the other, and why? Any particularly bad stories where you chose to leave things ad-hoc and got burned later? – Zev Chonoles Mar 20 '13 at 22:54
  • 1
    In my case (not answering globally, but your particular example) I've defined \pa{} to make autosize parenthesis (with something related to this). With some optional arguments \pa[auto]{}, autosize, [base], fixed standard size, [big], [Big], [bigg], [Bigg]. With this, I include them in any command I need: \newcommand{\foo}[2][auto]{\operatorname{foo}\pa[#1]{#2}}. And then use it like: \foo{x}, \foo[Big]{x}, … – Manuel Mar 20 '13 at 22:59
14

I don't think it's possible to get purely semantic markup across a wide range of disciplines (or at least not without giving up control of formatting or at least separating it far more radically from the document authoring stage). You see the same dichotomy in XML Markup for mathematics with OpenMath/Content-MathML on one side and Presentation-MathML on the other.

I think for hand written LaTeX for a particular subject area it is possible to define some consistent style of input.One big choice these days is whether to stick with a classic TeX markup approach \alpha^2 or to dabble with Unicode input α² I'll assume the former, but wanted to mention the latter as a possibility.

I wouldn't introduce too many * forms and positional [] arguments as they get hard to remember and don't really improve the readability. One thing that might work is to consistently give your commands a single optional key-value optional argument. That way you can extend the syntax possibilities without changing your existing documents.

so

\foo{x}              foo(x)  % using \mathop not just \mathrm to get prefix spacing
\foo[paren=none]{x}  foo x   % like \sin x
\foo[paren=auto]{x}  foo\left(x\right)
\foo[paren=Big]{x}   foo\Bigl(x\Bigr)
\foo[tizdecoration=big pink circle] {x} % .. whatever it takes:-)

The point is you can think of new keywords and add them to some or all of your commands without breaking your existing documents and semantically you can always ignore the stuff in the optional argument which is a style hook only.

| improve this answer | |
  • ... and this is exactly what SemanTeX is all about. Based on this post, you’ll have to admit that typing math using this kind of keyval syntax is nice. ;-) – Gaussler Nov 23 at 10:50
  • @Gaussler sure but doing it in a way consistent with latex syntax. Also I think this is something of the inverse of semantex, here I am assuming the symbol carries the main semantic information, and the keyval optional argument is controlling more cosmetic styling, whereas you I think have more semantics being expressed via the keyval options, both approaches make sense (and both are explicitly modelled in mathml where you can annotate "presentation mathml" with "content" (semantics) or annotate content mathml with explicit presentation). – David Carlisle Nov 23 at 11:04
  • I don’t completely agree. In SemanTeX, the user is also encouraged to let the command name carry semantic information. The manual advises you to create commands like \set (set constructions), \derivative, \Hom, etc. However, practical experience convinced me that life becomes less cumbersome if you create a fixed collection of variables \va, \vb, \vc etc. so you don’t have to create these variables every time you are using them. But in principle, you can create \setA, \manifoldM, or whatever you want instead. – Gaussler Nov 23 at 11:12
  • And the only thing not consistent with ordinary LaTeX syntax it the g-type argument. But I have been brainstorming about this issue for many years, and I simply don’t see any proper alternative that still makes the syntax easy to use. – Gaussler Nov 23 at 11:13
  • I may add that the idea that all variables should be able to take (optional) functional arguments came out of practical necessity. For a very long time during the development phase, I distinguished between variables and maps, with variables taking no functional argument. But in practise, this just made the system so much harder and more cumbersome to use. Variables tend to become functions much more often than I initially imagined. – Gaussler Nov 23 at 11:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.