Displaying equations

Pages: 12
Hello reader,

I have basic skills as a C++ programmer but there isn't or I am just unable to find any information on how to display mathematical equations.

Is it possible to create a DOS-program which would display square roots or fractions in their mathematical forms?

Do you know of any tutorials or other form of information that would help me to get a grasp of what actually needs to be coded to create an equation format?
https://www.w3.org/Math/

Alternatively, if you want to write decent mathematics in documents, then use LaTex. A good implementation is MikTex:
https://miktex.org/about
Last edited on
Ill answer another way:
1) not really. The console does not support the things you need *directly*. I mean, you can do this
1
2
3
       _____________
-    /        X
  \/


to make ascii art sqrt big enough to hold some stuff.

you can also translate it to powers, eg (X+3y)^1/2

and finally you can use some ugliness like

char root = 251; //I think this is a sqrt symbol?
cout << root<< "(x+3y)"

or something like that.

All these are fairly hideous. You are better off using a graphics tool that can handle the symbols (see above). The problem is this: the console uses a default font that lacks the symbols you need. You have to either make do with the symbols that it has that are close, or try to draw them yourself with the art characters, which are extremely limited. Fractions are easier, just do numerator, a row of underscores, and denom. But roots and powers and superscript, subscript, fractional powers, etc, are a nightmare.

you can do a LOT in a windows form by swapping to the symbolic fonts that it supports (and changing the size of the symbols so you can put other stuff around them, and using the sub/superscript font options, etc), but the console does not work that way, it can't use the windows fonts (or if it can, I have never seen it done!).

Last edited on
So, one creates a program that uses such tools?

If so, I still need more info on how to do that. I could not find much help from those pages. Also, I saw Java mentioned quite a few times, I'd like to stick to C++ if that's possible?
Last edited on
Yes, you can create a program that writes the equations graphically, you simply can't do a really clean job in a console window (or, as you put it, a "dos" program). You can do this in c++, but you either need to do it with a graphics library or a GUI program, or alternately write to a file that another program can read, such as MS word, a program that supports the fonts and such needed to write a nice looking result (but writing to word format has its own headaches!!).

C++ can do pretty much anything that can be done on a computer, that is not the issue. I am just telling you that the console is very limited in what it can display.

Most tools like this have a couple of example projects, or you can find one on the web, to show how to use them. Now that you know what you are looking for, see if you can find a starting place / example program?
It seems you misinterpreted a bit jonnin. You made quite clear that using the dos-window isn't an effective way to create formulas. I wasn't pushing for that.

I guess I should come clean with this. I already have created a program using Qt and I'm happy with the graphical user interface for the most part. It does not seem to have capabilities for formula production though. At least not off-the-shelf.

So, what I am trying to ask is exactly this starting place or an example program so I could create my own. If anyone else knows more, please do tell?
You asking to create your own GUI framework instead of using Qt? Or, are you asking for another GUI framework that can handle Math equations?

I have not used this, but from searching it sounds like you could use Qwt, a Qt widget used for technical applications. It apparently supports MathML, but you'd have to do more research to figure out the specifics.
http://qwt.sourceforge.net/
http://qwt.sourceforge.net/class_qwt_text.html (search MathML)
Last edited on
closed account (E0p9LyTq)
Is it possible to create a DOS-program which would display square roots or fractions in their mathematical forms?

You'd need a third-party graphical library to display the symbols, C++ is not designed for that.
You asking to create your own GUI framework instead of using Qt? Or, are you asking for another GUI framework that can handle Math equations?

I have not used this, but from searching it sounds like you could use Qwt, a Qt widget used for technical applications. It apparently supports MathML, but you'd have to do more research to figure out the specifics.

Since I've gotten pretty far with my program in Qt, I'm not too keen on changing the framework.

Qwt is for the widget part of Qt and I am not using that.

I guess the best case would be to create something similar to Qwt but I'd need more information on how to do something like that. This all comes back to the original question, how do you actually program a software which displays formulas?

I mean let's be real here. Using a GUI framework like Qt to create graphics has got nothing to do with programming. The tool does it for you.

You'd need a third-party graphical library to display the symbols, C++ is not designed for that.

