WeBWorK Main Forum

Font size in dynamically generated graphs

Font size in dynamically generated graphs

by Carl Yao -
Number of replies: 10
Hello:

I would like to increase the font size in an image, but don't know how. In the attached code, I would like to see a much bigger (, ), [ and ] symbols. It would be great if there is a command to control the font size.

Thank you for any help!

Carl Yao
Portland Community College

# WeBWorK problem written by Carl Yao
# Portland Community College
#
# Change set notation to graph.
#
# Last edited by Carl Yao on 7/18/13
#
# ENDDESCRIPTION

## DBsubject('Algebra')
## DBchapter('Basic Algebra')
## DBsection('Inequalities')
## KEYWORDS('linear','inequality', 'number line','set notation')
## DBCCSS('7.EE.4')
## TitleText1('')
## EditionText1('')
## AuthorText1('')
## Section1('')
## Problem1('')
## Author('Carl Yao')
## Institution('PCC')

##############################################

DOCUMENT();

loadMacros(
"PGgraphmacros.pl",
"PGstandard.pl",
"MathObjects.pl",
"PGcourse.pl",
"PGML.pl",
"parserRadioButtons.pl",
);

##############################################

Context("Numeric");

$reach = 5;
$shift = 11;
$a=random(-5,5,1);
$symbol = '>';
$inequ = "x $symbol $a";

@ar_points = (
[$a,'(','inf',')'],
['-inf','(',$a,')'],
[$a,'[','inf',')'],
['-inf','(',$a,']'],
);

sub graphInterval {
my ($ra_points, $r_gr) = @_;

if (@$ra_points[0] eq '-inf') {
$$r_gr->lb( new Label($shift+@$ra_points[2],-0.5,
@$ra_points[2],'red','center','middle'));
$$
r_gr->lb( new Label($shift+@$ra_points[2],0,
@$ra_points[3],'red','center','middle'));
$$r_gr->moveTo($shift+@$ra_points[2],0);
$$
r_gr->arrowTo($shift-$reach+0.1,0,'red',3);
return;
}
else {
$$r_gr->lb( new Label($shift+@$ra_points[0],-0.5,
@$ra_points[0],'red','center','middle'));
$$
r_gr->lb( new Label($shift+@$ra_points[0],0,
@$ra_points[1],'red','center','middle'));
$$r_gr->moveTo($shift+@$ra_points[0],0);
}

if (@$ra_points[2] eq 'inf') {
$$
r_gr->arrowTo($shift+$reach-0.1,0,'red',3);
}
else {
$$r_gr->lb( new Label($shift+@$ra_points[2],-0.5,
@$ra_points[2],'red','center','middle'));
$$
r_gr->lb( new Label($shift+@$ra_points[2],0,
@$ra_points[3],'red','center','middle'));
$$r_gr->lineTo($shift+@$ra_points[2],0,'red',3);
}
}

@gr=();

for ($i=0;$i<=3;$i++) {
$gr[$i] = init_graph($shift-$reach,-1,$shift+$reach,1,
axes=>[0,0],
size=>[1000,50]
);

@ticks = ();
for my $j (($shift-$reach)..($shift+$reach)) {push(@ticks,$j);};

$gr[$i]->h_ticks(0,"black",@ticks);
$gr[$i]->stamps( closed_circle($shift,0,'blue') );
$gr[$i]->lb( new Label($shift,-0.5,0,'black','center','middle'));

&graphInterval($ar_points[$i], ~~$gr[$i]);
}

#randomize choices
$p=random(1,3,1);
do {$q=random(1,3,1);} until ($p!=$q);
$temp=$gr[$p];
$gr[$p]=$gr[$q];
$gr[$q]=$temp;

@ar_choice = ("A","B","C","D");
$correct = 0;
$correct = random(0,3,1);
$temp=$gr[$correct];
$gr[$correct]=$gr[0];
$gr[0]=$temp;

$radio = RadioButtons(
["A","B","C","D"],
"$ar_choice[$correct]", # correct answer
order => ["A","B","C","D"],
);


##############################################

Context()->texStrings;

BEGIN_TEXT

$PAR
Choose the graph showing \($inequ\)
$PAR

A. \{ image( insertGraph($gr[0]), width=>1000, height=>50, tex_size=>800,
extra_html_tags=>'title="Graph of an interval on a number line." alt="Graph of an interval on a number line."' ) \}
$PAR
B. \{ image( insertGraph($gr[1]), width=>1000, height=>50, tex_size=>800,
extra_html_tags=>'title="Graph of an interval on a number line." alt="Graph of an interval on a number line."' ) \}
$PAR
C. \{ image( insertGraph($gr[2]), width=>1000, height=>50, tex_size=>800,
extra_html_tags=>'title="Graph of an interval on a number line." alt="Graph of an interval on a number line."' ) \}
$PAR
D. \{ image( insertGraph($gr[3]), width=>1000, height=>50, tex_size=>800,
extra_html_tags=>'title="Graph of an interval on a number line." alt="Graph of an interval on a number line."' ) \}

$BR
$BR
\{ $radio->buttons() \}



END_TEXT

Context()->normalStrings;

##############################################

ANS( $radio->cmp() );

BEGIN_PGML_SOLUTION

The inequality is [`[$inequ]`].

Since [`x`] needs to be greater than [`[$a]`], we should go toward the [` +\infty `] end of the number line.

Since the inequality symbol is [$symbol], we should use an open ending at [`[$a]`].

