Problems with in_header, \usepackage{cancel} for Rmarkdown. Cancel package not working/recognized

I have followed the instructions here among other places.

In other words, in_header: followed by -\usepackage{cancel} includes the latter in the preamble, thus presumably loads the package when creating the tex document.

When reviewing the tex doc, I see that it is in fact added. However, I ran into 2 problems:

  1. Originally, for whatever reason, it doesn't find my cancel.sty file even though it's installed. Consequently, I had to add a .sty from CTAN to the same directory as the rmd file so it could knit. I also updated my tex to 2021.

  2. Even after doing this, when I try to use commands from the package in my rmd file, they don't work nor display under the chunk like the other latex code (i.e. if I do $$ \cancel{2} $$ it doesn't work).

Any thoughts on why this is happening?

I haven't experimented with other packages to see if this problem is package dependent nor am I familiar with the behavior of Rmarkdown within RStudio. In other words, what happens when packages are added to the preamble using the process above vs already in the template I'm using.

For example, I obviously need certain math packages (amsmath) to write out my equations and those packages are already in the default template (\usepackage{amsmath}). I'm using an APA 6th edition template.

Therefore, when I'm writing equations in my Rmarkdown file within RStudio , they pop up in the display which signifies to me packages are loaded and all things are well.

Hi,

If you can share a reproducible example, that would help us look into it.

It will help others help you if you provide some content that can be used directly; otherwise, one as to create an example themselves on what you are describing; It is time consuming and prone to error as we are not sure about what you have tried.

Thanks!

1 Like

Hey! I did some more troubleshooting and did the following:

  1. I'm on MacOS, so while I updated my TeX Live to 2021 using MacTeX, I updated tlmgr just in case.

  2. MacTeX adds /Library/TeX/texbin to PATH, but per the recommendation here, I added the full path to my 2021 Tex LIVE to PATH.

After doing these two things, my rmd document knits without needing a local cancel.sty package and the output works. As before, all I did was add the following to my rmd document:

header-includes   :
  - \usepackage{cancel}

I'm not sure exactly what provided the solution, but nevertheless it is fixed.

Final Question

Despite the solution, I was wondering how RStudio handles preview of latex when wrapped by $$ ... $$. I'm curious because for the cancel package it doesn't preview its functionality, so one can only see the proper display once the document is knitted. I have attached two comparison images to make my point.

Is it that RStudio preview only relies on a certain collection of "base" packages so any other packages won't update the display?

My RStudio is Version 1.2.5033.

I did some more troubleshooting and did the following:

  1. I'm on MacOS, so while I updated my TeX Live to 2021 using MacTeX, I updated tlmgr just in case.

  2. MacTeX adds /Library/TeX/texbin to PATH, but per the recommendation here, I added the full path to my 2021 Tex LIVE to PATH.

After doing these two things, my rmd document knits without needing a local cancel.sty package and the output works. As before, all I did was add the following to my rmd document:

header-includes   :
  - \usepackage{cancel}

I'm not sure exactly what provided the solution, but nevertheless it is fixed.

Final Question

Despite the solution, I was wondering how RStudio handles preview of latex when wrapped by $$ ... $$. I'm curious because for the cancel package it doesn't preview its functionality, so one can only see the proper display once the document is knitted. I have attached two comparison images to make my point.

Is it that RStudio preview only relies on a certain collection of "base" packages so any other packages won't update the display?

My RStudio is Version 1.2.5033.