Skip to content

Feature add for issue #74#115

Closed
JohannesHatke wants to merge 8 commits intolervag:masterfrom
JohannesHatke:master
Closed

Feature add for issue #74#115
JohannesHatke wants to merge 8 commits intolervag:masterfrom
JohannesHatke:master

Conversation

@JohannesHatke
Copy link
Copy Markdown
Contributor

Hi, im sorry for being super inactive on this issue.

I cleaned up my fork and implemented almost everything mentioned in issue: #74
The only thing im not sure about is the mention of the preamble options. Thats a feature of apy i havent used and so i was not sure how i should implement that. I added the config options with the apropiate default though.

The '$$'/'$' couldn't be parsed easily with text.replace, so i just manually split the text, into latex/markdown parts.

I hope this is useful after being inactive for so long :)

@lervag
Copy link
Copy Markdown
Owner

lervag commented Aug 5, 2025

Hi, im sorry for being super inactive on this issue.

No problem! Late is better than never!

The only thing im not sure about is the mention of the preamble options. Thats a feature of apy i havent used and so i was not sure how i should implement that. I added the config options with the apropiate default though.

My first impression is that what you've implemented should work fine. We might improve it later if we find that necessary.

The '$$'/'$' couldn't be parsed easily with text.replace, so i just manually split the text, into latex/markdown parts.

Sounds like a good approach! 👍🏻

I hope this is useful after being inactive for so long :)

Definitely!

Copy link
Copy Markdown
Owner

@lervag lervag left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the code looks more or less good; I only had minor comments. But I think we should also as a minimum add some documentation that explains what this does. E.g. here:

apy/README.md

Lines 105 to 132 in 28eda7d

## Configuration
`apy` loads configuration from `~/.config/apy/apy.json`. The following keys are
currently recognized:
- `base_path`: Specify where `apy` should look for your Anki database. This is
usually something like `/home/your_name/.local/share/Anki2/`.
- `img_viewers`: Specify a dictionary of image viewer commands. Each key is
a file extension. The value is a command list, e.g. `['display', 'density',
'300']` which specifies the command and its options to use for the
corresponding key (file extension).
- `img_viewers_default`: Specify the default command to show an image. Must be
provided as a list of the command and desired options, such as `['feh',
'-d']`.
- `markdown_models`: Specify a list of models for which `apy` will use
a markdown converter.
- `markdown_pygments_style`: Specify the code highlight scheme to use for
fenced code blocks in Markdown notes. See the [Pygments
documentation](https://pygments.org/docs/styles/) for more details.
- `pngCommands`/`svgCommands`: Set LaTeX commands to generate PNG/SVG files.
This is inspired by the [Edit LaTeX build
process](https://ankiweb.net/shared/info/937148547) addon to Anki.
- `presets`: Specify preset combination of model and tags for use with `apy
add-single`.
- `profile_name`: Specify which profile to load by default.
- `query`: Specify default query for `apy list`, `apy review` and `apy tag`.
- `review_show_cards`: Whether to show list of cards by default during note
review

I think we need to explain the possible option values, as well as what the effects are.

Finally, if possible we should add some tests. That's a nice to have, though - so if you don't want to go there I can look into that myself.

@JohannesHatke
Copy link
Copy Markdown
Contributor Author

Hey, I added some simple tests and found also found and fixed a small bug in the process.

@lervag
Copy link
Copy Markdown
Owner

lervag commented Aug 10, 2025

Great, thanks! I'm merging this now. Thanks for the contribution!

@lervag
Copy link
Copy Markdown
Owner

lervag commented Aug 10, 2025

Note, by the way, I made some small changes. The most important one is that I want to use consistent casing; I use snake case, not camel case.

lervag added a commit that referenced this pull request Aug 10, 2025
@lervag lervag closed this Aug 10, 2025
@lervag
Copy link
Copy Markdown
Owner

lervag commented Aug 10, 2025

Sorry, I have a couple of follow-up questions:

With latex_translate_mode = "off"

This is the original behaviour. It simply ensures that the mathjax borders like \[ ... \] and \( ... \) are properly escaped (if we don't do this, then the resulting html will be [ ... ] and ( ... ). Thus, this is a sensible thing to do in any case, right? I mean, even if you want to do additional translations?

With latex_translate_mode = "mathjax"

This will convert $$ ... $$ to \[ ... \] and $ ... $ to \( ... \). Thus, "mathjax" refers to the resulting type.

With latex_translate_mode = "latex"

This will convert [$$] ... [$$] to \[ ... \] and [$] ... [$] to \( ... \). Thus, "latex" refers to the original type. This is inconsistent with the
earlier naming.

Additional comments

The translation mode only works when translating from Markdown to the field HTML, it does not go the other way. This means it becomes unreliable when you use the apy review or apy edit features.

Conclusion

I think we need something better here:

  • The "off" mode should always apply to avoid breaking support for any existing mathjax.
  • We should have an option that specifies how to treat $$ and $ in markdown. It should either produce mathjax or classic latex [$$] and [$]. I propose markdown_latex_target = "mathjax" | "classic".
  • markdown_latex_target should work both ways. If the value is "classic", then it should translate the field text when we generate Markdown, i.e. [$$] should become $$. And similar, if the value is "mathjax", then we transform \[ ... \] from the field html into $$ ... $$ markdown.

Please let me know what you think!

lervag added a commit that referenced this pull request Aug 10, 2025
@lervag
Copy link
Copy Markdown
Owner

lervag commented Aug 10, 2025

Ok, I've worked through this now and I think the current version should be much better. Please test and let me know what you think.

The main changes:

  • The behaviour is controlled by a global option only. We don't allow to change the behaviour in the markdown files directly, as that will impose much more complexity with regard to keeping the states.
  • The option is called markdown_latex_mode and has the values mathjax and latex. mathjax is the default, because most people will want to use that (I think).

@camoz
Copy link
Copy Markdown
Contributor

camoz commented Jan 28, 2026

Not involved in the PR but I read through this thread. Really cool that it works both ways now!

What you wrote in the 2 previous comments makes much sense IMO.

Thank you for putting effort into this even though (afaik) you don't use the feature yourself.

@lervag
Copy link
Copy Markdown
Owner

lervag commented Jan 28, 2026

Thanks for the kind words!

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

Successfully merging this pull request may close these issues.

3 participants