Is the third party graphical library you are referring to something like Mathml or Latex? And Again I'd need information about how to implement this in to the program?

jonnin suggested writing a file which another program could then read. I have no interest in doing that although with the limited knowledge I have at the moment, that is the best I could do.
Well, if you are prepared to use ANYTHING that displays MathML (including about half of all web browsers) then you can create suitable HTML with a C++ program.

For MathML see the very first link that I gave you.

Here's a bodged job. Provided your default browser is not Chrome then it might display as is. Otherwise, load the file it creates (mymath.htm) into Firefox. Obviously, if you already link to a suitable widget library that displays MathML then that will be able to do the same. C++ will never do the rendering for you: you will have to call some external library or program to do that.

Instead of MathML you could also create LaTeX formulae instead by a similar approach. MathML and LaTeX are mark-up languages. As above, you need a library or external program to render them.

Wikipedia has a list of math formula editors:
https://en.wikipedia.org/wiki/Formula_editor

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <string>
#include <cstdlib>
#include <fstream>
using namespace std;

const string MATH = "ath";
const string SQRT = "sqrt";
const string IDEN = "i";
const string OPER = "o";
const string NUMB = "n";


//=====================================================================


struct formula
{
    string markup;

    void begin( string elem ){ markup += "<m"  + elem + ">"; }
    void end  ( string elem ){ markup += "</m" + elem + ">"; }
    void item( string elem, string content ){ markup += "<m" + elem + ">" + content + "</m" + elem + ">"; }
    void nl(){ markup += "\n"; }
};


//=====================================================================


string startHTML()
{
   return "<!DOCTYPE html>                \n"
          "<html>                         \n"
          "<head>                         \n"
          "<title>MathML in HTML5</title> \n"
          "</head>                        \n"
          "<body>                         \n";
}


//=====================================================================


string endHTML()
{
   return "</body>\n"
          "</html>\n";
}


//=====================================================================


int main()
{
   const string filename = "mymath.htm";


   // Create formula
   formula s;
   s.begin( MATH );   s.nl();
      s.begin( SQRT );
         s.item( IDEN, "x" );
         s.item( OPER, "+" );
         s.item( NUMB, "2" );
      s.end( SQRT );
      s.nl();
   s.end( MATH );   s.nl();


   // Add headers and footers as desired
   string html = startHTML() + s.markup + endHTML();


   // Write it to screen and file
   ofstream out( filename );
   cout << html;                                                                                                 
    out << html;
   out.close();


   // This might or might not work!!!
   system( filename.c_str() );
}


