How to make fig.width and out.width compatible with knitr?

I recently opened knitr, and I mainly use it to create graphs with dev=tikz to easily get a set of latex sets. However, I do not understand how to set the package parameters fig.width and out.width with consistency.

I mean, I know that the first one is the R option and the second is the latex option coming with \includegraphics , but it seems that if fig.width too large compared to out.width , then the lines are very thin, because latex compresses the image. On the other hand, if it is too small, then latex stretches it, and everything is too large.

In principle, I would like to have a setting in which I select only out.width , and then the thickness of the lines and texts is consistent with the size of the document text.

I include MWE illustrating my problem. In addition, I set fig.height in the first example, otherwise the image was larger than the page, and I do not really understand this.

Any help is appreciated!

 \documentclass[12pt,a4paper]{article} \usepackage[utf8]{inputenc} \usepackage[english]{babel} \usepackage[T1]{fontenc} \usepackage{graphicx} <<setup, include=FALSE, cache=FALSE>>= library(knitr) options(formatR.arrow=TRUE,width=50) opts_chunk$set(fig.path='figure/graphics-', cache.path='cache/graphics-', fig.align='center', dev='tikz') @ \begin{document} \begin{figure}[!ht] <<fig_1,fig.width=2, fig.height = 3,out.width = '\\textwidth',echo=FALSE>>= x = seq(1,3,l=100) plot(x,cos(x), type ="l", xlab = "x", ylab = "y") @ \end{figure} \begin{figure}[!ht] <<fig_2,fig.width=10, out.width = '\\textwidth',echo=FALSE>>= x = seq(1,3,l=100) plot(x,cos(x), type ="l", xlab = "x", ylab = "y") @ \end{figure} \end{document} 
+5
source share
2 answers

As a rule, no resizing should be done after the creation of tikzpicture , because it will lose consistency with the text style. In fact, when the chunk cache=FALSE parameter is set, then out.width does not work because a PDF file is not created. Therefore, for each fragment, you need to specify the exact measurements in inches for fig.width and fig.height .

After some research on both stackoverflow and Knitr sites, I found a trick to have almost the same user experiment, that is, I don’t want to care about real sizes, but I only think about \textwidth and other latex variables:

  • in the installation fragment, define R variables with an explicit name (for example, paperwidth , textwidth corresponding to latex in inches, for example, A4 paper (w, h) = (8.3, 11.7).
  • in each fragment with the graph, you can use fig.width = 0.5*paperwidth , for example

Here is the MWE:

 \documentclass[12pt,a4paper]{article} \usepackage[utf8]{inputenc} \usepackage[english]{babel} \usepackage[T1]{fontenc} \usepackage{graphicx} \usepackage{tikz} <<setup, include=FALSE, cache=FALSE>>= library(knitr) options(formatR.arrow=TRUE,width=50) opts_chunk$set(fig.path='figure/graphics-', cache.path='cache/graphics-', fig.align='center', dev='tikz', external=TRUE, echo=FALSE ) a4width<- 8.3 a4height<- 11.7 @ \begin{document} \begin{figure}[!ht] \centering <<fig_1, fig.width = 0.4*a4width, fig.height = 0.2*a4height>>= x = seq(1,3,l=100) plot(x,cos(x), type ="l", xlab = "x", ylab = "y") @ <<fig_2, fig.width = 0.2*a4width, fig.height = 0.5*a4height>>= x = seq(1,3,l=100) plot(x,cos(x), type ="l", xlab = "x", ylab = "y") @ \caption{Test figure} \end{figure} \end{document} 
+6
source

You probably shouldn't have \begin{figure} since knitr will include them for you. In addition, I believe that it is best when LaTeX does not resize the shapes at all, i.e. They already have the right size. Here is an example where I set the width of the text to 6 inches and the width of the shape to the same value.

 \documentclass[12pt,a4paper]{article} \usepackage[utf8]{inputenc} \usepackage[english]{babel} \usepackage[T1]{fontenc} \usepackage{graphicx} <<setup, include=FALSE, cache=FALSE>>= library(knitr) options(formatR.arrow=TRUE,width=50) opts_chunk$set(fig.path='figure/graphics-', cache.path='cache/graphics-', fig.align='center', dev='tikz') @ \usepackage[text={6in,9in},centering]{geometry} \begin{document} <<fig_1, fig.width=6, echo=FALSE>>= x = seq(1,3,l=100) plot(x,cos(x), type ="l", xlab = "x", ylab = "y") @ <<fig_2, fig.width=6, out.width = '\\textwidth',echo=FALSE>>= x = seq(1,3,l=100) plot(x,cos(x), type ="l", xlab = "x", ylab = "y") @ \end{document} 
+1
source

All Articles