Search code examples
rr-exams

Fixing font size in tex2image graphics


I created several graphics with different font sizes using the tex2image() function from the exams package. But the parameter pt, which should specify the font size, only seems to have an effect on the line spacing. I want to fix the font so that it appears the same size in all images.

For illustration try creating the following image with pt = 12 vs. pt = 5. The output is shown below.

library('exams')

tex <- 'test\\\\test\\\\test'

tex2image(tex, format = "png", dir= ".", name = 'test-12', resize = 350, pt = 12)
pt = 12 pt = 5
12pt image 5pt image

Solution

  • The reason for this behavior is that the default is to resize PNG output from tex2image() to a certain number of pixels: resize = 650. Thus, in your pt = 5 setup a smaller PDF and PNG is generated first compared to the pt = 12 setup but both are scaled to 650 pixels afterwards. This makes the resulting text similarly large but the pt = 5 version more pixelated.

    The first way to avoid this would be to set resize = NULL so that no rescaling is done. Then you would need to make sure to include the resulting PNG images "as is" without any rescaling in HTML.

    Alternatively, you can set a certain width = ... (in inches, > 1) for the text box to make sure that all images have the same width, thus preserving relative font sizes. This approach is illustrated by:

    tex2image("Short text",                                  width = 2, pt = 12, dir = ".", name = "tex2image-short")
    tex2image("A considerably longer text",                  width = 2, pt = 12, dir = ".", name = "tex2image-long-12")
    tex2image("A considerably longer text with small fonts", width = 2, pt =  5, dir = ".", name = "tex2image-long-5")
    

    Short text, large font

    Long text, large font

    Long text, small font