The html / MathML it creates is the following (and corresponds to the roots example in http://danielscully.co.uk/projects/mathml-guide/ )
<!DOCTYPE html>                
<html>                         
<head>                         
<title>MathML in HTML5</title> 
</head>                        
<body>                         
<math>
<msqrt><mi>x</mi><mo>+</mo><mn>2</mn></msqrt>
</math>
</body>
</html>
Last edited on
I needed to modify it to make it show the sqrt on Chrome, using MathJax from this link.
https://stackoverflow.com/questions/29682207/unable-to-render-mathml-content-in-google-chrome

1
2
3
4
5
6
7
8
9
10
11
12
13
<!DOCTYPE html>                
<html>                         
<head>                         
  <title>MathML in HTML5</title> 
  <script>window.MathJax = { MathML: { extensions: ["mml3.js", "content-mathml.js"]}};</script>
  <script type="text/javascript" async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=MML_HTMLorMML"></script>
</head>                        
<body>                         
  <math>
  <msqrt><mi>x</mi><mo>+</mo><mn>2</mn></msqrt>
  </math>
</body>
</html>


Firefox supports MathML natively, however.
Last edited on
Yes, I'm surprised that Chrome doesn't render MathML natively.

Actually, your MathJax extension makes it render better on Firefox as well, so it's a nice improvement.
you can embed an HTML viewing window in your QT program but I have no clue how you would tell that to render the equations if it does not do that already. Should be able to extend the html widget to do this but it may be extremely involved to get there :(

I know I have seen this done with font extensions too, we had something that let you do equations in ms word, and I can't recall ... it either used fonts to do the work or it created an image you could insert into the document. I never had to use it other than to view the document ... but that implies that someone out there has a library that can generate an image file if that works better for you. I wish I could be more help.
Well, if you are prepared to use ANYTHING that displays MathML (including about half of all web browsers) then you can create suitable HTML with a C++ program.

I don't understand. Can you not read the part where I specifically wrote I do not have interest in writing a file for another program to read? Thanks for the interest though.

As above, you need a library or external program to render them.

Can you please specify what you mean by a library?

you can embed an HTML viewing window in your QT program but I have no clue how you would tell that to render the equations if it does not do that already. Should be able to extend the html widget to do this but it may be extremely involved to get there

Yeah it has become obvious that this will be painfully difficult to do. I only wish the knowledge to make this happen wasn't secret.
I don't understand what parts of Qt you are and aren't allowed to use freely, but here's another link I found.
https://wiki.qt.io/Qt_and_LaTeX_via_KLFBackend
Render LaTeX code via KLFBackend library
KLFBackend is a C++ library originally developed by Philippe Faist to render LaTeX code in KLatexFormula

It has examples further down the page.

The library sounds more suited for Linux but apparently should work on Windows as well. According to the page, you'll also need to download LiveTex as a dependency on Linux, and MikTex as dependency on Windows. I have not tried this library, but figured it might help.
Last edited on
@Sogo, certainly I read your post.

The program I wrote was for demonstration and allowed the process to be tested simply by opening the file in a browser. You don't need to do that: if you can find a renderer for the MathML string then you can call that from within your C++ program. If you prefer LaTeX then @Ganado has given you some references, but the process would be the same: you create the string in a markup language and call some external library routine to render it. As far as I know, MathML and LaTeX have cornered the market in mathematical markup, so I reckon you should choose one of them.

I mean by a "library" a collection of routines that are not part of the C++ standard but which allow you to do other things and are linked in when you create an executable program. You are already using the Qt libraries apparently.
Last edited on
Alright, thank you very much lastchance! That was by far the most helpful post yet.

Also thanks to Ganado. The link does not specify whether the library works for Qt Widgets or Qt Quick, but judging by the code it's for Widgets. Probably going to be a waste of time trying to make that work but I'll have to look into it.

So let's delve a little bit deeper. If I understand correctly, C++ is not suited for making a library which would render a markup language. Is this correct?

How are those libraries created? The library still needs to be converted to bits in which case it should be possible to do in Assembly which leads to the possibility of C++.
SoGo wrote:
If I understand correctly, C++ is not suited for making a library which would render a markup language.


C++ can construct the markup strings (see my example). If you can find a suitable renderer then that would be invoked by calling a function with that markup string as an argument from within a C++ program. Alternatively, C++ can write the HTML to a temporary file (which is what my example currently does) and then something (Qt?) can render that file. I checked it in a browser because that was all I had at hand.

I don't use Qt, so I simply don't know which bolt-ons are required for it to render MathML (or HTML). So I'm not really able to help you with that last stage. If there is an HTML viewer available in Qt as @jonnin suggested then that would work at a pinch ... but I can't comment on that. The Qt wiki at https://wiki.qt.io/Handling_HTML unfortunately has a huge number of broken links at this point.

My example created the HTML by slinging the header and footer around the maths markup. It would be worth adding the extra lines that @Ganado put in the header because that would get past HTML renderers that can't cope with MathML (like Chrome) and also improves the rendered symbols.

Last edited on
SoGo wrote:
If I understand correctly, C++ is not suited for making a library which would render a markup language.

Not at all. If you can't do sth. in C++ you can't do it in any other language - exception maybe assembler.
The problem with C++ is that it is general purpose - that means you need external libraries for almost everything. Standard C++ doesn't know anything about graphics, keyboard, mouse, sound.....

Have you ever searched for a library for your purpose?
Every word processor I know has an equation editor and since they are all written in C++ there must be solutions.
Have you ever searched for a library for your purpose?
Every word processor I know has an equation editor and since they are all written in C++ there must be solutions.

My thought exactly. One would think there was information on how those programs are doing it. Yes, I've searched but haven't been able to find anything so far.
Pages: 12