yihui / knitr

A general-purpose tool for dynamic report generation in R
https://yihui.org/knitr/
2.38k stars 880 forks source link

Automatically derive fig.width from out.width #991

Closed e-pet closed 9 years ago

e-pet commented 9 years ago

Currently, when working with latex, I have to manually adjust the fig.width parameter in order to have my figure nicely scaled for the output size out.width. Consider e.g. the basic task of including a figure at full textwidth. To obtain a correctly scaled figure, I'd currently have to somehow obtain a numerical value for textwidth in inches (for me, this is something about 5.8in as I found out following this answer on TeX.SE) and then set fig.width to this value. This is annoying, as a) I somehow have to look up the current value of \textwidth, b) this value can change depending on the document's parameters, and my code does not adjust, c) I would consider it the most common use case that I want my figure to be scaled to the output size...

Is there a particular reason why this is not implemented? I was a little bit surprised how complicated this is. Did I miss something?

krlmlr commented 9 years ago

See yihui/tikzDevice#33 for the corresponding tikzDevice issue. IMO, looking up \textwidth once per document you write is probably simpler and more robust than implementing any automagic. If the geometry package is loaded, this information is even available in the LaTeX logfile -- why can't you grab it from there?

e-pet commented 9 years ago

As I said, of course there are ways to look up the value of \textwidth manually. But I would consider this a very basic use case - "Produce a correctly scaled R plot in Latex" -, and so I am quite surprised that this requires hassling with TeX internals. From a user perspective, I would have very much expected this to work 'out of the box'.

However, I think I just understood the problem here: That knitr runs before LaTeX and without any knowledge on the surrounding latex environment, so it simply can't know the current value of \textwidth. Did I get that right? If yes, I'd agree with you: Leaving it the way it is is probably more robust than somehow inferring the value of \textwidth etc. automatically from previous runs or something.

krlmlr commented 9 years ago

I guess so: knitr indeed cannot know how the geometry of your document looks like, and even tikzDevice would need to measure it based on your input.

yihui commented 9 years ago

Yes, it looks like a simple problem but there are endless technical difficulties. I'm as eager as you to find an automatic solution, but I don't think it is a trivial problem. First, R does not understand LaTeX, and it does not know what \textwidth is (unless your parse the LaTeX log afterwards). Second, by the time LaTeX makes the width information available, the compilation of LaTeX has already started, and it is too late to send anything back to R to draw the plot (unless you iterate between LaTeX and R for another round). Third, an automatic width is not necessarily a good thing. I'm not sure how many years you have been using LaTeX and how many documents you have written, and you probably know the pain of floating environments, which at least often drives me crazy. I think it can be worse if my plots have variable sizes.

To sum up, I think these kinds of typesetting details are natural in the LaTeX world. The only practical solution I can think of is to hand typesetting jobs over to publishers, unless we get a Donald Knuth v2 in the future who designs a more intelligent system from which we do not suffer. LaTeX achieved the goal to make readers happy, but authors often have to spend countless hours to get things right.

e-pet commented 9 years ago

Amen to that. ;) And: Eagerly looking foward to that v1.10! ;)

yihui commented 9 years ago

Unfortunately, I did not mean to solve this issue in v1.10. I meant to close it as "wontfix". Sorry.

e-pet commented 9 years ago

I wasn't really expecting it to be solved after reading your response. ;)

github-actions[bot] commented 3 years ago

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.