|
-
Feb 28th, 2010, 02:02 PM
#1
Thread Starter
Hyperactive Member
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?
-
Feb 28th, 2010, 02:06 PM
#2
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):
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!
Last edited by NickThissen; Feb 28th, 2010 at 02:23 PM.
-
Feb 28th, 2010, 02:30 PM
#3
Re: MathML, TeX, Math Formatting
In VB.NET that could look something like this
vb.net Code:
Private Sub CreatePng(ByVal equation As String) Dim texFile As New StringBuilder() 'Write LaTeX 'preamble' texFile.AppendLine("\documentclass{report}") texFile.AppendLine("\usepackage{amsmath}") 'some math functions texFile.AppendLine("\usepackage{nopageno}") 'no page numbers texFile.AppendLine("\setlength\textwidth{3in}") 'this doesn't look like a valid command but it seems to have worked :S texFile.AppendLine("\setlength\parindent{0in}") texFile.AppendLine("\begin{document}") 'add the equation texFile.AppendLine("\begin{displaymath}" & equation & "\end{displaymath}") 'end the document texFile.AppendLine("\end{document}") 'save it. Perhaps you should use a temporary file (in the Temp folder) for this! System.IO.File.WriteAllText("C:\equation.tex", texFile.ToString) 'Convert to DVI Process.Start("latex C:\equation.tex") 'And to PNG Process.Start("dvipng -T tight -x 1200 -z 1 C:\equation.dvi") 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!
-
Feb 28th, 2010, 02:34 PM
#4
Thread Starter
Hyperactive Member
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.
-
Feb 28th, 2010, 02:39 PM
#5
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).
-
Mar 1st, 2010, 09:56 PM
#6
Thread Starter
Hyperactive Member
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.)
-
Nov 13th, 2017, 10:31 AM
#7
New Member
Re: MathML, TeX, Math Formatting
I have one made. will post or upload to this site.
-
Nov 13th, 2017, 12:14 PM
#8
New Member
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.
-
Nov 13th, 2017, 08:15 PM
#9
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|