Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

formatting in-line code #5

Open
topepo opened this issue Feb 4, 2020 · 2 comments
Open

formatting in-line code #5

topepo opened this issue Feb 4, 2020 · 2 comments

Comments

@topepo
Copy link
Member

topepo commented Feb 4, 2020

This SO question doesn't appear to work anymore. I tried this but errors were produced:

hook_inline <- knitr::knit_hooks$get('inline')
knitr::knit_hooks$set(
  inline = function(x) {
    if (is.character(x) & knitr::is_html_output()) {
      highr::hi_html()
    } else {
      hook_inline
    }
  }
)
@topepo
Copy link
Member Author

topepo commented Jul 19, 2020

From this SO post:

I think this problem is possible to solve. Which output formats do you want the syntax highlighting to work with? I haven't tested it, but I guess HTML might be a little easier. LaTeX might be trickier, but shouldn't be terribly hard. – Yihui Xie Jun 29 at 4:58

@cderv
Copy link
Collaborator

cderv commented Feb 4, 2022

Sorry for the delay on this. It was always through my list so going back to it now as doing some cleaning in relation rmarkdown and bookdown

but errors were produced:

I am not sure about the error you had.

Trying your demo repo (thanks) seems to work with these adjusments:

  • The hook would be this one
```{r inline, include = FALSE}
local({
  hook_inline <- knitr::knit_hooks$get('inline')
  knitr::knit_hooks$set(
    inline = function(x) {
      if (is.character(x) & knitr::is_html_output()) {
        paste0(
          "<code>",
          highr::hi_html(x),
          "</code>"
        )
      } else {
        hook_inline(x)
      }
    }
  )
})
```

adding <code> is not needed if you write in the Rmd something like `` `r 'lm(y ~ x, data = dat)'` ``

```{css, echo = FALSE}
.hl.num { color: #fd971f; }
.hl.esc { color: #fd971f; }
.hl.ipl { color: #cc6633; }
.hl.str { color: #a6e22e; }
.hl.ppc { color: #ae81ff; }
.hl.pps { color: #a1efe4; }
.hl.com { color: #a59f85; font-style: italic; }
.hl.slc { color: #a59f85; font-style: italic; }
.hl.lin { color: #75715e; }
.hl.opt { color: #f8f8f2; }
.hl.kwa { color: #f92672; }
.hl.kwb { color: #f4bf75; }
.hl.kwc { color: #66d9ef; }
.hl.kwd { color: #ae81ff; }  
```

I get this in the output
image

So there would be some adjustment to make to get that working.

Also using inline hook would mean it would apply to any output, including those that are not supposed to be show as R code chunk in the output if I am correct. Ex:

There is `r nrow(data)` line in the table

I believe nowadays downlit would simplify this - or could if it can't already.

is this still something you want to do ?

downlit support for gitbook() is still a thing to do : rstudio/bookdown#844

At last, regarding bookdown project, the hook being applied may depend on where it is set according to the settings used (new_session: false specifically). This part could be related directly to #3

Anyway, it was a long overdue digging through. Sorry for the wait.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants