Highlighting bash code with knitr / rmarkdown

I am trying to create an HTML report using RStudio, R Markdown and knitr. In the report, I would like to display the bash code. I do not want to run the code, but I would like it to be highlighted.

It was mentioned in another question , but the suggestion for me does not work. Here is what I have tried so far:

 --- title: "bash highlighting?" output: html_document --- ```{r, engine = 'bash', eval = FALSE} for foo in (ls bar) do echo $foo done ``` ```{bash, eval = FALSE} for foo in (ls bar) do echo $foo done ``` 

None of them give me highlighting in an HTML document. I know that this is possible because I remember that I saw him about a week ago, but I can no longer find him! Does anyone know how I can achieve this?

Thanks for reading,

Tom

Edit: I just found this answer that suggests using the following code block in .Rmd

 <link rel="stylesheet" href="http://yandex.st/highlightjs/7.3/styles/default.min.css"> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script> <script src="http://yandex.st/highlightjs/7.3/highlight.min.js"></script> <script> $(document).ready(function() { $('pre code').each(function(i, e) {hljs.highlightBlock(e)}); }); </script> 

This works for bash code in the document, but kills the allocation for R code!

 ## R version 3.2.0 (2015-04-16) ## Platform: x86_64-pc-linux-gnu (64-bit) ## Running under: Ubuntu 14.04.2 LTS ## ## locale: ## [1] LC_CTYPE=en_AU.UTF-8 LC_NUMERIC=C ## [3] LC_TIME=en_AU.UTF-8 LC_COLLATE=en_AU.UTF-8 ## [5] LC_MONETARY=en_AU.UTF-8 LC_MESSAGES=en_AU.UTF-8 ## [7] LC_PAPER=en_AU.UTF-8 LC_NAME=C ## [9] LC_ADDRESS=C LC_TELEPHONE=C ## [11] LC_MEASUREMENT=en_AU.UTF-8 LC_IDENTIFICATION=C ## ## attached base packages: ## [1] stats graphics grDevices utils datasets methods base ## ## loaded via a namespace (and not attached): ## [1] formatR_1.2 tools_3.2.0 htmltools_0.2.6 yaml_2.1.13 ## [5] rmarkdown_0.5.1 knitr_1.10 stringr_0.6.2 digest_0.6.8 ## [9] evaluate_0.7 
+7
r syntax-highlighting rstudio knitr r-markdown
source share
2 answers

The default syntax highlighting theme is not suitable for code snippets other than R, and you can use other themes, for example. pygments

 --- title: "Bash Highlighting" output: html_document: highlight: pygments --- ```{r, engine = 'bash', eval = FALSE} for foo in (ls bar) do echo $foo done ``` 
+19
source share

OK, I figured it out thanks to the comments. It seems to be RStudio , which does not play well with backlight. When I save the in.md file as in.md :

 --- title: "Bash Highlighting" output: html_document: keep_md: true --- ```{r, engine = 'bash', eval = FALSE} for foo in (ls bar) do echo $foo done ``` 

then convert to html using pandoc , using for example CSS from BioConductor:

 pandoc -s in.md \ -c https://hedgehog.fhcrc.org/bioconductor/branches/RELEASE_3_1/madman/Rpacks/BiocStyle/inst/resources/html/bioconductor.css \ -t html -o out.html 

I get beautiful code highlighting for R and bash .

Thanks!

+2
source share

All Articles