roxygen2 icon indicating copy to clipboard operation
roxygen2 copied to clipboard

markdown support does not work within \if{latex}{} condition?

Open allenzhuaz opened this issue 5 years ago • 8 comments

I really like the roxygen2 build markdown support with option Roxygen: list(markdown = TRUE) or @md, but when I wrote some documentation within the if{latex}{} condition, the markdown language did not work within the latex condition, even if I set the markdown support option.

For example, I have to write multiple items as: #' \if{latex}{ #' Example: #' \itemize{ #' \item 1st item #' \item 2nd item #' \item 3rd item }}

rather than #' \if{latex}{ #' Example: #' * 1st item #' * 2nd item #' * 3rd item }

Similarly, for inserting a table, seemingly I am only allowed to write: #' \if{latex}{ #' \tabular{ll}{ #' a \tab b \cr #' c \tab d #' }} but not allowed to use markdown, like: #' \if{latex}{ #' | a | b | #' |----|----| #' | c | d | #' }

Can the markdown support also be implemented within LaTeX condition?

allenzhuaz avatar Feb 24 '20 05:02 allenzhuaz

Maybe we can fix this. Can you provide a reproducible examples please? See https://github.com/r-lib/roxygen2/blob/master/.github/SUPPORT.md#making-a-reprex

gaborcsardi avatar Feb 24 '20 13:02 gaborcsardi

Please see the reproducible example:

library(roxygen2) roc_proc_text(rd_roclet(), " #' @section Test #' \if{latex}{ #' List example: #' * 1st item #' * 2nd item #' * 3rd item #' Table example: #' | a | b | #' |---|---| #' | c | d | #' } #' @export fun <- function(){return(NULL)} ")

allenzhuaz avatar Feb 24 '20 20:02 allenzhuaz

Actual reprex:

library(roxygen2)
roc_proc_text(rd_roclet(), "
#' Title
#' @section Test:
#' \\if{latex}{
#' List example:
#' * 1st item
#' * 2nd item
#' * 3rd item
#' Table example:
#' | a | b |
#' |---|---|
#' | c | d |
#' }
#' @export
fun <- function(){return(NULL)}
")[[1]]
#> % Generated by roxygen2: do not edit by hand
#> % Please edit documentation in ./<text>
#> \name{fun}
#> \alias{fun}
#> \title{Title}
#> \usage{
#> fun()
#> }
#> \description{
#> Title
#> }
#> \section{Test}{
#> 
#> \if{latex}{
#> List example:
#> * 1st item
#> * 2nd item
#> * 3rd item
#> Table example:
#> | a | b |
#> |---|---|
#> | c | d |
#> }
#> }

Created on 2020-03-10 by the reprex package (v0.3.0)

hadley avatar Mar 10 '20 18:03 hadley

I think getting this to work is going to be hard, because we'd have to parse the Rd in order to figure out which bits should be transformed.

hadley avatar Mar 10 '20 18:03 hadley

According to my comments at https://github.com/r-lib/roxygen2/blob/357d856f6ccdb96b7a95b42bf747cf10e2a26fb3/R/markdown-escaping.R#L13-L16 this should work.

But it does not. It is probably not that hard, because we already escape these macros, so they are already parsed.

gaborcsardi avatar Mar 10 '20 18:03 gaborcsardi

Yeah, so I think for this we need to add another mode to the roxygen_parse_tag() C function, to only parse the first argument of \if or \ifelse and leave the rest in place.

https://github.com/r-lib/roxygen2/blob/e86cebf914331f580cc96c3f26a6cd203f82b1be/src/isComplete.cpp#L14

gaborcsardi avatar Mar 10 '20 18:03 gaborcsardi

Yeah, so I think for this we need to add another mode to the roxygen_parse_tag() C function, to only parse the first argument of \if or \ifelse and leave the rest in place.

https://github.com/r-lib/roxygen2/blob/e86cebf914331f580cc96c3f26a6cd203f82b1be/src/isComplete.cpp#L14

Thanks for looking into this feature, still on demand for the feature in our development, wondering whether it will be implemented in the next released version?

allenzhuaz avatar Jun 08 '20 15:06 allenzhuaz

We'll look at this issue before the next release, yes.

gaborcsardi avatar Jun 08 '20 15:06 gaborcsardi