r-lib / roxygen2

Generate R package documentation from inline R comments
https://roxygen2.r-lib.org
Other
591 stars 232 forks source link

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

Open allenzhuaz opened 4 years ago

allenzhuaz commented 4 years ago

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?
gaborcsardi commented 4 years ago

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

allenzhuaz commented 4 years ago

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)} ")

hadley commented 4 years ago

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 commented 4 years ago

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.

gaborcsardi commented 4 years ago

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 commented 4 years ago

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

allenzhuaz commented 4 years ago

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?

gaborcsardi commented 4 years ago

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