[Rd] Rd2txt: frmt() overestimates width of multi-line arguments

2021-07-31 Thread Ivan Krylov
Hi!

I wanted to use the second argument of \figure{}{} to provide a
pseudo-graphic rendition of the image and noticed that it's not centered
despite the intent to do so is visible in the code.

I traced it to a line in the frmt() function. It seems to me that
frmt() doesn't expect its input to consist of more than one line
because it sums the lengths of its components. A similar problem could
be observed in a \deqn{} consisting of multiple relatively short lines:
it would not be centered despite the available space.

A fix would be to change the width calculation:

Index: src/library/tools/R/Rd2txt.R
===
--- src/library/tools/R/Rd2txt.R(revision 80675)
+++ src/library/tools/R/Rd2txt.R(working copy)
@@ -356,7 +356,7 @@
 ## Use display widths as used by cat not print.
 frmt <- function(x, justify="left", width = 0L) {
 justify <- match.arg(justify, c("left", "right", "centre", "none"))
-w <- sum(nchar(x, "width")) # copes with 0-length x
+w <- max(nchar(c("", x), "width")) # copes with 0-length x
 if(w < width && justify != "none") {
 excess <- width - w
 left <- right <- 0L

I would like to clarify: should such small problems be raised first in
R-devel for discussion (e.g. whether it should be considered a bug)? Or
should I just file a bug in the Bugzilla and attach a patch there?

-- 
Best regards,
Ivan

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] \Sexpr[results=hide] produces \verb{ newlines }

2021-07-31 Thread Martin Maechler
> Ivan Krylov 
> on Thu, 29 Jul 2021 17:48:38 +0200 writes:

> Hello R-devel!
> 
> Here's an Rd file that produces a large empty area when converted to
> HTML:
> 
> \name{repro}
> \title{title}
> \description{description}
> \details{
>   Hello
>   \Sexpr[stage=build,results=hide]{
> invisible(NULL)
> invisible(NULL)
> invisible(NULL)
> invisible(NULL)
> invisible(NULL)
> invisible(NULL)
> invisible(NULL)
> invisible(NULL)
> invisible(NULL)
> invisible(NULL)
> invisible(NULL)
> "" # workaround: remove results=hide and use the return value
>   }
> }
> 
> This seems to happen because \Sexpr gets expanded to \verb{ as many
> newlines as there were code lines } by processRdChunk, by first storing
> a newline for each line of the code:
> 
> https://github.com/wch/r-source/blob/d7a4ed9aaeee1f57c3c165aefa08b8d69dfe59fa/src/library/tools/R/RdConv2.R#L257
> 
> ...and then the newlines get translated to \verb because res is
> not empty:
> 
> https://github.com/wch/r-source/blob/d7a4ed9aaeee1f57c3c165aefa08b8d69dfe59fa/src/library/tools/R/RdConv2.R#L332
> 
> As long as Rd above doesn't stem from my misuse of \Sexpr, I would like
> to propose the following patch, which seems to fix the problem:
> 
> Index: src/library/tools/R/RdConv2.R
> ===
> --- src/library/tools/R/RdConv2.R (revision 80675)
> +++ src/library/tools/R/RdConv2.R (working copy)
> @@ -329,6 +329,8 @@
>   }
>   } else if (options$results == "text")
>   res <- tagged(err, "TEXT")
> + else if (options$results == "hide")
> + res <- tagged("", "COMMENT")
>   else if (length(res)) {
>   res <- lapply(as.list(res), function(x) tagged(x, "VERB"))
>   res <- tagged(res, "\\verb")
> 
> There are probably other ways of fixing this problem, e.g. by only
> populating res if options$results != "hide" or only appending newlines
> if res is non-empty.

Thank you, Ivan, for the example and patch,

I have implemented a version of your patch in my local copy of
R-devel and tested your example, also with  Rd2latex() ..
interestingly   Rd2txt()  does not produce the extra new lines
even without your patch.

I plan to commit your proposal after the weekend unless has 
reasons against that.

Best regards,
Martin

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel