# Thread: Hi - I'm a LaTeX/MathJax Newbie

1. Hi:

I'm new here.

I'm heavily involved in math too and need a site where I can get some answers or new ideas once in a while. From what I've seen so far, this looks like a good place to visit.

I noticed that this site is written in PHP and uses LaTeX/MathJax, something I just recently discovered and have been trying to implement it on my own PHP-based web site.

Trying to figure out how to use MathJax along with PHP without conflicts is what brought me here in search of a solution from someone who may be a MathJax user and also into math coding in PHP.

Using MathJax seems simple enough and I've been converting several of the HTML pages on my web site to use MathJax expressions in place of the old JPEG graphics to display equations. The image quality is far superior.

So far, so good.

The problem I have yet to solve is using MathJax to display equations within a web page written in PHP, rather than just pure, plain HTML.

When I make an on-line calculator in PHP, below the calculator, I usually display the formulas that were used. The displayed formulas have always been ordinary GIF, PNG or JPEG images. Now I wish to use MathJax expressions to replace those graphics images. That's where the problem starts.

PHP uses the dollar sign ($) symbol to mark where computed variables are inserted into the dynamic web page while MathJax uses the dollar sign symbol within the same web page to identify blocks of math code to display. That breaks the PHP program as soon as a MathJax expression is encountered. All sorts of unpredictable gibberish results. I'd like to know how I can write a program in pure PHP and still be able to display a MathJax expression within the page it generates without the conflicts caused by the dollar signs and other symbols. Many of my web pages are written exclusively in PHP and I can't get the best use out of MathJax until I resolve this annoying PHP/MathJax conflict. Thanks for any help or suggestions. 2. Hello Jay, welcome to MHB! In your definition script for MathJax, you can define tags for inline math other than the dollar sign. In the tex2jax processor, replace the dollar signs with another character of your choosing. If you are unsure what to do, copy your definition script here, tell me what character you would like to use, and I will edit it for you. 3. PHP is executed server-side while Mathjax is implemented using javascript, which is run client-side so there shouldn't be any conflict. Anything within the <script></script> tags is not run as PHP. I would suggest looking at the official or going to their for specific questions. We are very happy you found us and look forward to any math questions you have in the future. 4. Thread Author Originally Posted by MarkFL Hello Jay, welcome to MHB! In your definition script for MathJax, you can define tags for inline math other than the dollar sign. In the tex2jax processor, replace the dollar signs with another character of your choosing. If you are unsure what to do, copy your definition script here, tell me what character you would like to use, and I will edit it for you. Thanks for the suggestion. I experimented with the MathJax config files and got your suggestion to work - but still only for plain HTML files. Apparently, simply using MathJax with PHP is a lot more complicated than I thought it would be. It seems that there are several characters, other than just the dollar sign, that PHP and MathJax both want to process in different ways at the same time. All is not lost. I found what seems to be a way around the problem by converting the MathJax Tex into MathML code and using that instead. The only drawback is that it takes a huge amount of code compared to simple MathJax expressions. But it works. Here are the files I'm currently experimenting with. They are both the same except one is plain HTML, the other is PHP. What I'm trying to do is build a basic PHP computations template page into which I can also insert MathJax expressions without conflict. Then I can use it as a starting point for developing calculator programs built around it while also displaying the formulas used on the same page below the calculator with MathJax. Here's a simple example of the idea: All the problems began when I tried to use MathJax to display the equations in the lower portion of the calculator page. I finally gave up and used plain PNG graphics made from MathJax screen caps instead. Seemed like such a little thing at first, but PHP and MathJax just won't cooperate. They act like they're married! LOL I've considered doing some of my math in JavaScript, but many of my programs need mathematical computational power far beyond the ability of JavaScript and only PHP easily provides it, like computing the exact value of 9999 factorial, all 35656 digits of it. I need that kind of computing power in some of the work I do, but JavaScript can't handle it, otherwise I would use it in place of PHP. Until I find a way to integrate PHP with MathJax, I'll insert MathML code instead of MathJax expressions. Thanx for your help. It got me experimenting. 5. Thread Author Originally Posted by Jameson PHP is executed server-side while Mathjax is implemented using javascript, which is run client-side so there shouldn't be any conflict. Anything within the <script></script> tags is not run as PHP. I would suggest looking at the official or going to their for specific questions. We are very happy you found us and look forward to any math questions you have in the future. Hi: In this particular case, the PHP dynamically generated page includes MathJax expressions along with PHP variables within the page returned to the client's browser. Apparently the conflict arises because some of the embedded MathJax code conflicts with PHP code attempting to evaluate the embedded MathJax expressions as if they were PHP expressions. However, at some significant, lengthy code overhead, I can still use MathML expressions without the conflicts - so far. I can still use MathJax, just not quite as easily as I thought with PHP. Thanks for the info. I'm still very new to MathJax, but eventually I'll figure it all out. 6. I'm not a programmer so I think you'll get better help at the link I posted in my previous post, but I'm looking at and trying to see what you're doing. If I think of something I'll post back. EDIT: is some basic information on working with PHP and javascript together. You might be able to get around this with a simple "echo" command for the math stuff? I've never seen the syntax construction you are using so it's a foreign to me, but good luck. EDIT2: Ok, my last suggestion... why must you wrap the whole page in php tags? I think you can save a .php file with html in it and it will run just fine. Use the opening and closing php tags for the stuff you need to code in php and the rest can be in plain html. What happens if you run the same code without the opening two lines and last two lines and simply save it as a php file? 7. Thread Author Originally Posted by Jameson ... EDIT2: Ok, my last suggestion... why must you wrap the whole page in php tags? I think you can save a .php file with html in it and it will run just fine. Use the opening and closing php tags for the stuff you need to code in php and the rest can be in plain html. What happens if you run the same code without the opening two lines and last two lines and simply save it as a php file? When I delete the lines you mentioned, it reverts back to behaving like plain HTML and the equations display perfectly. I just hoped I could make it work the same way when written in PHP. Lots of my pages perform internal PHP math and other operations, like computing dynamic table structures based on internal computations. Anyway, I got around the problem by using that messy MathML code instead of the MathJax code. But so far, at least I can mix PHP and MathJax without problems that way. That's all I really wanted. A way to embed math equations in my PHP pages. It sure beats working for hours with Photoshop, hand making every graphic by cut/paste/crop, etc. Most of my PHP programs can be written in the form of a single file when I mix PHP/HTML/JavaScript. It wasn't until I started using MathJax that JavaScript caused any problems, but now that I know why, I can work around it. Your suggestion was still helpful, since it pointed me in the right direction. Thanx 8. You can still use PHP on that page though. You just put the opening and closing php tags anywhere you want in the document and it will work fine. This seems more logical than wrapping the whole document in tags. You can still perform the calculations or whatever you need to in PHP and then output the results to be rendered. Maybe you're saying this. Let's say after a bunch of calculations you are left with this PHP variable... \$result. You want that to be rendered using Mathjax? Can you show me an example of something in PHP you want to be rendered?

I really think this can be fixed but if you want the quickest solution then maybe using a different delimiter would be the way to go, as Mark suggested.

Originally Posted by Jameson
You can still use PHP on that page though. ....

I really think this can be fixed but if you want the quickest solution then maybe using a different delimiter would be the way to go, as Mark suggested.
Hi again:

Thanks for the suggestions.

Between Mark and you, I think I have it under control now. I simply needed to change my programming technique when using MathJax.

I think I got the problem under control, thanks to the useful advice given here.

The simplest solution is to change the way I write PHP programs when MathJax is used.

So far, the easiest solution I've found is to use MathML code rather than the simpler MathJax code within a purely PHP generated page. I haven't had any conflicts yet by doing it that way. It just takes more code to do it, but nothing too extreme.

My other pages, written in pure HTML that simply discuss and display equations, work without any problems.

Here's the first page I ever published using MathJax (HTML only):

The MathJax graphics are much sharper than the old GIFs and you can zoom in without losing detail. Now I can't live without MathJax. LOL

I like to keep my coding as simple and self-contained as possible with a minimum of external files. Most of the time I create a programs consisting one main PHP program file (index.php) and one or two data related data files. This way I can make fully transportable projects that run right out of the box and require no special setup.

What I'm doing is creating specialized, completely ready-to-run PHP calculators that I can distribute as stand-alone packages that should immediately work on virtually any PHP5-enabled server. Anyone should be able to download and run the packages by simply adding one folder to a web site.

One of the things I like to do is share code with others who have similar interests in higher mathematics and the computational aspects of astronomy, physics and science in general.

Here's a few examples, all of which are single, stand-alone programs:

etc...

Prior to publishing programs on the WWW, I used to only use them on my local Apache server on my desktop. They weren't available on the public Internet. Now I want to move them to the WWW so anyone can use them, since quite a few people have expressed an interest in some of my work and since I discovered that Cambrige University Library links to a program my web site as an astronomical science reference. Interesting, since there are no ads and nothing commercial on my web site.

Eventually, I'll build up a decent library of PHP-based calculators for various specialized computations, complete with source code and educational material on how it all works so anyone else can do likewise.

One of the things that bugs me is on-line calculators that NEVER show how any of the computations are actually done, provide no source code and never show anyone else how to do it for themselves. I'm a retired math/science educator want to add my own humble contribution to the WWW and show others how do it as well. You have lots of free time when you're retired and bored to distraction!

This forum is a rich source of ideas I can use for my own web site. Lots of excellent information here.

Thanks for the useful suggestions. I'll be visiting here very often.

So, as far as I'm concerned, the problem is resolved and I can now move forward with my work.

10. Hi,
I have a same problem.
I can't solve my problem using the method discussed above.

So I am sending my index.php file for verification.

#### Posting Permissions

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