2

I am using knitr for a report wherein I have a lot of inline output text, mostly numeric values, using \Sexpr{}. I want to highlight All these inline outputs in my generated pdf.

Example code:

\documentclass[12pt]{article}
\begin{document}

<<echo=FALSE, include=FALSE>>=
N <- 100 # Total
N_f <- 60 # Women
@

There were \Sexpr{N} people in the company, \Sexpr{N_f} women and \Sexpr{N - N_f} men.

\end{document}

Hence, in the output all the number should be highlighted, i.e. with a shaded background (similar to using with \hl{} with the \usepackage{soul}).

It seems to me that the solution would use one of the inline output hooks. Another possibility might be to write a LaTeX function which search all the \Sexpr{...} expressions in the entire document and highlights them in the generated pdf. I am still learning and can not figure out how to implement these.

Thanks for any help or hints.

Note: The knitr page by yihui talks about manipulation of the numeric value (scientific notation, digits after decimal points) which I have got covered.

nilklin
  • 23
  • 3
  • Output hooks are exactly the right approach. What have you tried so far to use them? Using LaTeX to manipulate `\Sexpr{}` cannot work because by the time TEX "sees" your document `knitr` already processed all the `\Sexpr` commands, replacing them by their output. – CL. Jan 18 '16 at 12:44
  • @ CL. I had used the output hooks for manipulating the numeric value itself, for e.g., adding a custom thousand separator. What I was missing was the use of `sprintf`. – nilklin Jan 21 '16 at 16:31
  • I see. Yes, `sprintf` is very useful, but if you are completely new to string manipulation/concatenation, also take a look at `paste`. – CL. Jan 21 '16 at 16:33

1 Answers1

2

The output hook inline can be used to style output from \Sexpr{}. This is as simple as

knit_hooks$set(inline = function(x) { sprintf("\\textbf{%s}", x)})

Just define an arbitrary function that takes an argument x and returns the string to be printed. In this example I used \textbf to make the output bold, but this can be extended to any LaTeX commands.

In this answer, Yihui suggests an improvement that still takes the default inline hook into account. This ensures rounding as usually performed by the default hook:

hook_inline <- knit_hooks$get('inline')
knit_hooks$set(inline = function(x) { sprintf("\\textbf{%s}", hook_inline(x))})
Community
  • 1
  • 1
CL.
  • 14,577
  • 5
  • 46
  • 73
  • Thanks, this works. For the sake of completion, the text highlighting works with: `hook_inline <- knit_hooks$get('inline') knit_hooks$set(inline = function(x) {sprintf("\\hl{%s}", hook_inline(x))})` with `\usepackage{soul}` in the preamble for `\hl`. – nilklin Jan 21 '16 at 16:21
  • I am new in this type of context. So, I can't understand how to fit this two lines of code for an inline command. For example, for this line, `There were \Sexpr{N} people in the company,` where to write hook_inline codes? – user1896653 Apr 16 '20 at 11:37
  • @user1896653 The code goes into an code chunk, as shown in the question: between `<<>>=` and `@`. See, for example, here: https://yihui.org/knitr/demo/minimal/ – CL. Apr 16 '20 at 13:15