WeBWorK Problems

Newbie - help with PGML

Newbie - help with PGML

by Paul Hermans -
Number of replies: 9
Hi, I am trying to write my first problem, and have it working, but it is not very efficient code. I have two issues I could use help with.

1) I wrote a few functions to give me the LaTex formatted string for the prime factorization of a number. I based it on an example I found via google. It works great, but I will have several other problems that will need to use it, so I wanted to put the functions in a separate .pl file and include it. I tried this, by putting a my_utils.pl file in the same folder as my problem set, and then copied pasted the code into that file. I then added my_utils.pl to the LoadMacros() call in the actual problem's .pl file. Suddenly the function fails. I am new to Pearl so assuming I just did it wrong. Can somebody give me an example of how to do this?


2) I am finding myself making a lot of variables just so I can display them in the solution. Here's a silly example:

$num = 24;
$num2 = 36;

$lcd = lcd($num, $num2);
$lcdPretty = prettyFactor($lcd); # this is the function that returns prime factored version of an integer.
...
# SOLUTION
The prime factorization of the [`` \begin{aligned}
= {[$lcd]} \end{aligned} ``] is [`` \begin{aligned}
= {[$lcdPretty]} \end{aligned} ``]
...

Is there a way I can avoid making a second variable just to display it, and instead simply display the result of a function call?

something like this:
The prime factorization of the [`` \begin{aligned}
= {[$lcd]} \end{aligned} ``] is [`` \begin{aligned}
= {prettyFactor($lcdPretty)} \end{aligned} ``]

Thanks for any guidance you can provide!

Paul


In reply to Paul Hermans

Re: Newbie - help with PGML

by Alex Jordan -
Hello Paul,

For 1) I put local macro libraries in:
templates/local/macros/
but I believe it is supposed to be OK to put them adjacent to a problem using them.

Could you reply with the .pl file and a full problem file that is using it? If this forum only allows you to post one file, you could paste the other file's contents into a message.

For 2) several things look to me to be overkill. Using [`` instead of just [` means you are making display mode math instead of inline math. Also, the \begin{aligned}...\end{aligned} is for multi-lined equations to get them aligned at = signs. Based on what you wrote, I would think this is good:

The prime factorization of the [`[$lcd]`] is [`[@prettyFactor($lcd)@]`].

Where [@...@] is where you can execute some arbitrary perl code. You might even get away with:

The prime factorization of the [`[$lcd]`] is [`[prettyFactor($lcd)]`].

But I'm not sure about that. Also, depending on what prettyFactor() outputs, you might need:

The prime factorization of the [`[$lcd]`] is [`[@prettyFactor($lcd)@]*`].

(where there is an asterisk after [@...@].)

In reply to Alex Jordan

Re: Newbie - help with PGML

by Paul Hermans -
Wow,
A lot to digest in there. I will "play" with your suggestions on simplifying the output. I have to say that after looking over tutorials, there seems to be about a million ways to do it, some new, some old, very little explanation or indication as to what the "best practice" is.....so I appreciate the help.

Once I have that fixed up, I will attach my file with all the code so I can get advice on better structuring it and possibly moving code to a separate file.

Thanks again.

Paul
In reply to Alex Jordan

Re: Newbie - help with PGML

by Paul Hermans -
The code (sloppy but working) is attached.
If I move the three functions
Factor
Exponents
prettyFactor

into a separate file, suddenly prettyFactor returns all blanks.

No idea why.
In reply to Paul Hermans

Re: Newbie - help with PGML

by Michael Gage -
Hi Paul,

The .pl files are pure perl. The .pg files receive some preprocessing. This means that when you move things from .pg files to .pl files some things have to change.

In particular
my @factors = factor($x); # array of prime factors
 my @exp = exponents(~~@factors); # array of exponents
 my $length = scalar(@exp); # length of the returned array of exponents
 my @tmp = (); # array of Latex formatted string for each unique factor.
You will need to change ~~@factors back to the native perl \@factors.
Likewise if you had any TeX strings all TeX backslashes would have to be replaced by double backslashes.
for more details see here:


and


also


In reply to Michael Gage

Re: Newbie - help with PGML

by Paul Hermans -
Thanks Michael,
This does make sense....it took me a while to realize that .pg files are not being handled the same as .pl files.

I will try that and see if that is the issue.

Thanks

Paul
In reply to Paul Hermans

Re: Newbie - help with PGML

by John Travis -
Paul,

In case you are interested, there is an online workshop starting this week which is focused exactly on authoring WeBWorK problems with a focus on using PGML. Details are on the Main Forum on this site. Directly:

http://webwork.maa.org/moodle/mod/forum/discuss.php?d=4087

Let me know asap if you are interested in joining us.

JT
In reply to John Travis

Re: Newbie - help with PGML

by Paul Hermans -
Thanks for the heads up John.
I can't make this one, but will try to keep an eye out for the future.