The correct answer is [@ $radio->correct_ans() @]*.

END_PGML_SOLUTION

ENDDOCUMENT();

In reply to Carl Yao

Re: Font size in dynamically generated graphs

by Carl Yao -
In reply to Carl Yao

Re: Font size in dynamically generated graphs

by Gavin LaRose -
Hi Carl,

It looks as if the label font is specified as one of the built in GD fonts in .../pg/lib/Label.pm:

    font => GD::gdMediumBoldFont,

Changing this would therefore require figuring out a sensible way to specify the fonts available. My guess is that it would be easy enough to add an optional argument to Label that specified the font size (e.g., small, medium or large) which would then select a font accordingly. Obviously that requires going in and changing the code in Label.pm.

Gavin

In reply to Gavin LaRose

Re: Font size in dynamically generated graphs

by Andrew Dabrowski -
Has anything changed in the past 3 years? Those default fonts are ridiculously small.
In reply to Andrew Dabrowski

Re: Font size in dynamically generated graphs

by Gavin LaRose -
I don't know of any change. I attach an edited Label.pm file that allows declaration of a font size. (I haven't tested it, but the changes are—famous last words—innocuous.)

This should allow calling
    $label1 = new Label( $x, $y, "$text", 'blue', 'left', 'top', 'medium' );
to get a larger font than the default. It's a bit hackish, so that to declare a font size one must declare a color, but it doesn't care what order the justification and font size declarations are in. It enables the font sizes "small," "medium," and "large," with the default being "small," which is the current GD::gdMediumBoldFont. The "medium" and "large" settings are gdLargeFont and gdGiantFont.

Of course, if you're on a hosted service this may not be immediately useful. If Mike or someone sees this and decides it (a) works and (b) isn't too horrible, perhaps we can get it into the distribution.

Gavin
In reply to Gavin LaRose

Re: Font size in dynamically generated graphs

by Bruce Yoshiwara -
Is it (yet) possible to modify the font size in labels of a WeBWorK dynamically generated graph (without requiring Gavin's suggestion of modifying the Label.pm file)?

Is it possible to use LaTeX in a label? http://webwork.maa.org/moodle/mod/forum/discuss.php?d=3488

Bruce
In reply to Bruce Yoshiwara

Re: Font size in dynamically generated graphs

by Alex Jordan -
For WW 2.14, I added some features to making labels in such graphs. You can now tack on to the options: 'large', 'giant', 'small', 'tiny', or the default that has always been there, 'mediumbold'. I've become a fan of 'large'. (Side note, you can also now tack on 'vertical' to print the text vertically.)

These five sizes are the only sizes available with the GD font tools being used to make these graphs.

It helps to understand that these GD fonts are a fixed pixel size. For example, it seems to me that the 'large' font is about 10 pixels high. When you make these graphs, you initialize the plot as so many pixels wide and so many pixels tall. You have to think about that in relation to the roughly 10 pixel height of these fonts.

Later when you insert the graph, you separately declare how many pixels wide and tall to present the image. This is separate from the choice made when initializing the graph. If you make the pixel count at initialization small, and the count at presentation large, you could make these fonts appear even larger, but of course they will appear chunky and pixelated. I don't recommend doing this, just trying to help explain what's happening with these fonts. I recommend you try to make the pixel count at initialization and at presentation match. These fonts don't behave like fonts you work with elsewhere, because they have fixed pixel height instead of using typesetting measurements like pt.
In reply to Alex Jordan

Re: Font size in dynamically generated graphs

by Andrew Dabrowski -
Will it be possible to set the default font size in the init_graph command? If not there, where?

In reply to Andrew Dabrowski

Re: Font size in dynamically generated graphs

by Alex Jordan -
I don't think there is a way to set a default font when invoking the init_graph command. If that is a feature that could be added, it would be a nice contribution. Somehow you would need to store that option with the graph object, and then when the labeling commands are used, they would have to access that stored option.

For a server-wide global change, here is what I can say. I found that there are many OPL problems with dynamic graphs where some effort was made by the problem author to position labels precisely under the assumption the font would be the default boldface, and if I changed the font, then suddenly some labels became hard/impossible to read. For example, partially off screen or overlapping partially with other labels. So I dropped any idea to change the default font that webwork is packaged with for these images.

If you would like to change the default for your own installation, you can do that, but there will be some side effects as I mentioned. In pg/lib/Label.pm, search for GD::gdMediumBoldFont where the defaults are defined and you can change it to one of the other options (GD::gdSmallFont, GD::gdLargeFont, GD::gdTinyFont, GD::gdGiantFont).

Then if you do this, there won't be a way to get the bold font when you want it. So around line 96, you probably want to add a line like:

elsif ($j eq 'bold') {$self->font(GD::gdMediumBoldFont); }

If you make these edits, you will need to restart apache before you see any effects. And possibly clear cached images from the browser. And maybe also from where the webwork server caches images.
In reply to Alex Jordan

Re: Font size in dynamically generated graphs

by Andrew Dabrowski -
Thanks for those suggestions. What I was getting at (I wasn't clear) was can we set the font size for all labels in a particular graph in the init_graph command, or do we have to do it for each individual label?
In reply to Andrew Dabrowski

Re: Font size in dynamically generated graphs

by Alex Jordan -
> can we set the font size for all labels in a particular graph in the init_graph command

I was trying to get to that with the first paragraph. I don't think you could do it right now. But I think it's possible that is a feature that could be added.