The difficulty is you do not understand the solutions so its hard to provide you with code since it does what you want, you're just unable to implement it.

Mr.Sknake ,I was able to understand your code and also implemented it.
I found the output of your code in bracket form and not the fraction that we see in books.
The code which I am using is :

C# Syntax (Toggle Plain Text) 
textBoxEquation.AppendText("()/()");
            this.textBoxEquation.Focus();


            const string matchNumerator = @"\((?<Numerator>[\d,]*(\.\d*))\)";
            const string matchDenominator = @"\((?<Denominator>[\d,]*(\.\d*))\)";
            const string matchBoth = matchNumerator + @"\/" + matchDenominator;
           
            //const string txtBoxInput = @"()/()";

            string txtBoxInput = textBoxEquation.Text;
            System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(txtBoxInput, matchBoth);
            if (m.Success)
            {
                decimal dNumerator, dDenominator;
                if (decimal.TryParse(m.Groups["Numerator"].Value, out dNumerator) && decimal.TryParse(m.Groups["Denominator"].Value, out dDenominator))
                {
                    //and this right here is your string in latex format
                    string latex = @"\frac {" + dNumerator.ToString() + "}{" + dDenominator.ToString() + "}";
                    decimal valueOut = dNumerator / dDenominator;
                    System.Diagnostics.Debugger.Break();

                }

            }

Since I have made for all mathematical symbols and the mathematical equation can be the combination of any symbol.So when we click on any of the math symbol ,it is appended in the textbox.
So the code which you gave me for fraction dispalys bracket and not the fraction format.
And I have made differently for divide sign and for fraction.
For fraction I want the way as seen in the thumbnail attached
and for division I am using simply divide sign i.e."÷" .

Did the effort reading through this thread. Do not understand it very well.
I think that Sknake and Ryshad did their best to tell you how to translate text to LaTeX format. (At least for division that is, if you want to do this full blown you need a text to LaTeX compiler, wish you all the luck with that;) )
But if I understand it well, what you want, is feed a textbox some latex text and expect it shows math formulas you see in textbooks.
Did you not try it for yourself to notice that this won't work?
Why not try this or try to incorporate Microsoft Equation Editor 3.0 into your application?

But if I understand it well, what you want, is feed a textbox some latex text and expect it shows math formulas you see in textbooks.
Did you not try it for yourself to notice that this won't work?
Why not try this or try to incorporate Microsoft Equation Editor 3.0 into your application?

Actually after feeding in textbox I was able to display math formulae for many symbols.Also In textbox I am using such notations which the user can easily understand because donot know latex symbols and in background I am replacing those user friendly notations with actual latex symbols.
But the same replacement I was not able to do with fraction .In case of fraction it is not replacing with latex symbol rather its interpreting it is displaying it as user friendly notation only..........
I tried a lot but could not find any solution.....
The only solution which I could find is to make math dictionary from all latex types to user friendly symbols.I am working on that and hope that I may work....

VinniJain
I think u can make a dictionary and use LINQ....
Hope it works for you........

VinniJain
I think u can make a dictionary and use LINQ....
Hope it works for you........

Thanks for your reply avirag........i do not know how to use it...
Can you explain me in detail...........

Well vinniJain.........
I don't have much knowledge related to this, may be other Brilliant coders of daniweb Can explain you in a better way............!!!

We have done our best here to answer your question. You wanted to display a user friendly version of the equation in the textbox then convert that to latex in order to create the equation output. We have given you the code you need to parse a user friendly fraction using regular expressions to convert it into a latex format.

Your task now, having understood how our code works, is to adjust it to find the different symbols that the user friendly string might contain and convert those into latex format also.

If you decide to go the LINQ route I would recommend starting a new thread since that doesnt relate to this question. A thread titled "Help with LINQ Dictionary" or something similar is far more likely to attract the help you need than a 4 page thread on something losely related :)

Either way, please mark the thread as solved if you feel there is nothing more we can do for you.

Ryshad, thanks for helping me............But I don't understand why you all are not getting me.
I think ddnabe understood and even replied that it won't works.
And I want to ask one thing that did you not try it for yourself to notice that it was not giving the required result that was shown in thumbail?
Ryshad just tell me one thing that were you able to get the fraction form (like in books) on running the code ?

A TextBox, well it says it, can only display text, characters. Some fonts have characters for square root etc. , but that's a poor substitute for the things you see in textbooks. The math formulas you see in textbooks are in fact pictures.

That is not what the code does. This is why sknake and i aren't sure you have understood the code.

The code we gave you uses regular expressions to locate "(number1)/(number2)" in a string. It then extracts this, converts it into latex format "/frac {number1}{number2}".

You said you wanted to display user friendly text in the textbox, we assumed you could do that yourself. Your question, was how to convert that back to latex format.

Now, i want to make it more user friendly in such a way that when user clicks on divide button then a divide sign should appear like "÷" but in coding it should implement "\div" and generate the coresponding math format.Similarly for fraction ,in the textbox there should appear "()/()" and in background it should implement "\frac {}{}".Similarly for other functions also such text should appear in textbox which user can understand easily but in ocding it should implement the symbols which mimeTex can understand.

You already had the code to convert the Latex string into the image. We gave you an example of how to convert user friendly text into latex. We never said that the code would do everything you need it to, that isnt how we work here at Daniweb.
The best way to learn is by experimenting with the code. We will gladly help you if you hit an error or a problem, but we wont do all the work for you.

You say that you understand what the code is doing, if thats not the case we will explain it for you. But if you do understand it then the next step is to take that concept and work out how you can use it to acheive your goal.

@ddanbe
The root of it all is this:

Vinnijain has a form which has a textbox for the user to type out an equation in Latex. There is a picturebox which then displays the equation in the standard math format.
vinnijain wants to make it more user friendly so that when a user clicks a button, the textbox displays userfriendly text "()/()" but the code handles it as latex format "/frac {}{}".
We have shown him how to convert the user friendly format to latex. He already has the code required to convert the latex string to an image of the math notation.

There! 5 pages of thread condensed into 3 short paragraphs :D

commented: Could not give you rep any earlier:) +4

The code we gave you uses regular expressions to locate "(number1)/(number2)" in a string. It then extracts this, converts it into latex format "/frac {number1}{number2}"

yes, Ryshad you gave me the code ans as you mentioned that it converts into latex format.........
But the problem is that since that code is for converting it to latex format but actually its not converting it.........
And thats the main problem .

Here, try this:

const string matchNumerator = @"\((?<Numerator>[\d,]*(\.\d*)*)\)";
      const string matchDenominator = @"\((?<Denominator>[\d,]*(\.\d*)*)\)";

Adding the * after (\.\d*) means that the decimal place and trailing digit is optional. As sknake showed in his original post, the code accepts a number in the format "(1.2)/(3.4)".
Changing it to this will allow it to handle both "(2.3)" and "(2)".

Please try to be clearer when pointing out a problem. You were just saying that it didnt work.

If this fixes the issue, please mark the thread as solved :)

#
//and this right here is your string in latex format
#
string latex = @"\frac {" + dNumerator.ToString() + "}{" + dDenominator.ToString() + "}";
#
decimal valueOut = dNumerator / dDenominator;
#
System.Diagnostics.Debugger.Break();

Ryshad according to above lines of code ,it should convert "()/()" to "\frac {}{}". But its not converting it and thats the problem.
And the problem that it is not displaying fraction form is that since the code is not converting it to "\frac {}{}" (latex form) so latex compiler is interpreting it as "()/()" and so its not displaying the required fraction form.
As for converting to fraction form there is no particular coding.I just have to feed latex form input to latex compiler .....

const string matchNumerator = @"\((?<Numerator>[\d,]*(\.\d*)*)\)";
            const string matchDenominator = @"\((?<Denominator>[\d,]*(\.\d*)*)\)"; 
            const string matchBoth = matchNumerator + @"\/" + matchDenominator; 

            //Here is the change he told you to make
            //const string txtBoxInput = @"()/()";           
            string txtBoxInput = txtTextBox.Text;

            System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(txtBoxInput, matchBoth);
            if (m.Success)
            {
                decimal dNumerator, dDenominator;
                if (decimal.TryParse(m.Groups["Numerator"].Value, out dNumerator) && decimal.TryParse(m.Groups["Denominator"].Value, out dDenominator))
                {

                    //and this right here is your string in latex format
                    string latex = @"\frac {" + dNumerator.ToString() + "}{" + dDenominator.ToString() + "}";
                    decimal valueOut = dNumerator / dDenominator;
                    MessageBox.Show(latex);
                }

            }

That is the code i ran. When i typed "(2.3)/(3.4)" in the textbox i got a messagebox showing "/frac {2.3}{3.4}" and when i typed "(2)/(3)" i got a messagebox showing "/frac {2}{3}".

Did you change the numerator and denominator strings as i showed you?

Post the latest version of your code and i'll see if i can spot where yours is different.

My application is attached here...........
Kindly help me please....................
I don't want to continue the thread more but please solve my problem.........

Okay, firstly: i have made the changes to your code so that it processes fractions. I cant run the project directly as i dont have vs 2008, i have attached the updated version..please let me know if it runs correctly.

secondly, my post earlier that said you had the code in a button handler was correct, i just thought it was a different button. I have moved the regex code to the FormatText method, where it needs to be:

