How to change the font color?

前端 未结 7 1724
轻奢々
轻奢々 2020-11-29 00:30

In RMarkdown is there a way to specify the font color?

There doesn\'t seem to be an option while browsing through the chunk options

相关标签:
7条回答
  • 2020-11-29 00:36

    The answer given at the link provided by @Ben Bolker:

    Roses are <span style="color:red">red</span>, 
    violets are <span style="color:blue">blue</span>.
    

    does work if you select HTML (ioslides) as the output format.

    However, it does not work if you select pdf (beamer) as output format. If you want to create a pdf, use LaTeX syntax:

        Roses are \textcolor{red}{red}, violets are \textcolor{blue}{blue}.
    
    0 讨论(0)
  • 2020-11-29 00:38

    This seems to work very well in both output formats, pdf and html:

    Roses are $\color{red}{\text{beautiful red}}$, 
    violets are $\color{blue}{\text{lovely blue}}$.
    

    Hope it helps.

    0 讨论(0)
  • 2020-11-29 00:41

    I basically used Nicholas Hamilton's answer but because I used xtable and print, I had some problems with certain latex sequences being escaped. Namely, \\textcolor being transformed to $\backslash$textcolor. I was able to get it right by avoiding sanitizing in the following way:

    ```{r results='asis'}
    tbl = data.frame(a = letters[1:10], b = 1:10 / 10)
    tbl$b = ifelse(tbl$b < 0.5, colFmt(tbl$b, "red"), colFmt(tbl$b, "green"))
    print(xtable(tbl), sanitize.text.function = identity)
    ```
    

    I then had to go and manually sanitize a few characters like % but at least \textcolor was correctly applied. Of course, this could be avoided by expanding your own sanitize function.

    0 讨论(0)
  • 2020-11-29 00:43

    An output-format agnostic solution would be to use the dedicated text_spec() function in the kableExtra package:

    Roses are `r kableExtra::text_spec("red", color = "red")`,
    
    violets are `r kableExtra::text_spec("blue", color = "blue")`
    
    0 讨论(0)
  • 2020-11-29 00:53

    I create a function like this:

    #Color Format
    colFmt = function(x,color){
      outputFormat = knitr::opts_knit$get("rmarkdown.pandoc.to")
      if(outputFormat == 'latex')
        paste("\\textcolor{",color,"}{",x,"}",sep="")
      else if(outputFormat == 'html')
        paste("<font color='",color,"'>",x,"</font>",sep="")
      else
        x
    }
    

    Then you can use it inline like this:`r colFmt("MY RED TEXT",'red')`, and colored text will be rendered regardless of whether working on latex or HTML document.

    0 讨论(0)
  • 2020-11-29 00:53

    For PDF and HTML, to get colored text which you can modify with markdown highlighting : see the rmarkdown book. Pandoc filter is the best choice.

    For Microsoft word, you have to create first a Template_MS.docx with custom styles. Warning: create different styles for coloring paragraphs (paragraph style) and for colorings few words (character style). It is an option when you made a new style.

    Add in th YAML:

    ---      
    output:   
     word_document:   
        reference_docx: Template_MS.docx   
    ---   
    

    And next:

    For <span custom-style="Character1">few words</span> of colored text.
    

    For paragraph.

    <div custom-style="Paragraph1">Paragraph of colored text. Blabla. Blabla.</div>   
    

    Nota Bene:
    + Do not use the same style for Paragraph and a few words, it is bugging.
    + If it is not working, check that your style is for paragraph ou character in MS.
    + If it is not working, install an updated version of pandoc.

    0 讨论(0)
提交回复
热议问题