Skip to content
Prev 275557 / 398506 Next

set different font family for strings in mtext or text?

Thank you very much for the quick reply.
On 2011-10-26 18:24, Prof Brian Ripley wrote:
Yes, I can select 'family' for each call to mtext or text. however, when 
it's necessary to put both Chinese and English in one line, I should 
call text or mtext several times with position explicitly. It will be 
really tedious. The following code have been used for this purpose, 
however, I don't like this design:

put.text <- function(x, y, text, family, font, ...) {
    str.n <- length(text)
    sw.n <- numeric(length = str.n+1)
    sw.n[1] <- 0

    if (missing(family)) family <- rep("", str.n)
    if (missing(font)) font <- rep(1, str.n)

    for (i in 1:str.n) sw.n[i+1] <- strwidth(text[i], family = 
family[i], font = font[i])
    sw <- sum(sw.n)
    for (i in 1:str.n)
       text(x+sum(sw.n[1:i]), y, text[i], family = family[i], font = 
font[i], adj = c(0,0.5), ...)
}

## usage
## plot "??(English)" with different font family
## 'song' is a user defined font family for CJK.
pdf()
plot(1:10, type = "n")
put.text(5, 5, c("??", "(English)"), c("song", "Times"))
dev.off()
Yes, there are some font families that cover both English and Chinese, 
however, in those font families, the English characters are ugly...
Well, I have read this paper very careful, so I can draw CJK on the plot 
in postscript() and pdf().
I will try this device. Thanks for the information.

Regards,
Jinsong