---
title: Pretty printing
subtitle: Print highlighted source code of a function
date: '2012-02-11'
slug: pretty
---

In the **knitr** mailing list there was a  [question](https://groups.google.com/d/topic/knitr/ZTfm9fM6fQs/discussion) on how to print the highlighted source code of functions.

Of course it is fairly easy to print the source code of a function, as long as it is available to the user -- just write the name of the function, and R will print its source when this name is evaluated, but the problem is the printed result is treated as text output from a chunk, so it is put in the `verbatim` environment, and nothing is highlighted.

Below is a complete demo showing how to use a chunk hook extract the source code of a function using `dump()` and insert it into **knitr**.

<script src="https://gist.github.com/1805862.js"> </script>
