Skip to content

Switching back and forth from visual and editor mode makes number and reference equation broke. #8073

Closed

Description

@irpanch

System details

RStudio Edition : Desktop 
RStudio Version :  1.4.869
OS Version      :  Windows 10 x64 (build 18363)
R Version       :  4.0.2 (2020-06-22)

Steps to reproduce the problem

Creating a simple rmd file on source editor mode

---
title: "Generate number and reference equation"
output: 
  bookdown::html_document2
---

Sample equation from [Here](https://bookdown.org/yihui/bookdown/markdown-extensions-by-bookdown.html#equations)

\begin{equation} 
  f\left(k\right) = \binom{n}{k} p^k\left(1-p\right)^{n-k}
  (\#eq:binom)
\end{equation} 

As, we can see on equation \@ref(eq:binom) above..

It generates like this:
image

Describe the problem in detail

Now, when I first switch to visual rmarkdown editor, everything is fine. The code is as it is. Generate the same thing too.
The problem occurs when I switch back to source editor mode and switch again to visual mode.
Even when the code still the same, the output becomes an error in label and reference.

image

Notice the label number is disappear become label text, so the reference is in ??.

Then when I switch back to source editor mode, the code is changing (adding more backlash)

---
title: "Generate number and reference equation"
output: 
  bookdown::html_document2
---

Sample equation from [Here](https://bookdown.org/yihui/bookdown/markdown-extensions-by-bookdown.html#equations)

\begin{equation}
f\\left(k\\right) = \\binom{n}{k} p\^k\\left(1-p\\right)\^{n-k}
(\\\#eq:binom)
\\end{equation}

As, we can see on equation \@ref(eq:binom) above..

And it seems broke after that. I can't generate an equation with number and reference anymore in visual mode.
I try to switch back again to editor mode, fixing the equation, and generate it. It works. But when I switched again to visual mode, generate it. It won't work.

Describe the behavior you expected

The equation number and reference still persist even when we switch back and forth from visual and source editor mode.

  • I have read the guide for submitting good bug reports.
  • I have installed the latest version of RStudio, and confirmed that the issue still persists.
  • If I am reporting a RStudio crash, I have included a diagnostics report.
  • I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.

Activity

  1. changed the title [-]Switching back and forth from visual and editor mode makes number and reference equation broke.[/-] [+]Switching back and forth from visual and editor mode makes number and reference equation broke. [bug] [/+] on Oct 15, 2020
  2. changed the title [-]Switching back and forth from visual and editor mode makes number and reference equation broke. [bug] [/-] [+]Switching back and forth from visual and editor mode makes number and reference equation broke.[/+] on Oct 15, 2020
  3. added this to the v1.4 milestone on Oct 15, 2020
  4. added
    visual markdown
    on Oct 15, 2020
  5. self-assigned this
    on Oct 16, 2020
  6. jjallaire commented on Oct 19, 2020

    @jjallaire
    Member

    Fixed via 0a04f35 (will appear in the next daily build, anything >= 1.4.950)

    The issue was that pandoc was treating a multi-line \begin{equation} block as a single raw inline token embedded in a paragraph. Since the editor doesn't have the concept of newlines embedded in a raw-inline mark, the mark was broken after the first line (and subsequently escaped when round-tripping).

    The fix is to treat a multi-line raw inline as a raw text block.

    @irpanch Your code will now work correctly, however note that you might want to convert to using the $...$ or $$...$$ syntax for LaTeX equations, as then pandoc and the visual editor will "know" that it's math and do inline preview, etc.

    It's possible I've misunderstood the use case / desired outcome here as I'm not myself a LaTeX math user -- in that case pls elucidate and we'll find the right fix.

    cc @skeydan and @cderv Does this fix the issues you were seeing? (note that this fix won't appear until the next daily build)

    @adamconroy Saw that you had self-assigned this. Ping me if you want to discuss the fix here and any alternatives you may have been considering.

  7. adamconroy commented on Oct 19, 2020

    @adamconroy
    Contributor

    Verified as of commit 0a04f35 that multiline equations are now treated as raw blocks.

  8. skeydan commented on Oct 20, 2020

    @skeydan

    thanks @jjallaire - it's fixed!

  9. setgree commented on Mar 28, 2024

    @setgree

    Hi there, I am encountering a similar issue when trying to reference R objects inline. Here's what I have (line 533 of this document)

    Our meta-analysis of r num_studies policy-relevant interventions yields an overall meta-analytic effect of rmain_result$Delta(se =rmain_result$se), p =r main_result$pval`.

    switch to visual, and then back to source, and now I have

    Our meta-analysis of r num_studies policy-relevant interventions yields an overall meta-analytic effect of rmain_result$Delta(se =rmain_result$se), p =r main_result$pval`.

    Note the extra slashes.

    Shall I open a new issue? It looks like the same issue as this one

    (I also get a lot of little red dot characters when I switch back and forth where there used to be spaces)

    > sessionInfo()
    R version 4.3.3 (2024-02-29)
    Platform: x86_64-apple-darwin20 (64-bit)
    Running under: macOS Sonoma 14.2.1
    
    Matrix products: default
    BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib 
    LAPACK: /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0
    
    locale:
    [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
    
    time zone: America/New_York
    tzcode source: internal
    
    attached base packages:
    [1] grid      stats     graphics  grDevices utils     datasets  methods   base     
    
    other attached packages:
     [1] ggplot2_3.5.0       sessioninfo_1.2.2   knitr_1.45          metafor_4.4-0       numDeriv_2016.8-1.1
     [6] metadat_1.2-0       Matrix_1.6-5        purrr_1.0.2         stringr_1.5.1       dplyr_1.1.4        
    [11] ggplot_0.4.2        RColorBrewer_1.1-3  reshape_0.8.9      
    
    loaded via a namespace (and not attached):
     [1] sass_0.4.8        utf8_1.2.4        generics_0.1.3    stringi_1.8.3     lattice_0.22-5   
     [6] digest_0.6.34     magrittr_2.0.3    evaluate_0.23     bookdown_0.38     papaja_0.1.2.9000
    [11] fastmap_1.1.1     rprojroot_2.0.4   plyr_1.8.9        jsonlite_1.8.8    tinytex_0.49     
    [16] tinylabels_0.2.4  fansi_1.0.6       scales_1.3.0      jquerylib_0.1.4   cli_3.6.2        
    [21] rlang_1.1.3       munsell_0.5.0     withr_3.0.0       cachem_1.0.8      yaml_2.3.8       
    [26] tools_4.3.3       colorspace_2.1-0  mathjaxr_1.6-0    here_1.0.1        assertthat_0.2.1 
    [31] vctrs_0.6.5       R6_2.5.1          lifecycle_1.0.4   git2r_0.33.0      rmdfiltr_0.1.3   
    [36] pkgconfig_2.0.3   pillar_1.9.0      bslib_0.6.1       gtable_0.3.4      glue_1.7.0       
    [41] Rcpp_1.0.12       highr_0.10        xfun_0.42         tibble_3.2.1      tidyselect_1.2.0 
    [46] rstudioapi_0.15.0 farver_2.1.1      nlme_3.1-164      htmltools_0.5.7   labeling_0.4.3   
    [51] rmarkdown_2.25    compiler_4.3.3   
    ```
    
  10. ronblum commented on Mar 29, 2024

    @ronblum
    Contributor

    @setgree Thank you for the info! Yeah, can you please open a new issue for it? Thanks!

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

Metadata

Metadata

Assignees

Labels

visual markdown

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions