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

Using $$-style escapes for math #74

Open
JohannesHatke opened this issue Oct 26, 2023 · 8 comments
Open

Using $$-style escapes for math #74

JohannesHatke opened this issue Oct 26, 2023 · 8 comments

Comments

@JohannesHatke
Copy link

I really enjoy using this program because it speeds up adding flashcards for my preexisting notes. :)
Unfortunately my note taking programs all use the $$ style to start and end mathjax blocks. So I can't copy the notes over. I figured out that you can't change this setting in anki.
I have written a function to convert from the $$-notation to the other one in my fork: https://github.com/JohannesHatke/apy
If youre interested in including this feature, I would open up a pull request and finish implementing it by adding a configuration key for it.

I have implemented it so that _parse_file calls the function for each line. If you have any suggestions and are interested I'd love to know.

@lervag
Copy link
Owner

lervag commented Oct 27, 2023

I really enjoy using this program because it speeds up adding flashcards for my preexisting notes. :)

Thanks, I'm glad to hear that!

Unfortunately my note taking programs all use the $$ style to start and end mathjax blocks. So I can't copy the notes over. I figured out that you can't change this setting in anki. I have written a function to convert from the $$-notation to the other one in my fork: https://github.com/JohannesHatke/apy

Cool! For completeness, could you outline how your workflow with apy and your notes look like?

If youre interested in including this feature, I would open up a pull request and finish implementing it by adding a configuration key for it.

I'm not 100% sure. It seems like a very specific use case, and I don't immediately recognize this as a "natural" part of apy. This is why I'm asking for the workflow above, I would like to hear more how this feature might make sense possibly for more people.

@JohannesHatke
Copy link
Author

JohannesHatke commented Oct 30, 2023

Thanks for the reply.

Cool! For completeness, could you outline how your workflow with apy and your notes look like?

Usually i write long notes with a lot of math equations in vim with Markdown-Preview. I import some of these into Obsidian, where the $$-style is used. I then add flash cards by just copying the relevant parts over into apy.

I think the benefit of adding this feature is that it would allow more users to create flashcards from existing notes easily. Obsidian is quite popular and this would make apy a lot more useful for Obsidian users.

@lervag
Copy link
Owner

lervag commented Oct 30, 2023

Usually i write long notes with a lot of math equations in vim with Markdown-Preview. I import some of these into Obsidian, where the $$-style is used. I then add flash cards by just copying the relevant parts over into apy.

Ok, so, something like this?

  1. Oblivion open, copy some Markdown note with possibly some math.

  2. In a terminal, you do apy sync if necessary, then apy add and use the note you copied from step 1.

The point of asking for the details here is first to ensure we are not in a x-y problem setting. Also, I would want to know how the feature should be incorporated. E.g., should it be an option or only a configuration parameter in the config file? How does it actually look? If this feature existed, do I understand that your preferred integration would be to have this as an option in the apy.json file, such as markdown_math_to_mathjax which would be false by default (to ensure backward compatibility)?

I think the benefit of adding this feature is that it would allow more users to create flashcards from existing notes easily. Obsidian is quite popular and this would make apy a lot more useful for Obsidian users.

You may be right, but at the same time, this is the first time someone make this type of request. I want to properly understand the idea before I add it, as that makes it easier to properly maintain things.

@lervag
Copy link
Owner

lervag commented Mar 24, 2024

I'm closing this due to inactivity.

@lervag lervag closed this as completed Mar 24, 2024
@camoz
Copy link
Contributor

camoz commented Jul 12, 2024

Hey :)

Could this be reopened? I'm also interested in this.

My use case is similar to that of @JohannesHatke. Also it's really useful to preview MathJax before creating the cards, to ensure that the formulas etc. get rendered correctly. The $/$$ math delimiters are much more common in markdown and are supported by a lot of markdown editors.

I'm not 100% sure. It seems like a very specific use case, and I don't immediately recognize this as a "natural" part of apy.

I would argue that it is related to apy, since apy introduces the markdown format for writing Anki notes, which AFAIK Anki does not use (it uses HTML). Anki introduces \(...\) and \[...\] delimiters to HTML for MathJax and LaTeX (which ofc are not part of HTML), but for markdown, $/$$ math delimiters are more common, as mentioned above. So IMO it would make sense to support it in Anki.

Also, I would want to know how the feature should be incorporated. E.g., should it be an option or only a configuration parameter in the config file? How does it actually look? If this feature existed, do I understand that your preferred integration would be to have this as an option in the apy.json file, such as markdown_math_to_mathjax which would be false by default (to ensure backward compatibility)?

That is a good question.

Personally, I'd like to enable it globally, so a config option in apy.json sounds reasonable to me, perhaps with an associated command line switch (but I would not use that). I think it does not make sense to enable/disable this feature on a per-notes basis (correct me if I'm wrong).

It would be nice to convert from $...$ to \(...\) and from $$...$$ and \[...\], since markdown uses the former, and Anki uses the latter, respectively.

@camoz
Copy link
Contributor

camoz commented Jul 12, 2024

Another thought: I think it would be good to do the conversion on a per-field basis instead of per-note or even per-file, as a safety measure. Because, with $...$ and $$...$$, the left and right delimiter cannot be distinguished by itself, so unintentional syntax errors are more likely. E.g., a dangling $$ somewhere could "shift" all other $$ and result in text being interpreted as MathJax and vice versa.

@lervag
Copy link
Owner

lervag commented Sep 24, 2024

I'm sorry for the delay - yes, I'll reopen and reconsider this.

@lervag lervag reopened this Sep 24, 2024
@lervag
Copy link
Owner

lervag commented Sep 24, 2024

TLDR: My understanding is that the following feature and the specified behaviour will resolve this issue:

  • Add an option named latexTranslateMode. The option should be available both globally in apy.json and in note preambles. The values and their behaviour:
    • off: Keep the current behaviour. This should probably be the default to avoid surprising existing users.
    • mathjax: apy will automatically translate the following
      • $ ... $ and \( ... \)
      • $$ ... $$ and \[ ... \]
    • latex: apy will automatically translate the following
      • [$] ... [/$] and \( ... \)
      • [$$] ... [/$$] and \[ ... \]

@camoz Please confirm that this is what you want.


The following is a longer reply to explain the reasoning that reaches the above conclusion.

The $/$$ math delimiters are much more common in markdown and are supported by a lot of markdown editors.

That is true.

I would argue that it is related to apy, since apy introduces the markdown format for writing Anki notes

Ok, yes - but only "partly". That is, given this note input:

# A note

## Field 1

Contents of field 1.

…

## Field n

Contents of field n.

We see it uses the Markdown headings to delimit the fields. However, the field content can be parsed both as Markdown and as plain text/html. The markdown type is the default, but it can be disabled by using the markdown: false option in the note "preamble".

Anki introduces \(...\) and \[...\] delimiters to HTML for MathJax and LaTeX (which ofc are not part of HTML), but for markdown, $/$$ math delimiters are more common, as mentioned above.

So, your concrete feature request is for apy to automatically translate between $ ... $, \( ... \) and $$ ... $$, \[ ... \], respectively?

Also, I would want to know how the feature should be incorporated. E.g., should it be an option or only a configuration parameter in the config file? How does it actually look? If this feature existed, do I understand that your preferred integration would be to have this as an option in the apy.json file, such as markdown_math_to_mathjax which would be false by default (to ensure backward compatibility)?

That is a good question.

I think we don't really need an option here. Or, perhaps we could have an option to say that the translation should apply to [$] ... [/$] and [$$] ... [/$$] instead. Some people, e.g. me, always uses full LaTeX instead of MathJax, so this could actually be an improvement for me as well.

E.g. an option named latexTranslateMode with possible values off, mathjax and latex. And we should recognize this option both in apy.json and as a preamble option for a card.

Another thought: I think it would be good to do the conversion on a per-field basis instead of per-note or even per-file, as a safety measure. Because, with $...$ and $$...$$, the left and right delimiter cannot be distinguished by itself, so unintentional syntax errors are more likely. E.g., a dangling $$ somewhere could "shift" all other $$ and result in text being interpreted as MathJax and vice versa.

I don't think this will be a problem. And if it is, let's fix it after I've added the feature.

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