Search code examples
rplotlatexknitrr-exams

How to reduce plot (image) size for `exams2pdf` output?


When I reduce the size of the image in the scatterplot.Rmd file and weave it into exams2html, the result is as expected. The same thing happens when I knit the file with the knitr button.

I achieve this by modifying the sizes from

fig.path = "", fig.cap = ""}
plot(x, y)

changing the values of fig.height and fig.width.

But, when I do the same thing and try to knit with exams2pdf output, the image gets distorted or remains the same initial size.

What changes do I need to make to the code so that the result is as expected in the exams2pdf output?

Thanks for your attention.


Solution

  • TL;DR

    With your settings you control the size of the graphics file that is created. But the default LaTeX template includes all graphics with 80% of the text width. Thus, no matter how large the actual graphics file is (.pdf, .png, ...) it is always embedded in LaTeX/PDF with 80% width.

    Controlling the graphics size

    You can either control the properties of the graphics file by setting the corresponding options in the code chunks in the exercise directly. Namely you can set fig.height=... and/or fig.width=... as described in your question.

    Additionally, you can also not set these options in the exercise file but instead set them in the exams2xyz() interface, e.g., exams2pdf(..., height = ..., width = ...). This is convenient if you want different default graphics sizes in PDF output or HTML output, for example.

    Controlling the size of the embedded graphic

    Similarly you can control the size of the embedded graphics in different ways. In the HTML-based output in R/exams the graphics files are typically included "as is". Thus, the size of the graphic in the HTML text is determined by the size of the graphics file.

    Alternatively, you can try to control this through code chunk options like out.width etc.

    Finally, you can set properties in the template of the output document. This is what is implicitly done in the default plain.tex template. This uses

    \usepackage{Sweave}
    

    which contains the line

    \setkeys{Gin}{width=0.8\textwidth}
    

    by default. But you can suppress this setting by using

    \usepackage[nogin]{Sweave}
    

    Thus, if you modify your LaTeX template accordingly and call exams2pdf(..., template = "mytemplate.tex") then the graphics files are included "as is".