ElMaestro ★★★ Denmark, 2022-04-24 21:09 (962 d 09:31 ago) Posting: # 22936 Views: 7,957 |
|
Hi all, hot greetings from Ahmedabad. Let's say I do this: foo=function(s1, s2) On my system (and it may indeed be dependent on various factors like devices, resolution, OS and a ton of stuff I know nothing about) I am getting the two labels printed with Arial in the rectangle, and while s1 fits inside the rectangle, s2 doesn't - it extends beyond the rectangle borders to the right. So here's the challenge: How can I determine dynamically if any text s2 fits inside the rectangle or not? I have deliberately chosen not to use specify a fixed-width font family (Courier would otherwise be easy to set). Note also the "useKerning" option of the pdf function which may have some influence on actual text width. Many thanks for any input. EM. — Pass or fail! ElMaestro |
Helmut ★★★ Vienna, Austria, 2022-04-25 00:24 (962 d 06:16 ago) @ ElMaestro Posting: # 22937 Views: 7,515 |
|
Hi ElMaestro, ❝ hot greetings from Ahmedabad. Hope you enjoy it. The problem with your code is that rect() does not – and can not – ‘know’ how wide text() will be.A workaround:
With the additional argument plot = FALSE you could assign all what is needed to a variable:
rect a list with componentstext a list with componentslegend() directly (eventually with x, y coordinates) is much simpler. See the examples at the end of the man page of legend .— Dif-tor heh smusma 🖖🏼 Довге життя Україна! Helmut Schütz The quality of responses received is directly proportional to the quality of the question asked. 🚮 Science Quotes |
ElMaestro ★★★ Denmark, 2022-04-25 06:20 (962 d 00:21 ago) @ Helmut Posting: # 22938 Views: 7,464 |
|
Hi Helmut, thanks for your response. Perhaps my example was badly illustrated. I am not trying to create a legend. I am trying to know how wide a text is, even if in the example I have illustrated the matter through something similar to the notion of a legend. So, more generally: I need to check if my text can fit within a span of dx units on a plot (dx in my example was the width of a rect, this rect was only for visualisation of the challenge). If the text fits can then I am happy and if it can't then I need to change certain things, like creating two lines in stead of one (but changing cex is not the appropriate option). dx could also be the space between the two tall bars on the plot, or the entire plot area. And so forth. Kindly allow me to rephrase: I have a dynamic text s. I am going to put it on a plot. But I need to know if s at a given set of parameters and setting fits onto the plot within a width of dx units on the horizontal axis as one line. Nothing else. — Pass or fail! ElMaestro |
Helmut ★★★ Vienna, Austria, 2022-04-25 11:26 (961 d 19:14 ago) @ ElMaestro Posting: # 22940 Views: 7,407 |
|
Hi ElMaestro, ❝ I have a dynamic text s. I am going to put it on a plot. But I need to know if s at a given set of parameters and setting fits onto the plot within a width of dx units on the horizontal axis as one line. I see. You create the plot first and then strwidth(s) and strheight(s) will give you what you need.— Dif-tor heh smusma 🖖🏼 Довге життя Україна! Helmut Schütz The quality of responses received is directly proportional to the quality of the question asked. 🚮 Science Quotes |