WeBWorK Problems

Changing font to Courier

Changing font to Courier

by Robin Cruz -
Number of replies: 7
I am writing problems were R commands are in the text of the problem.  I wanted to set these apart by having them in Courier.  I've tried a couple things and both give errors when I try to make a hardcopy. Both print the text to the screen in Courier.  I put the command before the problem text.

1)  This one prints the text to the screen in Courier, but the text is small, red and enclosed in a box.
#####Courier font
$BCOURIER = "<span class='tex2jax_ignore'><code>";
$ECOURIER = "</code></span>";

2)  This one is from a post Davide recently made to a forum on Mathjax. The text shows up in Courier, is black and is scaled nicely with the other text, but still causes an error in making a hardcopy.
#####Courier font
$BCOURIER = "\(\style{font-family:Courier!important}{\text{";
$ECOURIER = "}}\)";


Thanks -- rac

---------------------------------Example of problem text-------
BEGIN_TEXT
Use ${BCOURIER}findZeros()$ECOURIER to find the zeros of each of these polynomials.
END_TEXT
In reply to Robin Cruz

Re: Changing font to Courier

by Paul Pearson -
Hi Robin,

You might try using typewriter text via ${BTT}findZeros()${ETT}. For small bits of code, you might also try \( \verb+findZeros()+ \). Another alternative might some custom commands such as

$BCODE = MODES( TeX => '\begin{verbatim}', HTML => '<code>' );
$ECODE = MODES( TeX=> '\end{verbatim}', HTML => '</code>' );

that could be used via ${BCODE}findZeros()${ECODE}. I encourage you to write custom commands, like the one above, so that the pdf hardcopy is usable (TeX mode sometimes gets forgotten). You could also put the custom commands into a macro file.

People with more font expertise are encouraged to chime in with better answers that will actually provide Courier font.

Take care,

Paul Pearson
In reply to Paul Pearson

Re: Changing font to Courier

by Davide Cervone -
Your first approach was better than the second. The reason that you get red, a box, and smaller is that there are CSS rules in your theme that style the <code> element. (I object to having such styles in the theme CSS, indicating that it would cause problems like this. I think some have been removed, but not all.)

You could change the theme CSS, or could use PGcourse.pl to insert (into every problem that includes PGcourse.pl) some CSS that overrides the styles for <code> elements.

Paul's idea is the one I recommend as well. Here are some definitions that implement in-line and multi-line code segments:

    $BCODE = MODES(HTML=>'<code style="font-family: Courier"&gtl',TeX=>'\verb'.chr(0x85));
    $ECODE = MODES(HTML=>'</code>',TeX=>chr(0x85));
    
    $BPRE = MODES(HTML=>'<pre style="font-family: Courier">',TeX=>'\begin{verbatim}');
    $EPRE = MODES(HTML=>'</pre>',TeX=>'\end{verbatim}');
which you use as
    BEGIN_TEXT
    This is in-line code: ${BCODE}findZeros()${ECODE} finds the zeros of the function.
    $PAR
    Here is multi-line code:
    $BPRE
         Line1 of code
           Line1.1
         Line2
    $EPRE
    That's it.
    END_TEXT
The theme styling may affect these as well, so you might need to specify more style rules, such as
    $BCODE = MODES(
        HTML=>'<code style="font-family: Courier; font-size:inherit; background:transparent; border:0; padding:0">',
        TeX=>'\verb'.chr(0x85)
    );
to override the ones set in the theme. It would also be possible to use <tt> rather than <code> tags, which might not be styled by the theme (I didn't check).

Davide

In reply to Davide Cervone

Re: Changing font to Courier

by John Jones -
These seem like commands which should go in PGbasicmacros where other styling macros are found.

John

In reply to John Jones

Re: Changing font to Courier

by Davide Cervone -
I think you could do a generic one (that uses <code> and <pre> tags) in PGbasicmacros, but I would not do the Courier-specific ones there. I'm also not sure that the CSS to override the theme should go there, either. (I would rather see the themes not modify the styles used within the problem itself, but that's a different argument.)
In reply to Robin Cruz

Re: Changing font to Courier

by Alex Jordan -
The first thing that occurred to me though was to use PGML, and use the preformatted text marker, a colon followed by three spaces.

BEGIN_PGML
: Here is some code
: Here is some code
END_PGML

Of course, in my experience "preformatted" has always meant monospaced font. I'm sure it actually means something different and more involved, so it is recommended to understand what preformatted text is better than I do.
In reply to Alex Jordan

Re: Changing font to Courier

by Robin Cruz -
Hi,

Thanks for the suggestions, I tried them all and found the one that seemed to work the best is a somewhat modified version:

$BCOURIER = MODES(
HTML=>'<code style="font-family: Courier; font-size:inherit; background:transparent; border:0; padding:0; word-wrap:break-word; white-space: pre-wrap; white-space: -moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; display: inline; ">',
TeX=>'\verb'.chr(0x85)
);
$ECOURIER = MODES(HTML=>'</code>',TeX=>chr(0x85));

My collegue, Dave Rosoff, added the "extra" options in hopes of making it compatible with multiple browsers. It wraps lines and the text scales nicely. The text shows up on the screen in red in Chrome and IE, but I kind of like it. I suppose there is an option to add that would make it black all the time. I've put it in a file in my macros folder and load it when I need it so it is easy to modify for all of the problems using it. I'll be interested to know if a courier font gets added to the PGbasicmacros in the future.

Thanks, again--rac

In reply to Robin Cruz

Re: Changing font to Courier

by Davide Cervone -
You could set color:black or color:inherit if you want to control the color. I had meant to include that, but forgot it.

Glad you got it to work as you would like.