Best way to display a Latex Equation on a canvas?

What’s the best way to display a latex equation on a canvas? Are there any examples out there?

Havent seen code to render the latex directly to a canvas, but maybe you could use javascript and a HTML viewer:

1 Like

There’s no way to convert Latex code to an image without an external tool such as MathJax as Ivan mentioned.

An alternative is to install Latex on your computer and use shell commands from your Xojo application to do it. Here is a version that works on a Mac. It requires that that you have MacTex/TexLive installed.

Public Function Latex2pic(mTxt As String, res As Integer) as Picture
  'Convert a LaTeX format math formula string to a picture
  'mTxt is the LaTeX text
  'res is the required picture resolution in dpi
  'Uses a shell to call pdflatex and dvipng commands
  'which are part of the MacTex/TexLive distributions
  '
  'cmdPath is the path to the directory of the Tex commands.
  'Depending on which Tex distribution is used this may need to be changed.
  '
  Static cmdPath As String = TexLiveLatest 'Get path to latest MacTex installation
  if len(cmdPath)<5 then return nil 'Bad path, so exit
  dim f As FolderItem = SpecialFolder.ApplicationData.Child("MyAppStuff")
  if not f.Exists then
    'Create Application Support/MyAppStuff folder.
    f.CreateAsFolder
  end if
  dim sPath As String = f.NativePath
  Dim My_Shell As New Shell
  My_Shell.TimeOut=-1
  Dim theShellCode As String = "cd """+sPath+""""+EndOfLine
  theShellCode = theShellCode + cmdPath+"pdflatex -output-format=dvi <<<'"
  theShellCode = theShellCode + "\documentclass[12pt]{slides}"+EndOfLine
  theShellCode = theShellCode + "\usepackage{geometry}"+EndOfLine
  theShellCode = theShellCode + "\usepackage{lingmacros}"+EndOfLine
  theShellCode = theShellCode + "\usepackage{tree-dvips}"+EndOfLine
  theShellCode = theShellCode + "\usepackage[usenames]{color} %used for font color"+EndOfLine
  theShellCode = theShellCode + "\usepackage{amssymb} %maths"+EndOfLine
  theShellCode = theShellCode + "\usepackage{amsmath} %maths"+EndOfLine
  theShellCode = theShellCode + "\usepackage[utf8]{inputenc} %useful to type directly diacritic characters"+EndOfLine
  theShellCode = theShellCode + "\geometry{papersize={8.5in,1.5in}}"+EndOfLine
  theShellCode = theShellCode + "\begin{document}"+EndOfLine
  'Insert latex math code here:
  theShellCode = theShellCode + mTxt+EndOfLine
  'End of math code
  theShellCode = theShellCode + "\end{document}'"+EndOfLine
  theShellCode = theShellCode + cmdPath+"dvipng -bg Transparent -D "+str(res)+" texput.dvi"+EndOfLine
  My_Shell.Execute theShellCode
  'If everything went according to plan, there should be a png file in the app's app support folder.
  dim picFile As FolderItem = f.Child("texput1.png") 'default png file name
  If picFile <> Nil Then
    Dim pic As Picture
    pic = Picture.Open(picFile)
    'Optionally, place the picture directly in the window with either of the following uncommented:
    if pic.Width>ImageWell1.Width-30 or pic.Height>ImageWell1.Height-10 then
      ImageWell1.Image = ScaleImage(pic,ImageWell1.Width-100,ImageWell1.Height-10)
    Else
      ImageWell1.Image = pic
    End If
    if pic.Width>Canvas1.Width-30 or pic.Height>Canvas1.Height-10 then
      Canvas1.Backdrop = ScaleImage(pic,Canvas1.Width-100,Canvas1.Height-10)
    Else
      Canvas1.Backdrop = pic
    End If
    Return pic
  Else
    'Error - Couldn't open picFile
    return nil
  End If
End Function

Public Function TexLiveLatest() as string
  'Return the path to the latest version of MacTex/TexLive distribution
  '
  'Called by Latex2pic to find path to latest version.
  'The path should be: /usr/local/texlive/****/bin/x86_64-darwin/
  ' where **** is the year of release.
  ' Eg.: /usr/local/texlive/2018/bin/x86_64-darwin/
  '
  'May need to update for Mac silicon processors
  dim cmdPath As String = "/usr/local/texlive/****/"
  dim dt As  new Date
  dim curYear As Integer = dt.Year
  dim ff As FolderItem
  dim path As String = ""
  'Start with current year and go back as far as 20 years
  for y as Integer = curYear to curYear-20 step -1
    path = Replace(cmdPath,"****",str(y))
    ff = new FolderItem(path,FolderItem.PathTypeShell)
    if ff.Exists then
      exit 
    end if
  next
  if right(path,1)<>"/" then path = path+"/"
  path = path + "bin/x86_64-darwin/"
  if ff<>nil and ff.Exists then 
    return path
  Else
    return ""
  end if
End Function

***Edited to remove extraneous debug code.

2 Likes

Im looking at the website. Where and what do I download for Xojo?