private string FormatText(string input)
         {
             const string matchNumerator = @"\((?<Numerator>[\d,]*(\.\d*)*)\)";
             const string matchDenominator = @"\((?<Denominator>[\d,]*(\.\d*)*)\)";
             const string matchBoth = matchNumerator + @"\/" + matchDenominator;
             //Here is the change he told you to make

             //const string txtBoxInput = @"()/()";
             System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(input, matchBoth);
             while (m.Success)
             {
                 decimal dNumerator, dDenominator;
                 if (decimal.TryParse(m.Groups["Numerator"].Value, out dNumerator) && decimal.TryParse(m.Groups["Denominator"].Value, out dDenominator))
                 {
                     //and this right here is your string in latex format
                     string latex = @"\frac {" + dNumerator.ToString() + "}{" + dDenominator.ToString() + "}";
                     input = input.Remove(m.Index, m.Length);
                     input = input.Insert(m.Index, latex);
                 }
                 m = System.Text.RegularExpressions.Regex.Match(input, matchBoth);

             }
            input = input.Replace("÷", "\\div");
            input = input.Replace("√", "sqrt");
            input = input.Replace("x", "\\times"); 
            input = input.Replace("π", "\\pi");
            input = input.Replace("θ", "\\theta");
            input = input.Replace("∞", "\\infty");
            //and so on
            return input;
         }

I have also added a loop so that it handles multiple occurances of fractions in the same string.

My concern, however, is that in doing this for you you wont have learned how all of this works. As such, the code doesnt format "\\sqrt[]{}" or anything else that needs custom formatting. If the code works correctly then its down to you to disect it and work out how to add that functionality to it.

Thanks a lot .But the code is not working .It is again giving the original result., i.e. same thing which i was getting in the original code.
Also initially it gave result once but after that it is hanging(runtime problem) and displaying no error.

Thanks Ryshad,
I am able to modify it myself.........
There was little modification which I have made and finally I got success.
Modified code is :

const string matchNumerator = @"\((?<Numerator>[\d,]*(\.\d*)*)\)";
             const string matchDenominator = @"\((?<Denominator>[\d,]*(\.\d*)*)\)";
             const string matchBoth = matchNumerator + @"\/" + matchDenominator;
             //Here is the change he told you to make

             //const string txtBoxInput = @"()/()";
             System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(input, matchBoth);
             
                 decimal dNumerator, dDenominator;
                 if (decimal.TryParse(m.Groups["Numerator"].Value, out dNumerator) && decimal.TryParse(m.Groups["Denominator"].Value, out dDenominator))
                 {
                     //and this right here is your string in latex format
                     string latex = @"\frac {" + dNumerator.ToString() + "}{" + dDenominator.ToString() + "}";
                     input = input.Remove(m.Index, m.Length);
                     input = input.Insert(m.Index, latex);
                 
                 }

Thanks once again to you Ryshad and all others who replied my thread.............

Okay, the FormatText function works, i wrote and tested it in a seperate app. Can you use a breakpoint and tell me what the input is at the start of the method and what it is at the end please.

Also, go line by line to see where it is hanging.

Ignore last post...thread hadnt updated to show your last message :)

Glad you got it working. Took some doing but we got there

For finding nth root , latex format is "\sqrt[]{}" and on button click the form which displays in textbox is : "root:[]{}" . Now to convert this "root:[]{}" into latex format i am using following code :

const string matchNumerator1 = @"root:\((?<Numerator4>((?<p>\()|[^\(\)]|(?(p)(?<-p>\))|))*)\)";
        const string matchDenominator1 = @"\((?<Denominator4>((?<p>\()|[^\(\)]|(?(p)(?<-p>\))|))*)\)";
        const string matchBoth1 = matchNumerator1 + matchDenominator1;
        System.Text.RegularExpressions.Match n = System.Text.RegularExpressions.Regex.Match(input, matchBoth1);
        while (n.Success)
        {
            string sNumerator1 = n.Groups["Numerator4"].Value;
            string sDenominator2 = n.Groups["Denominator4"].Value;
            string latex1 = @"\sqrt[" + sNumerator1.ToString() + "]{" + sDenominator2.ToString() + "}";
            input = input.Remove(n.Index, n.Length);
            input = input.Insert(n.Index, latex1);
            n = System.Text.RegularExpressions.Regex.Match(input, matchBoth1);

        }

And I using this code just after the fraction code.
But this code is not giving desired result .
Kindly help me out..........

Of course it isnt working; you told me the format was root:()() so thats what it is looking for.

const string matchNumerator1 = @"root:\[(?<Numerator4>((?<p>\()|[^\(\)]|(?(p)(?<-p>\))|))*)\]";
        const string matchDenominator1 = @"\{(?<Denominator4>((?<p>\()|[^\(\)]|(?(p)(?<-p>\))|))*)\}";

the above code will find the string you need.


