Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Latex Issue #102

Open
Mjvolk3 opened this issue Feb 23, 2023 · 8 comments
Open

Latex Issue #102

Mjvolk3 opened this issue Feb 23, 2023 · 8 comments

Comments

@Mjvolk3
Copy link

Mjvolk3 commented Feb 23, 2023

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
Copy link
Author

Mjvolk3 commented Feb 23, 2023

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
Copy link

rleyvasal commented Feb 24, 2023

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
Copy link

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
Copy link
Author

Mjvolk3 commented Feb 25, 2023

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

  • So for some reason the _ is being translated to <em> and </em>.

My fix (Example 1)

  • Copy paste the exact Latex that didn't work from VsCode into the MathJax inline text box.
    • $\hat{\mathcal{D}} \approx \mathcal{D}$ using $\mathcal{X}=\left\{\mathbf{x}_{1}, \dots, \mathbf{x}_{n}\right\}$
      • Note: This is the portion of the Latex from Input above that failed.

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)

  • Same as Example 1
    • Note too, this is still a MathJax block.

screenshot
image

  • This works, and I have around 5 different instances where this solution works. I can provide them if there is more interest in this.

@rleyvasal
Copy link

@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
Copy link
Author

Mjvolk3 commented Feb 25, 2023

@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
Copy link

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
Copy link
Owner

jasonwilliams commented Feb 27, 2023

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 _ markedjs/marked#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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants