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

Feature Request : Latex Support #138

Closed
Divyanshg01 opened this issue Sep 6, 2024 · 18 comments · Fixed by #133
Closed

Feature Request : Latex Support #138

Divyanshg01 opened this issue Sep 6, 2024 · 18 comments · Fixed by #133

Comments

@Divyanshg01
Copy link

Divyanshg01 commented Sep 6, 2024

Loving this plugin guys but It would be even cooler if it can support latex
Any plans or potential work arounds to have latex with this plugin?

@OXY2DEV
Copy link
Owner

OXY2DEV commented Sep 6, 2024

I have never used latex so I have no experience working with it, so I am probably not the person you are looking for.

This would most likely have to be done via another cli tool(like the one render-markdown uses).

But that introduces a whole new range of things to do.

I will have to see if there's something that can be done.

@Divyanshg01
Copy link
Author

No issue
One thing I would like to request you
can you please provide the config files to make this plugin look like this ,since the default behaviour is different
image

@OXY2DEV
Copy link
Owner

OXY2DEV commented Sep 7, 2024

How does it look on your machine?

@OXY2DEV
Copy link
Owner

OXY2DEV commented Sep 7, 2024

Screenshot_2024-09-07-12-15-03-973_com termux-edit

I have started working on this. So far, I have only added brackets to it.

But it should be possible without any external dependencies.

I also plan on letting users use other cli tool to do the latex → Unicode translation and show that with virtual lines.

@OXY2DEV
Copy link
Owner

OXY2DEV commented Sep 7, 2024

I also added an option to add scopes to the brackets.

Screenshot_2024-09-07-12-41-24-491_com termux-edit

@kusnezoff-alexander
Copy link

Just wanted to through in nabla.nvim, which seems to provide a similar feature, with the only difference being that the result is rendered in a floating window instead of being rendered inline. Maybe some inspiration could be drawn from there.

@OXY2DEV
Copy link
Owner

OXY2DEV commented Sep 7, 2024

I honestly don't like using too many virtual lines as it gets distracting very quickly if you have a bunch of them on screen.

I would rather have something simple like this which doesn't get in the way.

latex-cropped.mp4

@kusnezoff-alexander
Copy link

I am coming from Obsidian, so I am used to the "live-rendering" of LaTeX there. For me personally it would be really nice if the latex could be rendered just like it does there.

In the end it comes down to personal taste, but how are you then planning to render things like integrals/limes/matrices/... ?

@OXY2DEV
Copy link
Owner

OXY2DEV commented Sep 7, 2024

Hmm. That's an issue too.

Guess I will have to see if a less destructive way to show them is possible.

@OXY2DEV
Copy link
Owner

OXY2DEV commented Sep 7, 2024

