Results 1 to 9 of 9

Thread: MathML, TeX, Math Formatting

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    MathML, TeX, Math Formatting

    Firstly, I don't want to SOLVE equations, I just want to create them.

    I've looked through several posts about this and haven't found anything helpful.

    I want to create a series of programs (or maybe web apps) to let my students practice solving math problems. I need a way to format certain kinds of math expressions correctly.

    Some stuff is easy for format...
    x² - 3x + 2 = 0
    5x = 10
    x - (-4) = 15

    But some stuff has no simple (that I know of) way to be displayed in VB...
    Simplify (x² - 4) over (x - 2)
    Simplify sqrt(50)
    Solve x over 10 = 100

    I've looked into using MathML, TeX etc. to format the output and maybe use a browser window or rtf but there doesn't seem to be a VB OCX for this.

    I want my problems to be randomly generated and then displayed in the correct format. Then the solutions would be generated from the created problem.

    Any suggestions?

  2. #2
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: MathML, TeX, Math Formatting

    To display the equations you could create a LaTeX equation string and turn it into a PNG image using a command line parser. I've done this in the past (but not with VB.NET) and I think I used MikTex for this. It has very little to do with VB.NET, except running a command to create the LaTeX file and running another command to turn it into a PNG file.

    Of course, the equations have to be in LaTeX form:
    \frac{x^2 - 4}{x - 2}
    \sqrt{50}
    etc...


    EDIT
    I found my 'project' where I used this. It was a script for Windows Live Messenger Plus! but I never finished it.
    I could send you the full (JScript) source but it's alot of stuff you don't need.

    The important stuff is this:
    Code:
    // -----------------
    		// Create .tex file
    		// -----------------
    		var texFile = fso.CreateTextFile(path + "\\file.tex", true);
    		
    		// Write latex 'preamble'
    		texFile.WriteLine("\\documentclass{report}\n" + 		// we always need to set the documentclass
    					   "\\usepackage{amsmath}\n" + 		// use special math package
    					   "\\usepackage{nopageno}\n" + 		// get rid of page numbers
    					   "\\setlength\\textwidth{3in}\n" +	// make page width a little smaller to avoid huge windows
    					   "\\setlength\\parindent{0in}\n" +	// no paragraph indent required for us
    					   "\\begin{document}\n");			// start of document
                    var texCode = "";
    		
    		// Check which equation type to use:
    		// - If there are only two tokens, and they are at the start and end of the message,
    		// 	then we have a single equation without text, so we can use the displaymath equation style
    		// - Else, we may have some text between the equations, so we want to use the inline equation style
    		if (i===2 && startsWithToken(trim(Message)) && endsWithToken(trim(Message)))
    		{
    			Debug.Trace("2 tokens at start and end found --> Using displaymath style.");
    			
    			// Replace first occurence of $$ with the begin displaymath string
    			texCode = Message.replace("$$", "\\begin{displaymath}");
    			// Replace the second (and last) occurence with the end displaymath string
    			texCode = texCode.replace("$$", "\\end{displaymath}");
    		}
    		else
    		{
    			Debug.Trace(i + " tokens found --> Using inline style.");
    			// Replace ALL occurences of $$ with a single $, which automatically transforms the message into valid latex.
    			texCode = Message.replace(/\$\$/g, "$");
    		}
    		
    		texFile.WriteLine(texCode);
    		texFile.WriteLine("\n\\end{document}");
    		texFile.Close();
    		
    		Debug.Trace("Tex file created.");
    		
    		
    		// -----------------
    		// Create DVI file and PNG file
    		// -----------------	
    		
    		var cmd = "cmd /c " + path.substring(0,2) + " & " +	// start cmd and set drive (C: , D: , etc..)
    				"cd \"" + path + "\" & " +				// set current directory
    				"latex file.tex & " +					// convert tex to dvi
    				"dvipng -T tight -x 1200 -z 1 file.dvi";	// convert dvi to png
    							
    		Debug.Trace("Executing command: \"" + cmd + "\".");
    		var oShell = new ActiveXObject('WScript.Shell');
    		oShell.Run(cmd, 0, true);
    The first part is writing the start of the LaTeX file (you can't just write the equation only, it needs to be in a valid file), things like the packages to use etc.
    Then, the "if (i===2 && startsWithToken(trim(Message)) && endsWithToken(trim(Message)))" statement can probably be ignored by you. In this script, it was possible to have an inline equation: ($$ denotes start and end of equation)
    Code:
    This: $$y(x) = 3x^2 + 2$$ is an inline equation
    or just an equation
    Code:
    $$y(x) = 3 + 4x - 1$$
    The difference is that, in an inline equation, I use the $ ... $ LaTeX command, for inline equations, and for just an equation I use the \begin{displaymath} and \end{displaymath} commands, which yield some larger and nicer equations.
    I assume you don't need any text with your equations, so you can always use the \begin{displaymath} and \end{displaymath} commands.

    Finally, I run two commands (at the same time, after setting the working directory):
    Code:
    latex file.tex
    this creates a .dvi file from the .tex file
    Code:
    dvipng -T tight -x 1200 -z 1 file.dvi
    this converts file.dvi into a PNG image using some properties. For these commands you need MikTex and DviPng (I think both come togheter but I dunno..)


    Hope that helps!

  3. #3
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: MathML, TeX, Math Formatting

    In VB.NET that could look something like this
    vb.net Code:
    1. Private Sub CreatePng(ByVal equation As String)
    2.    
    3.     Dim texFile As New StringBuilder()
    4.    
    5.     'Write LaTeX 'preamble'
    6.     texFile.AppendLine("\documentclass{report}")
    7.     texFile.AppendLine("\usepackage{amsmath}")  'some math functions
    8.     texFile.AppendLine("\usepackage{nopageno}")  'no page numbers
    9.     texFile.AppendLine("\setlength\textwidth{3in}")  'this doesn't look like a valid command but it seems to have worked :S
    10.     texFile.AppendLine("\setlength\parindent{0in}")
    11.     texFile.AppendLine("\begin{document}")
    12.    
    13.     'add the equation
    14.     texFile.AppendLine("\begin{displaymath}" & equation & "\end{displaymath}")
    15.    
    16.     'end the document
    17.     texFile.AppendLine("\end{document}")
    18.    
    19.     'save it. Perhaps you should use a temporary file (in the Temp folder) for this!
    20.     System.IO.File.WriteAllText("C:\equation.tex", texFile.ToString)
    21.    
    22.     'Convert to DVI
    23.     Process.Start("latex C:\equation.tex")
    24.    
    25.     'And to PNG
    26.     Process.Start("dvipng -T tight -x 1200 -z 1 C:\equation.dvi")
    27. End Sub
    Not too sure if the commands at the end would work like this... But it's something like that anyway.


    After that, just display the 'equation.png' file in a PictureBox.
    Of course, the users of this application need to be able to execute the 'latex' and 'dvipng' commands so they need to have latex installed!

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    Re: MathML, TeX, Math Formatting

    Thanks for your response. I'll use it if things must go that way. But....

    Is there no way to create a TeX string and use somebody's OCX to covert it to a pgn image (or bitmap or jpg) - or even better: some text or HTML - and display that?

    Surely someone has written a MathML or TeX OCX that can be used for this kind of stuff. I don't want to rely on any external programs.

  5. #5
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: MathML, TeX, Math Formatting

    Not too sure what you mean by an 'OCX'. I know the term OCX from VB6 where they were something like DLL's but other than that I don't know what they are.

    If you mean some code somebody has written in VB.NET, then I highly doubt it. I don't think there is anything available for VB.NET or C#.NET even that will convert a latex string into an image without using some kind of external tools (like Miktex).

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    Re: MathML, TeX, Math Formatting

    Thanks for the help, Nick. I'd rather go with an all-in-one solution and not need to depend on extra software. I still might go with your idea if all else fails.

    Any one else want to chime in?

    PS to Nick.
    I have an exchange student this year from the Netherlands. She's a pretty good kid. I haven't learned much dutch but my spanish is getting better. (I have a student from Venezuela, too.)

  7. #7
    New Member
    Join Date
    Oct 2012
    Posts
    3

    Re: MathML, TeX, Math Formatting

    I have one made. will post or upload to this site.

  8. #8
    New Member
    Join Date
    Oct 2012
    Posts
    3

    Re: MathML, TeX, Math Formatting

    I have a program that I made. Written in Vb6.
    you can activate it with a checkmark to keep program floating, You type in the textbox, example given: sqrt 25 and press enter.
    It will format the sentence and send to a server and the server gives you a picture in picture box.
    Picture box follows the mouse cursor for you to put any where in a environment where you can "draw" with mouse.
    Once you place the picture where you want it. Press enter and it start drawing it for you. I use it for myself in Real Time Board for tutoring math across the internet. Also recently added a grid maker to help in Geometry. I can not figure a way to up load to here.

  9. #9
    New Member
    Join Date
    Oct 2012
    Posts
    3

    Re: MathML, TeX, Math Formatting

    Not trying to direct traffic away from this site, since I can not find a way to upload to this site, may I humbly direct you to Planet Source Code and look for White board or you can Google it and look for math LaTek Vb6 formula generator by soundminded. If I have broen any rules, please accept my apologies, and will the proper people please contact me as I wish to share this program and source written in Vb6. I had this IDE since 1994. Yes, call me old and old fashioned.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width