Quantcast
Channel: How to balance being semantic, and retaining control of formatting? - TeX - LaTeX Stack Exchange
Viewing all articles
Browse latest Browse all 2

How to balance being semantic, and retaining control of formatting?

$
0
0

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.


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images