How do I specify a list of words to be bolded or colored though out an Rmarkdown document?

I'm creating an rmarkdown report and wanted to know if there is a way of specify a list of words that should be bolded or colored throughout the document. For example, I found this function that I can use to highlight certain words red .

This goes in a code chunk -

colorize <- 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
}

When I type This word is `r colorize("red", "red") and knit the document, the word "red" comes out in red text.

Is there a way to specify about 10 words that I want to appear in red text without having to type r colorize("word", "red") each time I type the word.

The minimum problem is that the inline code has to look back to find it's word argument. If you're willing to compose as

This `r colorize("rose")`

you could modify colorize with

redwords <- c("red", "rose", "flag", "t-shirt", "rock", "crayon", "dye","phone", "line", "button")
if(x %in% redwords)

\dots

But that's almost as much trouble. If it's worth the trouble, you do this with a system call to filter the Rmd file through sed or another program or even run knit from the console with a flag to filter the stdin through a Lua or Haskell filter. I've done the latter for a LaTeX document that required some fancier table formatting than I was able to get otherwise, but I don't recommend it unless fluency in Haskell is in the toolkit.

Here's an example of what I ended up with in flex/bison

/* NB: OSX, cannot link to -lfl, use -ll */
/* can':t have internal comments */

%{
%}


%%

\"              { printf("")            ; }
^%.*$           { printf("")            ; }
^.%.*$          { printf("")            ; }
"{rrrrr}"       { printf("{lrrrr}")     ;
                  printf("\n")          ; }
" \\midrule"    { printf("Cause of Death & \\%% & \\%% & \\%% & \\%% \\\\");
                  printf("\n")          ;
                  printf(" \\midrule")  ;
                  printf("\n")          ; } 
"& 0.00"        { printf("& .  ")       ; }  
%%

int main()

{
  yylex();
}

You mean 10 following words or 10 words repeated multiple times in the document that you always want in red ?

For the former, you can use colorize() function on several words at once

Some `r colorize("words in red", "red")` in this sentence

For the latter, there is nothing out of the box in knitr that allows to do that.

You may be able to do this using rmarkkown and Pandoc's Lua filter - I don't know of any example of that though. Lua filters enables parsing the Abstract Syntax Tree during Pandoc processing. You could detect words you want and write the correct output syntax for highlight.

BTW, there is a feature request for another syntaxt than the colorize function in this feature request. It shows the use of a Lua filter:

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.