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

Inline code prints negative number without sign #2288

Closed
3 tasks done
fkohrt opened this issue Sep 19, 2023 · 3 comments
Closed
3 tasks done

Inline code prints negative number without sign #2288

fkohrt opened this issue Sep 19, 2023 · 3 comments
Assignees
Labels
bug Bugs

Comments

@fkohrt
Copy link
Contributor

fkohrt commented Sep 19, 2023

---
title: "Inline test"
output: html_document
---

The following prints a positive(!) number: `r (-1) * 10^7`

...leads to...

Screenshot 2023-09-19 at 15-50-31 Inline test

xfun::session_info('knitr')
R version 4.3.1 (2023-06-16)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.6 LTS


Locale:
  LC_CTYPE=C.UTF-8       LC_NUMERIC=C          
  LC_TIME=C.UTF-8        LC_COLLATE=C.UTF-8    
  LC_MONETARY=C.UTF-8    LC_MESSAGES=C.UTF-8   
  LC_PAPER=C.UTF-8       LC_NAME=C             
  LC_ADDRESS=C           LC_TELEPHONE=C        
  LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C   

time zone: UTC
tzcode source: system (glibc)

Package version:
  evaluate_0.21   graphics_4.3.1  grDevices_4.3.1
  highr_0.10      knitr_1.44      methods_4.3.1  
  stats_4.3.1     tools_4.3.1     utils_4.3.1    
  xfun_0.40       yaml_2.3.7     

By filing an issue to this repo, I promise that

  • I have fully read the issue guide at https://yihui.org/issue/.
  • I have provided the necessary information about my issue.
    • If I'm asking a question, I have already asked it on Stack Overflow or RStudio Community, waited for at least 24 hours, and included a link to my question there.
    • If I'm filing a bug report, I have included a minimal, self-contained, and reproducible example, and have also included xfun::session_info('knitr'). I have upgraded all my packages to their latest versions (e.g., R, RStudio, and R packages), and also tried the development version: remotes::install_github('yihui/knitr').
    • If I have posted the same issue elsewhere, I have also mentioned it in this issue.
  • I have learned the Github Markdown syntax, and formatted my issue correctly.

I understand that my issue may be closed if I don't fulfill my promises.

@cderv cderv added the bug Bugs label Oct 3, 2023
@cderv
Copy link
Collaborator

cderv commented Oct 3, 2023

I think the issue comes from this function we use for printing

knitr:::format_sci((-1)*10^7)
#> [1] "10^{7}"

We use this function in inline hooks

knitr/R/hooks-md.R

Lines 204 to 207 in 42f6b3a

inline = function(x) {
if (is_latex_output()) .inline.hook.tex(x) else {
.inline.hook(format_sci(x, if (length(pandoc_to()) == 1L) 'latex' else 'html'))
}

It seems there is several issues here:

  • @yihui why do we latex as format and not HTML here ? It seems we should use html.

     knitr:::format_sci_one((-1)*10^7, format = "latex")
     #> [1] "10^{7}"
     knitr:::format_sci_one((-1)*10^7, format = "html")
     #> [1] "10<sup>7</sup>"

    Not sure I understand why we do length(pandoc_to()) == 1L then latex

  • We still loose the minus sign does not seems right...

    knitr/R/utils.R

    Lines 206 to 207 in 42f6b3a

    b = round_digits(x / 10^lx)
    b[b %in% c(1, -1)] = ''

    if not -1 we keep it

     knitr:::format_sci_one(-2*10^7, 'html')
     #> [1] "-2 &times; 10<sup>7</sup>"

    shouldn't we have a special case for -1 ?

    I am in doubt with your implementation of scientific notation

    knitr/R/utils.R

    Lines 227 to 229 in 42f6b3a

    sci_notation = function(format, base, times, power) {
    sprintf(format, base, ifelse(base == '', '', times), power)
    }

    Should it be "-1 &times; 10<sup>7</sup>" or "-10<sup>7</sup>" ?

@yihui
Copy link
Owner

yihui commented Oct 5, 2023

Should be fixed now. Thanks for the report!

---
title: "Inline test"
output: html_document
---

The following prints a positive(!) number: $`r (-1) * 10^7`$

Note that you need $ $ around the number.

Copy link

github-actions bot commented Apr 3, 2024

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 3, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Bugs
Projects
None yet
Development

No branches or pull requests

3 participants