Bug 281813 - Add support for LaTeX formulas in annotations
Summary: Add support for LaTeX formulas in annotations
Status: RESOLVED FIXED
Alias: None
Product: okular
Classification: Applications
Component: general (show other bugs)
Version: unspecified
Platform: Compiled Sources Linux
: NOR wishlist
Target Milestone: ---
Assignee: Okular developers
URL:
Keywords:
: 213251 (view as bug list)
Depends on:
Blocks:
 
Reported: 2011-09-11 16:50 UTC by Niels Ole Salscheider
Modified: 2011-09-22 19:39 UTC (History)
3 users (show)

See Also:
Latest Commit:
Version Fixed In: 4.8.0
Sentry Crash Report:


Attachments
patch to add LaTeX support to annotations (7.60 KB, patch)
2011-09-11 16:50 UTC, Niels Ole Salscheider
Details
Screenshot (10.29 KB, image/jpeg)
2011-09-11 16:50 UTC, Niels Ole Salscheider
Details
LatexRenderer class (9.07 KB, patch)
2011-09-17 12:22 UTC, Niels Ole Salscheider
Details
adds support for latex annotations to the annotation window (5.09 KB, patch)
2011-09-17 12:23 UTC, Niels Ole Salscheider
Details
new screenshot (32.21 KB, image/jpeg)
2011-09-17 12:25 UTC, Niels Ole Salscheider
Details
LatexRenderer class (9.76 KB, patch)
2011-09-22 09:42 UTC, Niels Ole Salscheider
Details
adds support for latex annotations to the annotation window (6.75 KB, patch)
2011-09-22 09:43 UTC, Niels Ole Salscheider
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Niels Ole Salscheider 2011-09-11 16:50:05 UTC
Created attachment 63563 [details]
patch to add LaTeX support to annotations

Version:           unspecified
OS:                Linux

I tried to add support for LaTeX formulas in annotations to Okular (see attachment).

I reused some parts of Kopete's LaTeX plugin to do so. Maybe it would be better to add this code to some library if more applications start to support LaTeX formulas...

If you think that this functionality is useful I can try to finish my patch.

Reproducible: Didn't try



Expected Results:  
Render LaTeX formulas inside $$...$$
Comment 1 Niels Ole Salscheider 2011-09-11 16:50:36 UTC
Created attachment 63564 [details]
Screenshot
Comment 2 Albert Astals Cid 2011-09-11 17:22:26 UTC
This feature goes a bit against the fact that we are trying to save our annotations to file in case of PDF that support annotations, the problem is that this is not supported as far as i know by PDF thus makes our already hard work even harder
Comment 3 Niels Ole Salscheider 2011-09-11 17:34:06 UTC
I thought about that, too. However, there is no way to store formulas in PDF annotations as far as I know. So the best one can do is to store them as plain Latex-style code (as I did until now).

But if I do so why should not Okular allow to render them properly while all other readers just display the plain code? If that is not wanted I am ok with it, though.
Comment 4 Albert Astals Cid 2011-09-11 17:51:56 UTC
Oh, i see what you did is that any annotation out there with $$ is rendered with latex, do not think it is a good idea, i am sure there are files out there with $$ in the text and they did not want a to represent a latex  formula
Comment 5 Niels Ole Salscheider 2011-09-11 18:13:27 UTC
I think most annotations won't include $$valid_latex_code$$ if it is not on purpose but you are right that we cannot be sure about that.

