Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
3  
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
add comment

1 Answer 1

up vote 12 down vote accepted

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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