Can I strike out text in an equation in R markdown
Asked Answered
C

2

6

I have an equation that I wrote in r-markdown as follows

$$ 2 \frac{meter}{day} * 3 ~ days $$

base equation

I'd like to show that the days cancel by striking them out. You can strike out text in r-markdown normally by ~~surrounding the text in tildaes~~

However, the tildae in the expression seems to only insert spaces.

$$ 2 \frac{meter}{~~day~~} * 3 ~ ~~days~~ $$

tildae

I also tried as one might in latex

$$ 2 \frac{meter}{sout{day}} * 3 ~ days $$

But this doesn't seem to work either.

souteqn

Are there any options for striking out bits of equation that work in r-markdown equations?

Cirrhosis answered 29/11, 2018 at 16:12 Comment(1)
You're missing a backslash for \soutHaemoid
W
3

For PDF output you can use the LaTeX style cancel:

---
header-includes: \usepackage[makeroom]{cancel}
output: pdf_document
---

$$ 2 \frac{\text{meter}}{\cancel{\text{day}}} * 3 ~ \cancel{\text{days}} $$

Result:

enter image description here

Note that the usage of \text is unrelated to your question and only used for typographical reasons.

Unfortunately this does not work with HTML output.

Washbowl answered 29/11, 2018 at 17:23 Comment(8)
When I add the header you have suggested on the header of the markdown document it stops the stops the creation of the pdf with the following error message ... ! Undefined control sequence. <argument> \cancelDrupelet
@Drupelet Does the minimal example that I posted work for you? Does your TeX system have the cancel.sty installed?Washbowl
@Ralph It generates the error message that I mentioned above. I am running markdown using RStudio on a Mac and my tex live utility includes the cancel package.Drupelet
@Drupelet Please update the test document to keep the TeX file (c.f. https://mcmap.net/q/580006/-rmarkdown-retain-tex-file) and check that the resulting TeX file contains \usepackage[makeroom]{cancel}.Washbowl
@Ralph I looked at the TeX file and its does not contain the package. Can you please help/tell me on how to add it?Drupelet
@Drupelet The header-includes YAML header should take care of that. Very odd. As an alternative you can write that line into a file and include it via includes, c.f. bookdown.org/yihui/rmarkdown/pdf-document.html#includes-1.Washbowl
@Ralph Adding the line \usepackage[makeroom]{cancel} in the file preamble.tex did this for me. Thank you for your help.Drupelet
I have \usepackage[makeroom]{cancel} listed in header-includes but I had to add additionally $$\require{cancel} ... $$ in the equation to make it work.Pseudaxis
O
3

For HTML, add at the beginning of the markdown,

<script type="text/x-mathjax-config">
MathJax.Hub.Register.StartupHook("TeX Jax Ready",function () {
  MathJax.Hub.Insert(MathJax.InputJax.TeX.Definitions.macros,{
    cancel: ["Extension","cancel"],
    bcancel: ["Extension","cancel"],
    xcancel: ["Extension","cancel"],
    cancelto: ["Extension","cancel"]
  });
});
</script>

and then

$$2 \frac{\text{meter}}{\cancel{\text{day}}} * 3 ~ \cancel{\text{days}}$$

will work

enter image description here

Ohmage answered 3/6, 2020 at 10:28 Comment(1)
I am using markdown to write a book and one of my chapters has an equation where I am using strike through font. When you say add the script at the beginning of the markdown do you mean to add this at the top of the relevant chapter? I tried it and it does not work. ThanksDrupelet

© 2022 - 2025 — McMap. All rights reserved.