@kusnezoff-alexander I have looked into the code of that plugin and I can only say that the approach that plugin is not exactly fast(it's checking a line letter-by-letter).

And I don't know about you but I would prefer not to do something like this as it can easily start making the entire redraw slower as it doesn't lavarge tree-sitter for the syntax.

For stuff like integrals, I would use something like this.

Upper limit0 ∞
  ↑ Lower limit

I would then use highlighting to make it more readable.

Is it perfect? No, but then again, this is a simple markdown previewer so this should be a good enough sacrifice.

Neovim runs in a TUI, so it makes sense to use all the features of a TUI rather than mimicking a GUI.

@kusnezoff-alexander
Copy link

kusnezoff-alexander commented Sep 7, 2024

@OXY2DEV Ah oke, I thought it was leveraging tree-sitter (at least it says so in the README and I had to :TSInstall latex before I could use it).

Regarding your approach, I would say that it could be difficult if not impossible to manually render all of what LaTeX has to offer. Another idea would be to use an existing renderer (pandoc?) to render an image and then display the resulting image instead (it looks like image.nvim could be used for this)?

Honestly I don't understand your quote in this context, since a GUI is "is a form of user interface that allows users to interact with electronic devices through graphical icons and visual indicators such as secondary notation." (Wikpedia). This feature restricts itself to visualization and doesn't include direct user interaction, or am I wrong?

@OXY2DEV
Copy link
Owner

OXY2DEV commented Sep 7, 2024

I thought it was leveraging tree-sitter.

It only uses Tree-sitter to get the text from the buffer(otherwise it would have to manually check everything line by line).

Regarding your approach, I would say that it could be difficult if not impossible to manually render all of what LaTeX has to offer.

Just like html, I can only give you a simplified version to see. Tree-sitter doesn't tell me what a \frac{1}{2} is. It only tells me that this is a generic_command so I have to manually add support for everything. And to make matters worse, lua-patterns isn't as powerful as regex so some edge-cases will go through.

So, the goal is to provide a good enough preview, instead of trying to support everything(which isn't possible, at least inside a terminal).

Another idea would be to use an existing renderer (pandoc?) to render an image and then display the resulting image instead?

Wouldn't it be simpler to create a keybind that gets the text under the cursor(you can use tree-sitter too, if you want)? Then just send it to the terminal to run pandoc to get the image.

No idea how image.nvim gets the image but there should be some function to load an existing image from a path.

Honestly I don't understand your quote in this context,

I meant that unlike a GUI which has 100,000s of pixels to draw on the terminal only has a limited number of cells to draw on.

So something like 222 can easily mess the rendering. Same goes for anything beyond x/y.

And aligning text is kind of a pain. There's too many what-if cases. For example, latex inside a description list wouldn't know where to place itself, nested block quotes would result in text being off by a few characters, tables not retaining their structure when using latex inside them and so much more.

@Divyanshg01
Copy link
Author

How does it look on your machine?

Terminal : Kitty with Alabaster dark theme , transparency is enabled
This is how it looks
I also use obsidian.nvim but I don't think that is the reason because I have disabled its ui features
image

my config look like this

return {
	"OXY2DEV/markview.nvim",
	lazy = false, -- Recommended
	-- ft = "markdown" -- If you decide to lazy-load anyway

	dependencies = {
		-- You will not need this if you installed the
		-- parsers manually
		-- Or if the parsers are in your $RUNTIMEPATH
		"nvim-treesitter/nvim-treesitter",

		"nvim-tree/nvim-web-devicons",
	},
	config = function()
		require("markview").setup({
			modes = { "n", "no", "c" }, -- Change these modes
			-- to what you need

			hybrid_modes = { "n", "i" }, -- Uses this feature on
			-- normal mode

			-- This is nice to have
			callbacks = {
				on_enable = function(_, win)
					vim.wo[win].conceallevel = 2
					vim.wo[win].concealcursor = "c"
				end,
			},
		})
	end,
}

@OXY2DEV
Copy link
Owner

OXY2DEV commented Sep 7, 2024

@Divyanshg01 basically your background option is set to dark. But since you have a transparent background the plugin doesn't find a background color.

So, it looks through a bunch of fallback colors and one of them has a black background. This color is then mixed with other colors resulting in what you are seeing.

@Divyanshg01
Copy link
Author

So is there a way to fix it without setting a opaque background of terminal ??
Also how to fix rendering of things like [!TIP] and [!DANGER]

@OXY2DEV
Copy link
Owner

OXY2DEV commented Sep 7, 2024

So is there a way to fix it without setting a opaque background of terminal ??

You can just link the plugin's highlight groups(see the bottom of the README) to the ones provided by your colorscheme.

Also how to fix rendering of things like [!TIP] and [!DANGER].

The plugin doesn't render anything if you just use [!Tip]. That's due to Tree-sitter thinking that this is a link. Not much I can do(at least without breaking other stuffs).

@OXY2DEV
Copy link
Owner

OXY2DEV commented Sep 8, 2024

A bit of an update on the issue.

Screenshot_2024-09-08-09-04-59-783_com termux-edit
Screenshot_2024-09-08-09-05-12-913_com termux-edit

I have also checked to see if I could do stuff like integrals. However, tree-sitter doesn't recognize them so there's really not much to do(at least without breaking something else).

OXY2DEV added a commit that referenced this issue Sep 8, 2024
Supports simple symbols & some functions.

Ref: #138
@OXY2DEV
Copy link
Owner

OXY2DEV commented Sep 8, 2024

Since I didn't mention why this issue was closed let me say it here.

  1. Converting latex to unicode is quite problematic. Even nabla can only handle some cases so this approach isn't a solution.

  2. Evaluating latex via tree-sitter also doesn't quite work as converting a tree to an array isn't easy especially if you need to add spaces to things for alignment.

  3. Iterating letter by letter is quite slow and the plugin can redraw a few times per second(depending on how you configured it) which makes it a bad solution.

  4. Virtual lines don't scroll horizontally so it doesn't look good when viewing on smaller windows.

  5. There's no latex to image converter available in my package manager and building from source isn't possible for me so using images isn't possible for me.

  6. even if we ignore the previous issue, images aren't supported in termux and termux-x11 isn't great for typing so testing would take more time than it should.

@Divyanshg01, for now I can only show primitive representations in the plugin. For example,

$\lim_{0 \to \infty}$
lim_(0 → ∞)

$\int_{2}^{3}$
∫₂³

JeffDess pushed a commit to JeffDess/markview.nvim that referenced this issue Sep 10, 2024
Supports simple symbols & some functions.

Ref: OXY2DEV#138
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 a pull request may close this issue.

3 participants