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

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.

Glad you found a solution.

About the preview, this is HTML math I believe using MathJax. Not all packages and command are known by default. So this \cancel is not recognized by mathjax.

Mathjax support some packages including this ones cancel — MathJax 3.2 documentation

However for the preview it is not easy to setup to make it work.

Easy way is to use

$$
\require{cancel}
\cancel{\binom{n}{s}}
$$

and this should preview ok but no compile in PDF.

To change the mathjax configuration of the preview is not something I know how to do. This would probably be a feature request for the IDE.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.