WeBWorK Problems

Force all parens to be parentheses

Force all parens to be parentheses

by Alex Jordan -
Number of replies: 4

Now that we can use tikz in a PG problem, it's nice to plot a function using pgfplots ad the \addplot macro. However there are some incompatibilities between how MathObjects prints a formula and what the syntax for an argument to \addplot must be. One of these incompatibilities is that \addplot does not understand brackets as a grouping symbol. So if you have like:

$f = Formula("1/(x(x+1))");

Later if you are in tikz code and use

\addplot ... {$f};

It becomes

\addplot ... {1/[x*(x+1)]};

and the latex image compilation fails.


Is there a way to force brackets (and braces?) to be parentheses in MathObjects string output?

I have hit a few other incompatibilities and I resort to manually re-writing the formula in the tikz code. But most of these time, this issue has been this bracket issue.


In reply to Alex Jordan

Re: Force all parens to be parentheses

by Andrew Swanson -
I use these two lines (this is from a subroutine, so the variable $exp is local):

my $exp = $f->string;
$exp =~ tr /[]/()/; #TikZ does not like square brackets so translate them for parentheses

Then you canĀ 

\addplot ... {$exp};


I had to learn about perl's translate operation just for this...
In reply to Andrew Swanson

Re: Force all parens to be parentheses

by Alex Jordan -

Yes, I did think about something like this that would search-and-replace on the strings. But I'd like to know if there is something more slick like a context flag. So it would be set once (well, once for each context where it's needed) and then work for all the functions I might plot in that problem. I looked at the wiki for a flag like this and didn't find one. I have not yet searched the MathObjects code, but that is my next step if something doesn't come up on this thread about it.

In reply to Alex Jordan

Re: Force all parens to be parentheses

by Alex Jordan -
FWIW, I looked into changing pgf's behavior (as an alternative to changing MathObjects behavior) so that brackets would be interpreted as grouping symbols. In a pgf math expression, brackets are used to indicate an array entry. While pgf math functions are customizable (able to be redefined), I could not find in the documentation anything that would allow me to change the meaning of brackets.
In reply to Alex Jordan

Re: Force all parens to be parentheses

by Glenn Rice -
I doubt that you can redefine brackets in LaTeX. I don't think it is a PGF math limitation, but a LaTeX limitation. Brackets are used to delimit command options in LaTeX. In PGF math expressions, brackets are used to access a particular element of an array, but this is just the usual LaTeX command option notation. Perhaps you could change the definition using catcodes, but that seems like a dangerous thing to do.

You are more likely to have success modifying MathObject behavior than changing bracket behavior in TikZ and PGF.