Search code examples
rtufte

Define color of newthought() with Tufte style when rendering to pdf


I have no problem defining color of newthought() with Tufte style when rendering to HTML, as follows:

<font color = "blue">`r newthought('The Lab Supervisor')`</font> ...

However, it does not work out when rendering to pdf.

I learned to define color when rendering to both HTML and pdf by the following code:

em <- function(x, color) {
  if (knitr::is_latex_output()) {
    sprintf("\\textcolor{%s}{%s}", color, x)
  } else if (knitr::is_html_output()) {
    sprintf("<span style='color: %s;'>%s</span>", color,
      x)
  } else x
}

Then I apply in-line R expression as follows:
``` `r em("The Lab Supervisor", "blue")` ```

Is there any solution that I could combine both `r newthought()`and `r em()` so that The Lab Supervisor is set to small caps in blue when rendering to pdf?


Solution

  • I just worked it with your exact code. It worked in both pdf and HTML. You just combine the functions as you would in any other set of function calls.

    (I used the tufte template.)

    ```{r func}
    
    em <- function(x, color) {
      if (knitr::is_latex_output()) {
        sprintf("\\textcolor{%s}{%s}", color, x)
      } else if (knitr::is_html_output()) {
        sprintf("<span style='color: %s;'>%s</span>", color,
          x)
      } else x
    }
    
    ```
    

    r newthought(em('In his later books', "blue"))^[Beautiful Evidence], Tufte starts each section with a bit of vertical space, a non-indented paragraph, and sets the first few words of the sentence in small caps. To accomplish this using this style, call the newthought() function in tufte in an inline R expression `r ` as demonstrated at the beginning of this paragraph.^[Note you should not assume tufte has been attached to your R session. You should either library(tufte) in your R Markdown document before you call newthought(), or use tufte::newthought().]

    The pdf:

    enter image description here

    The HTML:

    enter image description here