Is there a way to render LaTeX equations in Crystal Reports?

I am developing a report in Crystal Reports and should display mathematical formulas and equations in it. Formulas and equations are stored in the SQL Server database as plain text (using LaTeX markup).

Getting their visualization in HTML is not a problem, because I use MathJax to do the work at the browser level (in HTML / CSS or MathML).

The real problem: how can I get these equations in a report? I searched the Internet and found nothing about it. When doing another search on the Crystal interface, the only thing I found was to insert the (obsolete) Microsoft Equation Editor as an OLE object in the report, but that didn't work.

So how do you visualize these LaTeX math equations in this Crystal report? Is there some (obscure) component / plugin that does this work? If not, is there a better way to do this? Has someone already received and allowed a similar use case?

OBS 1: I must have this report generated in PDF due to the already tested standards used in my work.

OBS 2: The application that will generate this report is an ASP.NET MVC 3 web application with a SQL Server 2008 database (using NHibernate).

+4
source share
2 answers

I found a workaround that is elegant at the same time and does not rely on the Crystal Reports extension using C.

I found this small and simple tool for visualizing latex equations: mimeTeX . Using this, I can visualize latex equations in GIF images (as a CGI application). With this, I created a phantom array field in datatable where the report receives data.

Here is what I did:

  • Retrieve the markup of the latex equation from my real database;
  • A mimeTeX request using this markup and mimeTeX returns a gif image;
  • Take this image and convert it to png format (Crystal unexpectedly does not support GIF files);
  • Finally, put this PNG image (its bytes) in the phantom field created in the datatable used in the report;
  • Now you can use this field in the report! Images for each record (equation) are generated and displayed without problems!

the only drawback I have found so far using this approach is that all the images are stretched to the same size as the placeholder. If the images have sizes that vary greatly, some will be displayed in pixels, while others will become “crushed”. But I look forward to how to solve this problem!

--- Edit ---

The problem of "crushed images" is resolved. I resize images in code, preserving their aspect ratio and "pasting" them into a fixed-size image. Now all images get the same size and are not crushed!

Here is the code for resizing:

MemoryStream ResizeImage(Stream OriginalFile, int NewWidth, int MaxHeight, bool OnlyResizeIfWider) { int finalWidth = NewWidth; int finalHeight = MaxHeight; System.Drawing.Image FullsizeImage = System.Drawing.Image.FromStream(OriginalFile); // Prevent using images internal thumbnail FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone); FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone); if (OnlyResizeIfWider) { if (FullsizeImage.Width <= NewWidth) { NewWidth = FullsizeImage.Width; } } int NewHeight = FullsizeImage.Height * NewWidth / FullsizeImage.Width; if (NewHeight > MaxHeight) { // Resize with height instead NewWidth = FullsizeImage.Width * MaxHeight / FullsizeImage.Height; NewHeight = MaxHeight; } System.Drawing.Image NewImage = FullsizeImage.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero); // Clear handle to original file so that we can overwrite it if necessary FullsizeImage.Dispose(); MemoryStream bmpStream = new MemoryStream(); // Put in a new image of A x B pixels to evict distortion using (var bitmap = new Bitmap(finalWidth, finalHeight)) { using (var canvas = Graphics.FromImage(bitmap)) { canvas.InterpolationMode = InterpolationMode.HighQualityBicubic; canvas.Clear(Color.White); canvas.DrawImage(NewImage, 0, 0); canvas.Save(); } bitmap.Save(bmpStream, ImageFormat.Bmp); } return bmpStream; } 
0
source

To do this, you need to create a library of user-defined functions (UFL) . Transfer LaTeX representation of the formula to UFL, generate representation of the image of the formula, return the URL of the image.

To use the path, insert the “image” and then the link to the UFL function in the “graphic location” property:

 //Insert | Picture...; graphic location conditional formula latex({table.latex_field}) 
+1
source

Source: https://habr.com/ru/post/1399208/


All Articles