Maybe a solution would be not to alter the tooltip but add a second view in the opened annotation that can be toggled by a small button? There is no need to save the status of the view, just default to the one that does not render LaTeX when opening it...
Comment 6 Albert Astals Cid 2011-09-11 19:37:01 UTC
What i would add is something that detects if there is valid latex code between $$ and then adds a status message on the top saying something like "This comment seems to have latex formula, click here to render it", and if it is clicked, the formula is rendered. What do you think?
Comment 7 Jonathan Verner 2011-09-12 08:12:06 UTC
(In reply to comment #2)
> annotations to file in case of PDF that support annotations, the problem is
> that this is not supported as far as i know by PDF thus makes our already hard
> work even harder

PDF supports Appearance Streams for annotations, so we could have latex render
the formula into a PDF file and create the AP entry in the annotation dict from
the pdf. We could also include some special entry in the annotation dict to
indicate that this is in fact a rendered LaTeX formula and what the LaTeX code
was so that we could edit it afterwards...

I had some luck with this approach, however the saving was done by podofo not poppler.

Same way we could --- I suppose at least --- add a custom field to the
annotation xml entry to indicate that it is a formula so that we don't need
to do automatic guessing (maybe even create a new annotation type). If that
is a no-go, I also like Albert's approach in comment #6.
Comment 8 Niels Ole Salscheider 2011-09-14 09:16:19 UTC
> PDF supports Appearance Streams for annotations, so we could have latex render
> the formula into a PDF file and create the AP entry in the annotation dict from
> the pdf. We could also include some special entry in the annotation dict to
> indicate that this is in fact a rendered LaTeX formula and what the LaTeX code
> was so that we could edit it afterwards...

Is it a good idea to include a rendered formula? This way, we cannot adjust font size and color easily...

> Same way we could --- I suppose at least --- add a custom field to the
> annotation xml entry to indicate that it is a formula so that we don't need
> to do automatic guessing (maybe even create a new annotation type).

If this is possible and can be stored in the PDF, too, it is probably the best solution. But if we decide to go this way I think I need some help to implement it.

Otherwise I can create a patch for the solution as proposed in comment #6.
Comment 9 Albert Astals Cid 2011-09-14 18:03:07 UTC
Including the rendered formula has the benefit that other programs (i.e. Acrobat) can render the formula too, but that obviously depends on another missing feature that is saving annotations back to the file.
Comment 10 Jonathan Verner 2011-09-15 09:50:54 UTC
(In reply to comment #8)
> Is it a good idea to include a rendered formula? This way, we cannot adjust
> font size and color easily...

As Albert said, it would have the benefit that other viewers would be able
to display it, or okular could be use the rendered formula on systems where
there is no latex installed. 

Since we would also store the latex code used to generate the formula, there
would be no problem with adjusting the color, font size, etc.

HOWEVER, as Albert said, currently poppler (the pdf library used by okular)
doesn't support saving annotations back to the file. I think there was a
GSoC project to implement it in poppler, but I have no idea how that went.

So, until this is supported in poppler, I think Albert's approach mentioned in #6 would be the best way forward. Although personaly I would probably default to rendering the formula and showing the code on click instead of showing the code and rendering on click. This is, however, for Albert to decide, I think.

I would be willing to help you with this, however currently I do not have much spare time to work on it :-(
Comment 11 Albert Astals Cid 2011-09-15 11:06:51 UTC
****
As Albert said, currently poppler (the pdf library used by okular)
doesn't support saving annotations back to the file.
****
I did not say that at all. I said Okular doesn't do it, poppler has support for it since a while ago (might be needed to be improved or might not).
Comment 12 Jonathan Verner 2011-09-15 11:13:53 UTC
Sorry, I misinterpreted your comment. But this is good news :-) I will have to look at poppler again.

(In reply to comment #11)
> ****
> As Albert said, currently poppler (the pdf library used by okular)
> doesn't support saving annotations back to the file.
> ****
> I did not say that at all. I said Okular doesn't do it, poppler has support for
> it since a while ago (might be needed to be improved or might not).
Comment 13 Niels Ole Salscheider 2011-09-17 12:22:06 UTC
While it would be nice to save the rendered formula to the pdf, I think it's something we can still implement once okular has support for saving annotations to it.

I created a patch to support something like Albert's proposal in comment #6. What do you think?
Comment 14 Niels Ole Salscheider 2011-09-17 12:22:54 UTC
Created attachment 63725 [details]
LatexRenderer class
Comment 15 Niels Ole Salscheider 2011-09-17 12:23:27 UTC
Created attachment 63726 [details]
adds support for latex annotations to the annotation window
Comment 16 Niels Ole Salscheider 2011-09-17 12:25:57 UTC
Created attachment 63727 [details]
new screenshot
Comment 17 Albert Astals Cid 2011-09-21 23:09:57 UTC
I'd like a bit more of user feedback in case latex is not installed, as far as i can see now what would happen is that he clicks the button and gets nothing?
Comment 18 Niels Ole Salscheider 2011-09-22 06:28:15 UTC
You are right, this should be changed. There are two possibilities:

Either do not display the button at all if latexis not installed or do so but show a message box saying "latex / pdftex is not installed". We can also display an error message if latex fails because of invalid syntax.

What do you prefer?
Comment 19 Niels Ole Salscheider 2011-09-22 09:42:53 UTC
Created attachment 63850 [details]
LatexRenderer class
Comment 20 Niels Ole Salscheider 2011-09-22 09:43:19 UTC
Created attachment 63851 [details]
adds support for latex annotations to the annotation window
Comment 21 Niels Ole Salscheider 2011-09-22 09:44:23 UTC
I think it is best to always display the button so that the user gets feedback why his code does not work.
I attached a patch for that.
Comment 22 Albert Astals Cid 2011-09-22 17:32:24 UTC
Commited, thanks a lot for your work, if you are interested in doing more okular work do not hesitate to contact me at aacid@kde.org
Comment 23 Niels Ole Salscheider 2011-09-22 18:06:21 UTC
Thanks.

I have just noticed that you removed "m_filelist". This way, the temporary files will not get deleted on destruction of the annotation window. Is this a good idea?
Comment 24 Albert Astals Cid 2011-09-22 18:21:46 UTC
Thanks for the review, i actually had a look at the code in the destructor and for some reason overlooked the QFile::remove there and then m_filelist did not make any sense anymore and i removed it altogether. But obviously, yes it is a bad idea to remove it, i've readded it now, can you have a second look to be sure i did not break something else?
Comment 25 Niels Ole Salscheider 2011-09-22 18:39:53 UTC
Seems to be right now.
Comment 26 Albert Astals Cid 2011-09-22 19:39:58 UTC
*** Bug 213251 has been marked as a duplicate of this bug. ***