Hi there, Is it possible to set different font family for strings in mtext or text? For example, on windows platform with windows() device: plot(1:10, type = "n") text(5,5, "Chinese (English)") #Chinese for Chinese characters it will give the correct Chinese and English characters with two different font family, i.e., English character in default sans family, and Chinese character in the system default font family (it seems that the Chinese font family can not be set or changed). However, when using pdf() or postscript(), if setting the font family to "Times", then error message will appear: conversion failure on '...' in 'mbcsToSbcs': dot substituted for... When set the family "song" (a CJK font family), the English character will be displayed in that CJK font family. I hope to know, is there a mechanism that can be used to set different font family for one string, e.g., if one character can not be find in the default font family, then search for another font family? Any suggestions or comments will be really appreciated? Regards, Jinsong
set different font family for strings in mtext or text?
4 messages · Brian Ripley, Jinsong Zhao
See ?par: check the 'family' paramater. You can select 'family' for each call to mtext or text. However, mixing families is rather ugly, and there are font families that cover both English and Chinese. Note that the main problem with postscript() and pdf() is the limited support in those languages for non-8-bit character encodings: R cannot magically remove restrictions of languages designed in the 1970s. See also http://cran.r-project.org/doc/Rnews/Rnews_2006-2.pdf (referenced from ?pdf) Users of other OSes have the option of using cairographics-based devices (e.g. cairo_pdf), and so will Windows' users as from 2.14.0 (which is in RC): however, the font flexibility is far less on Windows.
On Wed, 26 Oct 2011, Jinsong Zhao wrote:
Hi there, Is it possible to set different font family for strings in mtext or text? For example, on windows platform with windows() device: plot(1:10, type = "n") text(5,5, "Chinese (English)") #Chinese for Chinese characters it will give the correct Chinese and English characters with two different font family, i.e., English character in default sans family, and Chinese character in the system default font family (it seems that the Chinese font family can not be set or changed).
It certainly can, and the rw-FAQ describes how to do so.
However, when using pdf() or postscript(), if setting the font family to "Times", then error message will appear: conversion failure on '...' in 'mbcsToSbcs': dot substituted for... When set the family "song" (a CJK font family), the English character will be displayed in that CJK font family. I hope to know, is there a mechanism that can be used to set different font family for one string, e.g., if one character can not be find in the default font family, then search for another font family?
You have to specify the family: R will not guess what you wanted.
Any suggestions or comments will be really appreciated? Regards, Jinsong
______________________________________________ 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.
Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Thank you very much for the quick reply.
On 2011-10-26 18:24, Prof Brian Ripley wrote:
See ?par: check the 'family' paramater. You can select 'family' for each call to mtext or text.
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()
However, mixing families is rather ugly, and there are font families that cover both English and Chinese.
Yes, there are some font families that cover both English and Chinese, however, in those font families, the English characters are ugly...
Note that the main problem with postscript() and pdf() is the limited support in those languages for non-8-bit character encodings: R cannot magically remove restrictions of languages designed in the 1970s. See also http://cran.r-project.org/doc/Rnews/Rnews_2006-2.pdf (referenced from ?pdf)
Well, I have read this paper very careful, so I can draw CJK on the plot in postscript() and pdf().
Users of other OSes have the option of using cairographics-based devices (e.g. cairo_pdf), and so will Windows' users as from 2.14.0 (which is in RC): however, the font flexibility is far less on Windows.
I will try this device. Thanks for the information. Regards, Jinsong
On Wed, 26 Oct 2011, Jinsong Zhao wrote:
Thank you very much for the quick reply. On 2011-10-26 18:24, Prof Brian Ripley wrote:
See ?par: check the 'family' paramater. You can select 'family' for each call to mtext or text.
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()
However, mixing families is rather ugly, and there are font families that cover both English and Chinese.
Yes, there are some font families that cover both English and Chinese, however, in those font families, the English characters are ugly...
Not to my eyes in Arial Unicode MS (nor to millions of writers of Word documents). Not elegant, but not ugly. And that is one of the recommended choices in several places in the R documentation.
Note that the main problem with postscript() and pdf() is the limited support in those languages for non-8-bit character encodings: R cannot magically remove restrictions of languages designed in the 1970s. See also http://cran.r-project.org/doc/Rnews/Rnews_2006-2.pdf (referenced from ?pdf)
Well, I have read this paper very careful, so I can draw CJK on the plot in postscript() and pdf().
Users of other OSes have the option of using cairographics-based devices (e.g. cairo_pdf), and so will Windows' users as from 2.14.0 (which is in RC): however, the font flexibility is far less on Windows.
I will try this device. Thanks for the information. Regards, Jinsong
______________________________________________ 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.
Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595