[R] string width calculation

Henrique Dallazuanna wwwhsd at gmail.com
Sat Mar 27 20:45:03 CET 2010


See ?strwidth

On Sat, Mar 27, 2010 at 3:42 PM, Dennis Fisher <fisher at plessthan.com> wrote:
> Colleagues,
>
> I am trying to create a PDF document in which I use margin text with two different fonts.  The resulting text might be:
>        XXXXXyZZZ
> where X and Z are one font and Y is the other.
>
> My plan was to do this in the following manner:
>        mtext("XXXXX ZZZ", cex=2, adj=0.5, family=SOMEFONT)
>        mtext("Y", cex=2, adj=??, family=DIFFERENTFONT)
>
> My question regards how to calculate "adj" for the second string.
>
> I tried the following (different strings here and the same font was used for both strings - even under these circumstances, it does not work correctly):
>> STRING1       <- "0123456789 0"
>> STRING2       <- "          x "
>> pdf("GREEK.pdf", width=10, height=8)
>> par(mfrow=c(1,1))
>> WIDTH1        <- strwidth("0123456789",  cex=1, units="inches", family="", font=1)
>> WIDTH2        <- strwidth("0",           cex=1, units="inches", family="", font=1)
>> plot(1,1,type="n", axes=F, xlab="", ylab="")
>> mtext(line=-1, outer=T, cex=1, family="", font=1, adj=0.5, STRING1)
>> mtext(line=-1, outer=T, cex=1, family="", font=1, adj=0.5 - (WIDTH2 - WIDTH1)/2/par()$pin[1], STRING2)
>> mtext(line=-1, outer=F, cex=1, family="", font=1, adj=0.5, STRING1)
>> mtext(line=-1, outer=F, cex=1, family="", font=1, adj=0.5 - (WIDTH2 - WIDTH1)/2/par()$din[1], STRING2)
>> dev.off()
>
>
> The logic is:
>        1.  determine the width of the first and second portions of the string for the first font
>        2.  one-half of the difference between these two should be the offset
>        3.  however, WIDTH1 and WIDTH2 have units of inches - so I need to normalize them to the width of the figure (if outer=T, use width of the page)
>
> Unfortunately, spacing is not perfect.  Perhaps I misunderstand how "adj" is applied or I am apply.  Can anyone help me to understand how "adj" is applied?
>
> Thanks in advance.
>
> Dennis
>
> Dennis Fisher MD
> P < (The "P Less Than" Company)
> Phone: 1-866-PLessThan (1-866-753-7784)
> Fax: 1-866-PLessThan (1-866-753-7784)
> www.PLessThan.com
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O



More information about the R-help mailing list