Once again, i cannot understate the importance of learning what the code is doing. The numerator and denimonator code hasnt changed at all from the fraction regex. All that has changed is the enclosing parentheses.

\((?<Numerator4>((?<p>\()|[^\(\)]|(?(p)(?<-p>\))|))*)\)

root:\[(?<Numerator4>((?<p>\()|[^\(\)]|(?(p)(?<-p>\))|))*)\]

You could have easily changed the symbols yourself. I'm not trying to rant or condescend but, if you are trying to learn how to code, these are the kind of problems you will face all the time and learning how to overcome them yourself will make you a much stronger programmer. Every time you encounter new syntax, or a structure you havent used before, experiment with it, read the documentation for it, disect it. You need to understand exactly what its doing so that you can adapt it for future problems. The same goes for code that you are given in forums. We will gladly help you out and provide you with code, but its SO important that you dont just copy and paste the code and move on...learn why the code you are given works. Look at how it has been structured so that you can learn how other coders solve problems.

commented: Well said! +5

Thanks for replying ........
This I have already done.......but problem is that it is not working when I am trying to enter fraction...........
I don't what's the problem............

Thanks for replying ........
This I have already done.......but problem is that it is not working when I am trying to enter fraction...........
I don't what's the problem............

Have you added the code i gave you for powers, or have you replaced the old code with the new?

The old code replace fractions, the new code replaces powers. You need to have both. If you have implemented both correctly it will work fine for fractions within powers and vice versa:

private void button1_Click(object sender, EventArgs e)
        {
            textBox2.Text = FormatText(textBoxEquation.Text);
        }

        private string FormatText(string input)
        {
            input = ReplaceFrac(input);
            input = ReplacePowers(input);
            
            return input;
        }

        private string ReplaceFrac(string input)
        {
            const string matchNumerator = @"\((?<Numerator>((?<p>\()|[^\(\)]|(?(p)(?<-p>\))|))*)\)";
            const string matchDenominator = @"\((?<Denominator>((?<p>\()|[^\(\)]|(?(p)(?<-p>\))|))*)\)";
            const string matchBoth = matchNumerator + @"\/" + matchDenominator;

            System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(input, matchBoth);
            while (m.Success)
            {
                string dNumerator = m.Groups["Numerator"].Value;
                string dDenominator = m.Groups["Denominator"].Value;
                string latex = @"\frac {" + dNumerator.ToString() + "}{" + dDenominator.ToString() + "}";
                input = input.Remove(m.Index, m.Length);
                input = input.Insert(m.Index, latex);


                m = System.Text.RegularExpressions.Regex.Match(input, matchBoth);
            }

            return input;
        }

        private string ReplacePowers(string input)
        {
            const string matchNumerator = @"root:\[(?<Numerator>((?<p>\()|[^\(\)]|(?(p)(?<-p>\))|))*)\]";
            const string matchDenominator = @"\{(?<Denominator>((?<p>\()|[^\(\)]|(?(p)(?<-p>\))|))*)\}";
            const string matchBoth = matchNumerator + matchDenominator;

            System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(input, matchBoth);
            while (m.Success)
            {
                string dNumerator = m.Groups["Numerator"].Value;
                string dDenominator = m.Groups["Denominator"].Value;
                string latex = @"\sqrt [" + dNumerator.ToString() + "]{" + dDenominator.ToString() + "}";
                input = input.Remove(m.Index, m.Length);
                input = input.Insert(m.Index, latex);


                m = System.Text.RegularExpressions.Regex.Match(input, matchBoth);
            }
            return input;
        }

Thanks a lot Ryshad...........
It works............
I hav'nt removed the code but the mistake which I have done was that I added both codes one after the another instead of making functions and the calling those functions.................

I understood my mistake..........
Now the code works fine......................
Thanks once again...............

Thanks a lot Ryshad...........
It works............
I hav'nt removed the code but the mistake which I have done was that I added both codes one after the another instead of making functions and the calling those functions.................

I understood my mistake..........
Now the code works fine......................
Thanks once again...............

You can call them one after the other, it shouldnt cause a problem.(EDIT thats exactly what happens when you call the methods one after the other)

I just moved them to methods to make the code tidier/more readable/easier to maintain. Also, the regex variables are declared as const so you cant change them so if you wanted both pieces of code in the main method you would need to declare new variables for the new regex :)

Either way, glad it all works for ya...remember to mark the thread as solved and turn out the lights when you leave ;)

I added both codes one after the another instead of making functions and the calling those functions

Just a footnote, in C# there arent technically functions. A function is generally a code block that exists outside of an object, a stand alone block of code. As C# is object oriented, all code is associated with a class and is therefore a method.

Many people (including myself on occasion) misuse the terms, so i thought i'd just throw this in to try and un-muddy the waters of c# jargon :p

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.