Customization of the <code>highr</code> package

¶ # Customization of the `highr` package If you are not satisfied with the default syntax highlighting commands in the highr package, you can just use your own tags/commands. In this vignette, we show a brief example. |
¶ The default highlighting commands are stored in two internal data frames `cmd_latex` and `cmd_html`: |
    library(highr)
    highr:::cmd_latex
    ##                          cmd1 cmd2
    ## COMMENT              \\hlcom{    }
    ## FUNCTION             \\hlkwa{    }
    ## IF                   \\hlkwa{    }
    ## ELSE                 \\hlkwa{    }
    ## WHILE                \\hlkwa{    }
    ## FOR                  \\hlkwa{    }
    ## IN                   \\hlkwa{    }
    ## BREAK                \\hlkwa{    }
    ## REPEAT               \\hlkwa{    }
    ## NEXT                 \\hlkwa{    }
    ## NULL_CONST           \\hlkwa{    }
    ## LEFT_ASSIGN          \\hlkwb{    }
    ## EQ_ASSIGN            \\hlkwb{    }
    ## RIGHT_ASSIGN         \\hlkwb{    }
    ## SYMBOL_FORMALS       \\hlkwc{    }
    ## SYMBOL_SUB           \\hlkwc{    }
    ## SLOT                 \\hlkwc{    }
    ## SYMBOL_FUNCTION_CALL \\hlkwd{    }
    ## NUM_CONST            \\hlnum{    }
    ## '+'                  \\hlopt{    }
    ## '-'                  \\hlopt{    }
    ## '*'                  \\hlopt{    }
    ## '/'                  \\hlopt{    }
    ## '^'                  \\hlopt{    }
    ## '$'                  \\hlopt{    }
    ## '@'                  \\hlopt{    }
    ## ':'                  \\hlopt{    }
    ## '?'                  \\hlopt{    }
    ## '~'                  \\hlopt{    }
    ## '!'                  \\hlopt{    }
    ## SPECIAL              \\hlopt{    }
    ## GT                   \\hlopt{    }
    ## GE                   \\hlopt{    }
    ## LT                   \\hlopt{    }
    ## LE                   \\hlopt{    }
    ## EQ                   \\hlopt{    }
    ## NE                   \\hlopt{    }
    ## AND                  \\hlopt{    }
    ## AND2                 \\hlopt{    }
    ## OR                   \\hlopt{    }
    ## OR2                  \\hlopt{    }
    ## NS_GET               \\hlopt{    }
    ## NS_GET_INT           \\hlopt{    }
    ## STANDARD             \\hlstd{    }
    ## STR_CONST            \\hlstr{    }
¶ This data frame is passed to the `markup` argument in `hilight()`, so we are free to pass a modified version there. Suppose I want to use the command `\my<*>` instead of `\hl<*>`: |
    m = highr:::cmd_latex
    m[, 1] = sub("\\hl", "\\my", m[, 1], fixed = TRUE)
    head(m)
    ##              cmd1 cmd2
    ## COMMENT  \\mycom{    }
    ## FUNCTION \\mykwa{    }
    ## IF       \\mykwa{    }
    ## ELSE     \\mykwa{    }
    ## WHILE    \\mykwa{    }
    ## FOR      \\mykwa{    }
¶ Then |
    hilight("x = 1+1  # a comment")  # default markup
    ## [1] "\\hlstd{x} \\hlkwb{=} \\hlnum{1}\\hlopt{+}\\hlnum{1}  \\hlcom{# a comment}"
    hilight("x = 1+1  # a comment", markup = m)  # custom markup
    ## [1] "\\mystd{x} \\mykwb{=} \\mynum{1}\\myopt{+}\\mynum{1}  \\mycom{# a comment}"
¶ This allows one to use arbitrary commands around the text symbols in the R code. See https://github.com/yihui/highr/blob/master/R/highlight.R for how `cmd_latex` and `cmd_html` were generated in highr. |