jasonwilliams / anki

Anki VSCode Plugin
MIT License
273 stars 31 forks source link

Latex Issue #102

Open Mjvolk3 opened 1 year ago

Mjvolk3 commented 1 year ago

Card in VsCode

What are the classical approaches to Density Estimation? Briefly describe their procedures and important differences.

Density Estimation: Learn an approximation $\hat{\mathcal{D}} \approx \mathcal{D}$ using $\mathcal{X}=\left{\mathbf{x}{1}, \ldots, \mathbf{x}{n}\right}$

... Apparently github rendering doesn't like it either when I copy paste. I have provided a pic of the raw latex.

image


Card in Anki

image

image


Anyone know what this is happening? It makes it difficult to tell which equations render incorrectly before sending the cards to deck.

Mjvolk3 commented 1 year ago

I have now experience many issue when sending cards with Latex. My solution to this problem is to copy paste the Latex instance that failed directly in to the Anki GUI Mathjax inline or block editor. In every one of my instances this has worked perfectly. It seems that the Latex is getting jumbled up in an attempt to translate it to Mathjax, which might not be necessary. I can provide all of the failed instances if it is helpful.

rleyvasal commented 1 year ago

LaTex is not the same as Mathjax; therefore the syntax will be different.

You can test your Latex with overleaf.com before you send it to Anki

The following code may help to get you started.

VSCode LaTex

# latex

## Latex

$\hat{\mathcal{D}} \approx \mathcal{D}$

using

$\mathcal{X} = \{ {\mathbf{x}_1, \ldots, \mathbf{x}_n} \}$

Anki card

anki_latex

Note: It would help others to understand your issue if you indicate Issue and a screenshot of the Expected results in your comments.

rleyvasal commented 1 year ago

Following will also work

# Mathjax on back

## Mathjax on back 

$ \hat{\mathcal{D}} \approx \mathcal{D} $

using 

$ \mathcal{X} = \left \{  {\mathbf{\frac{x}{y}}_1, \ldots, \mathbf{x}_n} \right \} $ 
Mjvolk3 commented 1 year ago

Testing Suggestions

@rleyvasal I think you made a typo, but I get the point.

screenshot image

VsCode Markdown Example 1

I'll try to clarify my issue and my fix.

Input (Example 1)

Goal: Learn an approximation $\hat{\mathcal{D}} \approx \mathcal{D}$ using $\mathcal{X}=\left\{\mathbf{x}_{1}, \dots, \mathbf{x}_{n}\right\}$

Expected Output (Example 1)

screenshot image

Anki After Sending to Deck (Example 1)

screenshot image

My fix (Example 1)

screenshot image

VsCode Markdown Example 2

Input (Example 2)

$$
\hat{\theta}_{\mathrm{MAP}}=\operatorname{argmax}_{\theta} p\left(\theta \mid \mathbf{x}_{1: n}\right)
$$

Expected Output (Example 2)

screenshot image

Anki After Sending to Deck (Example 2)

screenshot image

My Fix (Example 2)

screenshot image

rleyvasal commented 1 year ago

@Mjvolk3

The problem is not with the Anki for VSCode extension. The issue is with the way the Latex is beign entered into vscode.

As a rule of thumb for subscripts if there is only one character {} are not needed, but when there are two characters as {1+m} , then the {} should be used such as the case below.


# test

## test

Goal: Learn an approximation $ \hat{\mathcal{D}} \approx \mathcal{D} $ 

using 

Your example `\mathbf{x}_{1}`

$ \mathcal{X} = \left \{ \mathbf{x}_{1}, \dots, \mathbf{x}_{n} \right \}$

My example  `\mathbf{x}_{1+m}` adnd  `\mathbf{x}_n`

$ \mathcal{X} = \left \{ \mathbf{x}_{1+m}, \ldots, \mathbf{x}_n \right \} $ 

anki_latex

Mjvolk3 commented 1 year ago

@rleyvasal How do you explain that things work when I copy the latex that contains _{x} into the MathJax inline/block in Anki? This can be seen in the first example I provided. This is the point I don't get.

Thanks for showing work arounds. Ideally, I could find all of the necessary work arounds and implement them in my Latex, but for some of my examples, like example 2 shown, I haven't been able to find a proper work around. If you have the time, would you mind taking a look at that example?

rleyvasal commented 1 year ago

This is very likely a bug.

Latex below does not show correctly when sending from VSCode to Anki, but entering the same code without in the Anki app (without `$$ ..$$) as a Mathjax block gives the correct math symbols.

$$
\hat{\theta}_{\mathrm{MAP}}=\operatorname{argmax}_{\theta} p\left(\theta \mid \mathbf{x}_{1: n}\right)
$$

The issue may be happening in one of the following:

  1. It could be the Markdown parser is not parsing the underscore correctly e.g. CommonMark, or Pandoc(not sure what is being used) or
  2. The problem maybe happening if the parsed output is not being correctly sent as a Mathjax Block to Anki (Inline $...$ has the same issue) .

@jasonwilliams may be in a better position to diagnose the issue.

jasonwilliams commented 1 year ago

I've only taken a brief look at this issue. It sounds like we would need some escape hatch whenever latex is involed so it's not interpreted as markdown (for e.g _ being <em>) I don't know if that should fix it?

I'm guessing the fix would need to be here somewhere: https://github.com/jasonwilliams/anki/blob/main/src/markdown/parsers/cardParser.ts#L146-L167

I don't use latex myself so I don't think I would be the best person to debug it but I'm happy to help guide anyone who wants to take a look. I really appreciate the test cases put in here, especially from @Mjvolk3, from what I can see in his comment is the markdown parser is changing some things as it has no awareness of latex syntax.

It could be the Markdown parser is not parsing the underscore correctly e.g. CommonMark, or Pandoc(not sure what is being used)

We use marked. In fact.. Here's an issue on marked where someone raised the same thing about _ https://github.com/markedjs/marked/issues/1538

I think there's more than enough solutions in that thread we could take and use, it would just take someone to make a PR