WeBWorK Main Forum

Why negative space in LaTeX

Why negative space in LaTeX

by Robert Mařík -
Number of replies: 4

Hello. I was surprised that WeBWorK renders expression like 6*(x-1) with negative space \! between 6 and the left parenthesis. Try the following example and check LaTeX code in Mathjax. What is the purpose of this behavior? Can be switched off? Thank you

BTW: I continue to translate the student part of WeBWorK into Czech. Will post in a couple of days to the developers.

Robert


DOCUMENT(); 
loadMacros(
"PGstandard.pl",
"PGML.pl",
"MathObjects.pl",
"PGcourse.pl",
);
TEXT(beginproblem());
$showPartialCorrectAnswers = 1;

$f=Formula("6*(x^3-1)")->reduce();   # funkce

BEGIN_PGML
Enter [``` [$f] ```][_____________________]{$f}
END_PGML

ENDDOCUMENT();
In reply to Robert Mařík

Re: Why negative space in LaTeX

by Alex Jordan -

>What is the purpose of this behavior?

I suspect it is there because with something like

x\left(\frac{x}{2}+1\right)

the spacing is excessive. This is a thing that happens with \left and \right, and even with 6(x-1), MathObjects will print using \left and \right.

If that is correct, maybe the badness from using `\!` outweighs the good that is achieved from cutting that excessive space.

You can see `\!` being introduced in pg/lib/Parser/BOP/multiply.pm, and in a different way in vim lib/Parser/Function.pm. I just glanced at these (after grepping for `\!`) and maybe the former is the place to cut it, if that is overall the right thing to do.

In reply to Robert Mařík

Re: Why negative space in LaTeX

by Davide Cervone -

Alex is right, the issue is one of spacing. In TeX, each item in the expression is assigned a "class" (like BIN for binary operator, REL for relation, OPEN for an open delimiter, ORD for an ordinary item like a variable, etc). The spacing between adjacent items is determined by their two classes. For something like \sin(x), the \sin is class OP (operator) and ( is class OPEN, and there is no extra space between those. But MathObejcts always uses \left(...\right) for parentheses, since its doesn't know how tall the contents of the parentheses will be, and a \left(...\right) group has class INNER. An OP followed by an INNER has a bit of space between them, and that means \sin\left(x\right) will be badly spaced (as Alex points out). MathObjects uses \! to remove that space, but there are situations when that doesn't work as well as it should, as you found out.

I have given it some thought, and perhaps a better idea would be to use \mathopen{\left(...\right)} around the parenthesis group, and \mathclose{} following it so that the parentheses appear to work as open and close items. This will get the correct spacing for \sin(x) as well as 6(x^3-1). This could be done in both locations that identified by Alex.

In reply to Davide Cervone

Re: Why negative space in LaTeX

by Alex Jordan -

> I have given it some thought, and perhaps a better idea would be to use \mathopen{\left(...\right)} around the parenthesis group, and \mathclose{} following it so that the parentheses appear to work as open and close items. This will get the correct spacing for \sin(x) as well as 6(x^3-1). This could be done in both locations that identified by Alex.

I use a variant of that approach a lot, usually buried in macros, where the \mathopen{} is empty at the beginning. I have found one bad side effect worth noting (and you will know better if that matters for MathObjects). If you have something like:

\ln\mathopen{}\left(\frac{x}{2}\right)\mathclose{}^2

then the exponent positioning at the end is bad. It's worse than the spacing issues at the front. In your version, the exponent spacing is better, but the exponent vertical positioning is not great:

\ln\mathopen{\left(\frac{x}{2}\right)}\mathclose{}^2

At least in this example, the following looks good without the \mathclose{}:

\ln\mathopen{}\left(\frac{x}{2}\right)^2

Meanwhile this version brings back extra space at the front:

\ln\mathopen{\left(\frac{x}{2}\right)}^2


Even with \ln\mathopen{}\left(\frac{x}{2}\right)^2, it's still hard to feel secure that there is not some unforeseen bad side effect.

On rare occasions, I have seen the \! causing characters to actually overlap a little. Maybe it is font dependent. That seems worse than having excess space.


In reply to Alex Jordan

Re: Why negative space in LaTeX

by Davide Cervone -

> If you have something like: > > \ln\mathopen{}\left(\frac{x}{2}\right)\mathclose{}^2 > > then the exponent positioning at the end is bad.

You are correct, that is a bad side-effect. I think the solution is to do

\ln\mathopen{}\mathclose{\left(\frac{x}{2}\right)}^2

so that the CLOSE applies to the whole parenthetic group (instead of the OPEN, as in my example). That will allow the position of the square to be correct, and will get the correct spacing before it.