Skip to content

Backslashes are added to citations when using the RMarkdown Visual Editor, resulting in the incorrect rendering of documents  #10075

Closed

Description

@pedrohbraga

System details

RStudio Edition : Desktop
RStudio Version :  2021.09.0 Build 351 - Ghost Orchid" Release (077589bc, 2021-09-20) for Windows
OS Version      :  Windows 10 x64 (build 22000)
R Version       : R version 4.1.2 (2021-11-01)

Steps to reproduce the problem

  1. Create and save the following .Rmd document within the desired directory in your system:
---
title: "Reproduce issue with backslashes in citations"
author: "Pedro Henrique P. Braga"
date: "11/11/2021"
bibliography: references.bib  
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>. 
  1. Create a references.bib file containing the code chunk below and save it to the same directory as the .Rmd file:
@Manual{R-rmarkdown,
  title = {rmarkdown: Dynamic Documents for R},
  author = {JJ Allaire and Yihui Xie and Jonathan McPherson and Javier Luraschi and Kevin Ushey and Aron Atkins and Hadley Wickham and Joe Cheng and Winston Chang and Richard Iannone},
  year = {2020},
  note = {R package version 2.6},
  url = {https://github.com/rstudio/rmarkdown},
}
  1. Open the .Rmd file in RStudio and switch from the script-based view to the "Visual Editor" (shortcut Ctrl or Command+Shift+F4);

  2. Add a citation by typing [@R-rmarkdown] after the first paragraph in the prose written below the YAML;

image

  1. Knit the document.

  2. Disable the "Visual Editor" (shortcut Ctrl or Command+Shift+F4).

Describe the problem in detail

Backslashes are added to citations created in the "Visual Editor" mode. They are apparent when switching back from the Visual Editor mode, and this results in the incorrect rendering of citations and references. See the output of the document below:

image

image

Describe the behaviour you expected

The expectation is that citations added when using the "Visual Editor" mode are reflected correctly in the code-based view of the .Rmd script and that citations and references are rendered correctly within the PDF or HTML document.

  • 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 an 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. pedrohbraga commented on Nov 11, 2021

    @pedrohbraga
    Author

    I am also adding another example, non-reproducible, which also demonstrates this same issue I am having, at a larger scale.

    1. Here is the text I am writing in the Visual Editor:

    image

    1. Here is what appears when I deactivate the Visual Editor:

    image

    1. Here is the rendered document:

    image

    Note how citations are not correctly rendered and how backslashes are added to the citations within the .Rmd document.

  2. changed the title [-]Backslashes are added to citations when using the RMarkdown Visual Editor and result in the incorrect rendering of documents [/-] [+]Backslashes are added to citations when using the RMarkdown Visual Editor, resulting in the incorrect rendering of documents [/+] on Nov 11, 2021
  3. Dijkie85 commented on Nov 12, 2021

    @Dijkie85

    Not sure if it's related to this, but I found that working with multiple child Rmd's will result in duplicate references between documents, because the visual editor numbers each reference in each document without regard for previous children.
    So, even if the first reference in 2.Rmd is "[^2-1]" it will be converted to "[^1"] and overlap with references from 1.Rmd.

  4. ronblum commented on Nov 12, 2021

    @ronblum
    Contributor

    @pedrohbraga Thank you for raising this! We've certain cases in which the backslashes are added incorrectly. However, I can't reproduce this problem: It works for me in both

    • RStudio Desktop 2021.09.0+351 on Windows 10
    • RStudio Desktop 2021.09.1+371 on MacOS 12

    Did this problem occur for you in previous versions of RStudio, such as 1.4.1717 or earlier?

    @Dijkie85 The issue with citations in children files appears to be a different problem. Can you file a new issue for this? It would also be helpful to include sample files, both the parent and child files. Thanks!

  5. added
    info needed
    visual markdown
    on Nov 12, 2021
  6. balthasars commented on Nov 12, 2021

    @balthasars

    I have the same issue in RStudio version 2021.9.1.372 on macOS 11.6. @ronblum is there a way of switching into the visual mode reproducible?
    In my case, somewhat strangely, not all citations in the document end up being formatted like this, however. Some retain the correct formatting

  7. Dijkie85 commented on Nov 12, 2021

    @Dijkie85

    @ronblum Will do. Thanks!

    edit: opened #10078

  8. ronblum commented on Nov 12, 2021

    @ronblum
    Contributor

    @balthasars Would you be willing to provide an example of a file in which some citations keep the correct formatting and others don't? I can try it and see if I see the same issue.

  9. pedrohbraga commented on Nov 15, 2021

    @pedrohbraga
    Author

    @ronblum and others,

    Here follows another example that should be reproducible in your environment and that (hopefully) brings to the same outcome I am having here.

    1. Save the content below to an .Rmd file:
    ---
    output:
      bookdown::pdf_document2:
        latex_engine: xelatex
        toc: false
        self_contained: true
      bookdown::html_document2:
        default
    title: "Little reproducible example"
    author: "Pedro Henrique P. Braga"
    date: "`r Sys.Date()`"
    abstract: "Answering how quickly species climatic niches evolve is essential to understand biogeographical patterns and to predict how species will respond to climate change."
    geometry: margin = 1in
    fontsize: 11pt
    spacing: double
    documentclass: article
    mainfont: "Times New Roman"
    editor_options:
      markdown:
        wrap: sentence
    bibliography: main_references.bib
    csl: https://raw.githubusercontent.com/marlonecobos/kuenm/master/ecography.csl
    link-citations: yes
    header-includes:
      - \usepackage[left]{lineno}
      - \linenumbers
      - \usepackage{setspace}\doublespacing
      - \setlength{\parindent}{4em}
      - \setlength{\parskip}{0em}
    ---
    
    ```{r setup, include=FALSE}
    library(bookdown)
    
    knitr::opts_chunk$set(tidy = FALSE, 
                          comment = "#",
                          cache = TRUE,
                          warning = FALSE,
                          message = FALSE,
                          fig.width = 6, 
                          fig.height = 6
    )
    
    options(htmltools.dir.version = FALSE)
    ```
    
    # Introduction
    
    Every species has a realized climatic niche (henceforward, "climatic niche"), which is determined by their responses and underlying tolerances to the set of long-term meteorological conditions that allow them to occur when and where they are [@holt2009p; @hutchinson1957cshsqb; @soberon2017po]. Hence, answering questions such as "*What determines species climatic affinities and limitations?* [@carscadden2020qrb]", "*How climatic niches contribute to the formation and maintenance of biodiversity patterns?* [@cooney2016jae; @kozak2010el]", "*Do certain climatic niche characteristics make species more vulnerable to the ongoing and projected climate change?* [@rinnan2019e; @smith2020jb]" , "*Can rapid climatic niche shifts attenuate climate change driven extinctions?* [@quintero2013el; @jezkova2016pbs]", "*Is the evolution of climatic niches similar across groups?* [@liu2020]" has been of critical interest for biogeographers, ecologists and conservation biologists.
    
    # References
    
    1. Save the content below to a main_references.bib file, within the same directory as the .Rmd file :
    @article{holt2009p,
    	title = {Bringing the Hutchinsonian niche into the 21st century: Ecological and evolutionary perspectives},
    	author = {{Holt}, {Robert D.}},
    	year = {2009},
    	month = {11},
    	date = {2009-11-17},
    	journal = {Proceedings of the National Academy of Sciences},
    	pages = {19659--19665},
    	volume = {106},
    	number = {Supplement 2},
    	doi = {10.1073/pnas.0905137106},
    	url = {https://www.pnas.org/content/106/Supplement_2/19659},
    	note = {Publisher: National Academy of Sciences
    Section: Colloquium Paper
    PMID: 19903876},
    	langid = {en}
    }
    
    @article{hutchinson1957cshsqb,
    	title = {Concluding Remarks},
    	author = {{Hutchinson}, {G. Evelyn}},
    	year = {1957},
    	month = {01},
    	date = {1957-01-01},
    	journal = {Cold Spring Harbor Symposia on Quantitative Biology},
    	pages = {415--427},
    	volume = {22},
    	doi = {10.1101/SQB.1957.022.01.039},
    	url = {http://symposium.cshlp.org/content/22/415},
    	note = {Publisher: Cold Spring Harbor Laboratory Press},
    	langid = {en}
    }
    
    @article{soberon2017po,
    	title = {Are fundamental niches larger than the realized? Testing a 50-year-old prediction by Hutchinson},
    	author = {{Soberón}, {J.} and {Arroyo-Peña}, {B.}},
    	year = {2017},
    	month = {04},
    	date = {2017-04-12},
    	journal = {PLOS ONE},
    	pages = {e0175138},
    	volume = {12},
    	number = {4},
    	doi = {10.1371/journal.pone.0175138},
    	url = {https://journals.plos.org/plosone/article?id=10.1371/journal.pone.0175138},
    	note = {Publisher: Public Library of Science},
    	langid = {en}
    }
    
    @article{liu2020,
    	title = {Climatic-niche evolution follows similar rules in plants and animals},
    	author = {{Liu}, {Hui} and {Ye}, {Qing} and {Wiens}, {John J.}},
    	year = {2020},
    	month = {05},
    	date = {2020-05},
    	journal = {Nature Ecology & Evolution},
    	pages = {753--763},
    	volume = {4},
    	number = {5},
    	doi = {10.1038/s41559-020-1158-x},
    	url = {http://www.nature.com/articles/s41559-020-1158-x},
    	note = {Bandiera{\_}abtest: a
    Cg{\_}type: Nature Research Journals
    Number: 5
    Primary{\_}atype: Research
    Publisher: Nature Publishing Group
    Subject{\_}term: Ecology;Evolution
    Subject{\_}term{\_}id: ecology;evolution},
    	langid = {en}
    }
    
    @article{rangel2018s,
    	title = {Modeling the ecology and evolution of biodiversity: Biogeographical cradles, museums, and graves},
    	author = {{Rangel}, {Thiago F.} and {Edwards}, {Neil R.} and {Holden}, {Philip B.} and {Diniz-Filho}, {José Alexandre F.} and {Gosling}, {William D.} and {Coelho}, {Marco Túlio P.} and {Cassemiro}, {Fernanda A. S.} and {Rahbek}, {Carsten} and {Colwell}, {Robert K.}},
    	year = {2018},
    	month = {07},
    	date = {2018-07-20},
    	journal = {Science},
    	pages = {eaar5452},
    	volume = {361},
    	number = {6399},
    	doi = {10.1126/science.aar5452},
    	url = {http://www.science.org/doi/10.1126/science.aar5452},
    	note = {Publisher: American Association for the Advancement of Science}
    }
    
    @article{dimarco2021e,
    	title = {Drivers of change in the realised climatic niche of terrestrial mammals},
    	author = {{Di Marco}, {Moreno} and {Pacifici}, {Michela} and {Maiorano}, {Luigi} and {Rondinini}, {Carlo}},
    	year = {2021},
    	date = {2021},
    	journal = {Ecography},
    	pages = {1180--1190},
    	volume = {44},
    	number = {8},
    	doi = {10.1111/ecog.05414},
    	url = {http://onlinelibrary.wiley.com/doi/abs/10.1111/ecog.05414},
    	note = {{\_}eprint: https://onlinelibrary.wiley.com/doi/pdf/10.1111/ecog.05414},
    	langid = {en}
    }
    
    @article{liu2017jb,
    	title = {Realized climatic niches are conserved along maximum temperatures among herpetofaunal invaders},
    	author = {{Liu}, {Xuan} and {Petitpierre}, {Blaise} and {Broennimann}, {Olivier} and {Li}, {Xianping} and {Guisan}, {Antoine} and {Li}, {Yiming}},
    	year = {2017},
    	date = {2017},
    	journal = {Journal of Biogeography},
    	pages = {111--121},
    	volume = {44},
    	number = {1},
    	doi = {10.1111/jbi.12808},
    	url = {http://onlinelibrary.wiley.com/doi/abs/10.1111/jbi.12808},
    	note = {{\_}eprint: https://onlinelibrary.wiley.com/doi/pdf/10.1111/jbi.12808},
    	langid = {en}
    }
    
    
    @article{quintero2013el,
    	title = {Rates of projected climate change dramatically exceed past rates of climatic niche evolution among vertebrate species},
    	author = {Quintero, Ignacio and Wiens, John J.},
    	year = {2013},
    	month = {08},
    	date = {2013-08},
    	journal = {Ecology Letters},
    	pages = {1095--1103},
    	volume = {16},
    	number = {8},
    	doi = {10.1111/ele.12144},
    	note = {PMID: 23800223},
    	langid = {eng}
    }
    
    @article{carscadden2020qrb,
    	title = {Niche Breadth: Causes and Consequences for Ecology, Evolution, and Conservation},
    	author = {Carscadden, Kelly A. and Emery, Nancy C. and Arnillas, Carlos A. and Cadotte, Marc W. and Afkhami, Michelle E. and Gravel, Dominique and Livingstone, Stuart W. and Wiens, John J.},
    	year = {2020},
    	month = {09},
    	date = {2020-09-01},
    	journal = {The Quarterly Review of Biology},
    	pages = {179--214},
    	volume = {95},
    	number = {3},
    	doi = {10.1086/710388},
    	url = {http://www.journals.uchicago.edu/doi/10.1086/710388},
    	note = {Publisher: The University of Chicago Press}
    }
    
    @article{cooney2016jae,
    	title = {Widespread correlations between climatic niche evolution and species diversification in birds},
    	author = {Cooney, Christopher R. and Seddon, Nathalie and Tobias, Joseph A.},
    	year = {2016},
    	date = {2016},
    	journal = {Journal of Animal Ecology},
    	pages = {869--878},
    	volume = {85},
    	number = {4},
    	doi = {10.1111/1365-2656.12530},
    	url = {http://onlinelibrary.wiley.com/doi/abs/10.1111/1365-2656.12530},
    	note = {{\_}eprint: https://besjournals.onlinelibrary.wiley.com/doi/pdf/10.1111/1365-2656.12530},
    	langid = {en}
    }
    
    @article{jezkova2016pbs,
    	title = {Rates of change in climatic niches in plant and animal populations are much slower than projected climate change},
    	author = {Jezkova, Tereza and Wiens, John J.},
    	year = {2016},
    	month = {11},
    	date = {2016-11-30},
    	journal = {Proceedings of the Royal Society B: Biological Sciences},
    	pages = {20162104},
    	volume = {283},
    	number = {1843},
    	doi = {10.1098/rspb.2016.2104},
    	url = {https://www.ncbi.nlm.nih.gov/pmc/articles/PMC5136599/},
    	note = {PMID: 27881748
    PMCID: PMC5136599}
    }
    
    @article{rinnan2019e,
    	title = {Climate-niche factor analysis: a spatial approach to quantifying species vulnerability to climate change},
    	author = {Rinnan, D. Scott and Lawler, Joshua},
    	year = {2019},
    	date = {2019},
    	journal = {Ecography},
    	pages = {1494--1503},
    	volume = {42},
    	number = {9},
    	doi = {10.1111/ecog.03937},
    	url = {http://onlinelibrary.wiley.com/doi/abs/10.1111/ecog.03937},
    	note = {{\_}eprint: https://onlinelibrary.wiley.com/doi/pdf/10.1111/ecog.03937},
    	langid = {en}
    }
    
    @article{kozak2010el,
    	title = {Accelerated rates of climatic-niche evolution underlie rapid species diversification},
    	author = {Kozak, Kenneth H. and Wiens, John J.},
    	year = {2010},
    	date = {2010},
    	journal = {Ecology Letters},
    	pages = {1378--1389},
    	volume = {13},
    	number = {11},
    	doi = {10.1111/j.1461-0248.2010.01530.x},
    	url = {http://onlinelibrary.wiley.com/doi/abs/10.1111/j.1461-0248.2010.01530.x},
    	note = {{\_}eprint: https://onlinelibrary.wiley.com/doi/pdf/10.1111/j.1461-0248.2010.01530.x},
    	langid = {en}
    }
    
    @article{castro-insua2018sr,
    	title = {Climatic niche divergence drives patterns of diversification and richness among mammal families},
    	author = {Castro-Insua, {Adrián} and {Gómez-Rodríguez}, Carola and Wiens, John J. and Baselga, {Andrés}},
    	year = {2018},
    	month = {06},
    	date = {2018-06-08},
    	journal = {Scientific Reports},
    	pages = {8781},
    	volume = {8},
    	number = {1},
    	doi = {10.1038/s41598-018-27068-y},
    	note = {PMID: 29884843
    PMCID: PMC5993713},
    	langid = {eng}
    }
    
    @article{smith2020jb,
    	title = {Climatic niche limits and community-level vulnerability of obligate symbioses},
    	author = {Smith, Robert J. and Jovan, Sarah and McCune, Bruce},
    	year = {2020},
    	date = {2020},
    	journal = {Journal of Biogeography},
    	pages = {382--395},
    	volume = {47},
    	number = {2},
    	doi = {10.1111/jbi.13719},
    	url = {http://onlinelibrary.wiley.com/doi/abs/10.1111/jbi.13719},
    	note = {{\_}eprint: https://onlinelibrary.wiley.com/doi/pdf/10.1111/jbi.13719},
    	langid = {en}
    }
    
    1. Switch to Visual Editor mode;

    2. Add the citation @castro-insua2018sr to the sentence "How climatic niches contribute to the formation and maintenance of biodiversity patterns?"`, as follows:

    `"*How climatic niches contribute to the formation and maintenance of biodiversity patterns?* [@cooney2016jae; @kozak2010el; @castro-insua2018sr]"`
    

    Note: Instead of copying and pasting, type ; @castro-insua2018sr within the citation tag that already exists there.

    1. Disable the Visual Editor.

    Expected behaviour

    Following Step 5, you should see that backslashes have been added before the square brackets in all citations.

    image

    Please let me know if you need a video to be recorded or further information about my system and about my RStudio and R sessions.

  10. bryanpmayfield commented on Nov 16, 2021

    @bryanpmayfield

    I am having the same exact issue using RStudio version 2021.09.0 on MaCOS 12.0.1.

    Also, not sure if this has anything to do with the canonical visual mode setting, but just in case I have tried it both with and without that setting set to true (in both global options and project options) and the problem persists.

    @pedrohbraga Have you found a work around for this?

  11. ronblum commented on Nov 16, 2021

    @ronblum
    Contributor

    @pedrohbraga Thank you! I can reproduce this issue based on your code and precise steps—I appreciate it. Specifically, tested using RStudio Desktop 2021.09.0+351 on MacOS 12. I was also able to reproduce is it by switching back and forth between source and visual mode—sometimes once was enough, sometimes twice.

  12. removed
    info needed
    on Nov 16, 2021
  13. 49 remaining items

  14. modified the milestones: , Spotted Wakerobin (2022.06.0) on Apr 7, 2022
  15. ronblum commented on May 23, 2022

    @ronblum
    Contributor

    Reopening because it looks like there's another version of this issue, which came up in #11198 and I can reproduce in PT and SW. Let me know if I should open a separate issue instead.


    [Copied from @Robinlovelace's note in #11198.]

    travis-main.zip

    No problem. See attached another maybe reproducible example. Every time I open this up in visual mode all the citations get changed from something like this:

    [@wood_visualizing_2011; @vonschmidt_webbased_2019; @morgan2020; @beecham2022]
    

    to this:

     [\@wood_visualizing_2011; \@vonschmidt_webbased_2019; \@morgan2020; \@beecham2022].
    

    which is frustrating to say the least...

  16. ronblum commented on May 24, 2022

    @ronblum
    Contributor

    Closed in favor of opening #11279.

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

Metadata

Metadata

Labels

backportbugregressionvisual markdown

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions