{"action":"closed","issue":{"url":"https://api.github.com/repos/yihui/knitr/issues/1477","repository_url":"https://api.github.com/repos/yihui/knitr","labels_url":"https://api.github.com/repos/yihui/knitr/issues/1477/labels{/name}","comments_url":"https://api.github.com/repos/yihui/knitr/issues/1477/comments","events_url":"https://api.github.com/repos/yihui/knitr/issues/1477/events","html_url":"https://github.com/yihui/knitr/issues/1477","id":283429356,"node_id":"MDU6SXNzdWUyODM0MjkzNTY=","number":1477,"title":"Setting `out.width` without `out.height` will not enlarge smaller images (pdf)","user":{"login":"mathematicalcoffee","id":1459546,"node_id":"MDQ6VXNlcjE0NTk1NDY=","avatar_url":"https://avatars0.githubusercontent.com/u/1459546?v=4","gravatar_id":"","url":"https://api.github.com/users/mathematicalcoffee","html_url":"https://github.com/mathematicalcoffee","followers_url":"https://api.github.com/users/mathematicalcoffee/followers","following_url":"https://api.github.com/users/mathematicalcoffee/following{/other_user}","gists_url":"https://api.github.com/users/mathematicalcoffee/gists{/gist_id}","starred_url":"https://api.github.com/users/mathematicalcoffee/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mathematicalcoffee/subscriptions","organizations_url":"https://api.github.com/users/mathematicalcoffee/orgs","repos_url":"https://api.github.com/users/mathematicalcoffee/repos","events_url":"https://api.github.com/users/mathematicalcoffee/events{/privacy}","received_events_url":"https://api.github.com/users/mathematicalcoffee/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":{"url":"https://api.github.com/repos/yihui/knitr/milestones/35","html_url":"https://github.com/yihui/knitr/milestone/35","labels_url":"https://api.github.com/repos/yihui/knitr/milestones/35/labels","id":3130982,"node_id":"MDk6TWlsZXN0b25lMzEzMDk4Mg==","number":35,"title":"v1.21","description":"","creator":{"login":"yihui","id":163582,"node_id":"MDQ6VXNlcjE2MzU4Mg==","avatar_url":"https://avatars2.githubusercontent.com/u/163582?v=4","gravatar_id":"","url":"https://api.github.com/users/yihui","html_url":"https://github.com/yihui","followers_url":"https://api.github.com/users/yihui/followers","following_url":"https://api.github.com/users/yihui/following{/other_user}","gists_url":"https://api.github.com/users/yihui/gists{/gist_id}","starred_url":"https://api.github.com/users/yihui/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yihui/subscriptions","organizations_url":"https://api.github.com/users/yihui/orgs","repos_url":"https://api.github.com/users/yihui/repos","events_url":"https://api.github.com/users/yihui/events{/privacy}","received_events_url":"https://api.github.com/users/yihui/received_events","type":"User","site_admin":false},"open_issues":8,"closed_issues":83,"state":"open","created_at":"2018-02-20T20:15:43Z","updated_at":"2018-12-09T16:04:12Z","due_on":null,"closed_at":null},"comments":1,"created_at":"2017-12-20T02:23:01Z","updated_at":"2018-12-09T16:04:12Z","closed_at":"2018-12-09T16:04:12Z","author_association":"NONE","body":"Sorry - I am not quite sure if this is an rmarkdown bug or knitr bug. It is to do with the rmarkdown latex template, but it's a knitr option.\r\n\r\nIf\r\n\r\n* pdf (latex) output\r\n* specify `out.width` on a chunk\r\n* include a graphic that is smaller than the `out.width`,\r\n\r\nthen\r\n\r\n* the image is not resized larger\r\n\r\n(when compiling using `rmarkdown::render`).\r\n\r\n## example\r\n\r\n~~~\r\n---\r\ntitle: \"test\"\r\noutput:\r\n  pdf_document:\r\n    keep_tex: true\r\n---\r\n\r\nFirst I create an image with imagemagick.\r\n\r\n```\r\nconvert magick:rose rose.png\r\n```\r\n\r\nIt is 70x46 pixels.\r\n\r\nThen I include it using `knitr::include_graphics` at its normal size\r\n\r\n```{r, fig.cap=\"Rose at its usual size\"}\r\nknitr::include_graphics('rose.png')\r\n```\r\n\r\nThen I scale it up to be 50% linewidth (as an aside, I need to escape the % symbol\r\nin the fig.cap).\r\n\r\n```{r, fig.cap=\"Rose at 50\\\\% linewidth\", out.width='50%'}\r\nknitr::include_graphics('rose.png')\r\n```\r\n\r\nIt does not change in size. I have to set the height also.\r\n\r\n```{r, fig.cap=\"Rose at 50\\\\% linewidth and textheight\", out.width='50%', out.height='\\\\textheight'}\r\nknitr::include_graphics('rose.png')\r\n```\r\n~~~\r\n\r\n## expected output\r\n\r\nThe second image should be 50% of the line width (enlarged).\r\n\r\n## actual output\r\n\r\nThe second image is still the original size, 70x46.\r\n\r\n## details\r\n\r\nOn examining the `.tex` file, the LaTeX is fine. There is a \r\n\r\n```\r\n\\includegraphics[width=0.5\\linewidth]{rose} \\caption{Rose at 50\\% linewidth}\\label{fig:unnamed-chunk-2}\r\n```\r\n\r\nso the `0.5\\linewidth` is being propagated correctly.\r\n\r\n## why\r\n\r\nIt seems to be to do with the `\\maxwidth` and `\\maxheight` in the rmarkdown latex template (e.g. `~/R/library/3.3/rmarkdown/rmd/latex/default-1.17.0.2.tex`): the code that stops the images from overflowing the page by setting the default options to `width=\\maxwidth, height=\\maxheight, keepaspectratio`.\r\n\r\nThis sets the default width, height to the width, height of the input image (ie 70x46). \r\nThen I set `out.width='50%'` so we get height=46 pixels, width=0.5\\linewidth.\r\nHowever `keepaspectratio` keeps the image so it fits in the supplied dimensions, so it keeps the height to 46 pixels and doesn't resize it.\r\n\r\n## fix\r\n\r\nTo fix it you have to set the out.height to \\textheight (or something large) so that the image is properly enlarged. Perhaps this is not a bug, but at the very least it is unintuitive and not mentioned anywhere in the documentation for chunk options `out.width` and `out.height`.\r\n\r\nFor me (if I don't want to remember to set `out.height` to some guessed value every time I set `out.width`) I can fix it by adjusting the template (in the rmarkdown package)\r\n\r\n```\r\n\\setkeys{Gin}{width=\\maxwidth,height=\\maxheight,keepaspectratio}\r\n```\r\n\r\nto\r\n\r\n```\r\n\\setkeys{Gin}{width=\\linewidth,height=\\textheight,keepaspectratio}\r\n```\r\n\r\nThen smaller images will be enlarged and larger images will be shrunk.\r\nHowever I do not know if this will have unintended consequences, like making images overflow the margins or something. If I make this change have a chunk with no `out.width` and no `out.height`, the image is not enlarged to the whole page size, but maybe that doesn't hold in general.\r\n\r\nAlternatively, add a note to the `out.width`/`out.height` chunk option documentation to explain that if `out.width` is set and the image is smaller, you must also set `out.height` (to something large) in order for the image to be enlarged.\r\n\r\n## package details\r\n\r\nI have this problem with rmarkdown 1.8.5, knitr 1.17.20.\r\n\r\nHowever I also had it with whatever the current versions are with R 3.3 (from CRAN). Sorry, I do not have the specific version numbers, because I (incorrectly) thought this was a pandoc problem so updated my local pandoc to 2.0.x, which prompted me to have to update rmarkdown (which went to 1.8.5) and I updated knitr with devtools while I was at it, and forgot to note the previous version numbers.\r\n\r\nI suspect any rmarkdown that uses the `\\setkeys{Gin}{width=\\maxwidth,height=\\maxheight,keepaspectratio}` will have this problem.\r\n\r\n## Aside\r\n\r\nI did not realise that percent signs in `fig.cap` should be escaped for LaTeX (or else it thinks they are a comment) though it makes sense. I had assumed knitr would do it for me. Perhaps add a little note in the `fig.cap` documentation about this?"}}
{"actor":{"display_login":"yihui"}}