Mayou
Mayou

Reputation: 8828

Set up different fonts for fragments of string in R

I have a long string txt that I want to display as margin text in a plot using mtext(). The txt string is composed of another string txt.sub, as well as of a date string, which applies a specific format to a date command argument. However, I want to display the "date" part of that string only in bold.

The string is:

 date.in = as.Date( commandArgs( trailingOnly=TRUE )[1], format="%m/%d/%Y" )
 date = format(date.in, "%b %d, %Y")
 txt.sub = "Today's date is: "
 txt = paste(txt.sub, date, sep = "") 

I tried the following

 ## Plot is called first here.
 mtext(expression(paste(txt.sub, bold(date), sep = "")), line = 0, adj = 0, cex = 0.8)

but the problem with this is that it doesn't paste the values of txt.sub and date, but rather displays literally the words "txt.sub" and "date".

Is there any way to get to the result I am looking for?

Upvotes: 4

Views: 307

Answers (1)

Jan van der Laan
Jan van der Laan

Reputation: 8105

Adjusting one of the examples from the help page on mathematical annotation (see example 'How to combine "math" and numeric variables'):

mtext(bquote(.(txt.sub) ~ bold(.(date))), line=0, adj=0, cex=0.8)

Upvotes: 5

Related Questions