0

I'm in the process of converting latex to mathml in php.Texmath is the command line tool through which the conversion process can be done.

Latex file:

\mathbf{f} = (f_{1},
f_{2})^{\prime}

test.php:

shell_exec('echo "password" | sudo -S /root/.cabal/bin/texmath latexfile > outputfile');

If I run this php file thorugh command line,it generates the desired output file which is below command line script: php test.php

 <math display="block" xmlns="http://www.w3.org/1998/Math/MathML">
  <mrow>
    <mstyle mathvariant="bold">
      <mi></mi>
    </mstyle>
    <mo>=</mo>
    <mo stretchy="false" form="prefix">(</mo>
    <msub>
      <mi>f</mi>
      <mn>1</mn>
    </msub>
    <mo>,</mo>
    <msub>
      <mi>f</mi>
      <mn>2</mn>
    </msub>
    <msup>
      <mo stretchy="false" form="postfix">)</mo>
      <mo>′</mo>
    </msup>
  </mrow>
</math>

When I run this php file through the browser am getting the output file like

<math display="block" xmlns="http://www.w3.org/1998/Math/MathML">
  <mrow>
    <mstyle mathvariant="bold">
      <mi>

It ignores all the mathtype characters. Why is it gives desired output only through the command line not through the browser. When I open these two files in the editor it displays 'UTF-8'. Is this is character encoding issue? How to solve this.

Learning
  • 848
  • 1
  • 9
  • 32

3 Answers3

1

Be sure to add this line before any output is sent to the browser:

header('Content-Type: application/mathml+xml; charset=utf-8');

This way, you tell the browser your page will be a MathML file with UTF-8 encoding. If you are using a web server, be sure to add the required MIME type to the list of supported formats.

On the other hand, you can add an utf-8 header to the file itself. According to Wikipedia, this should be the header of your file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE math PUBLIC "-//W3C//DTD MathML 2.0//EN"
       "http://www.w3.org/Math/DTD/mathml2/mathml2.dtd">
macl
  • 975
  • 9
  • 15
  • my best guess would be the charset indeed – hanshenrik Feb 23 '15 at 21:06
  • I tried `header('Content-Type: application/xml; charset=utf-8');` It doesn't make any change. Then tried `header('Content-Type: application/mathml+xml; charset=utf-8');` its downloading the php file – Learning Feb 24 '15 at 06:14
  • Additional note: If I run the php file in local host server it gives desired result. If encoding issue in web server then how could local host outputs the file with special character. I don't understand. Am struggling for two days – Learning Feb 24 '15 at 06:23
  • So you are missing the xml header for the MathML file. Let me edit the answer and try, please. – macl Feb 24 '15 at 06:28
  • I tried but no luck :( `shell_exec` function will create an output file. That file should have special character in it. Through command line and local host, If I run php ,output file contains special characters. This server is CentOS here am not getting the file with special characters. If web server is an issue then how come local host works fine? – Learning Feb 24 '15 at 07:06
  • Maybe the problem is not on your code, but on the server shell config not supporting utf-8. Can you run the command line you shell_exec directly on a server shell? Do you get the full file this way? – macl Feb 24 '15 at 07:37
0

You can try to write <mi>&#x1D487;</mi> instead.

I mean: use the hexadecimal entity instead of using the character.

The above number was taken from http://en.wikipedia.org/wiki/Mathematical_Alphanumeric_Symbols#Unicode_chart .

dani31415
  • 459
  • 3
  • 3
  • First I should get the special character only then I can replace with hexadecimal entity. The output file doesn't display special characters itself then how can I change it into hex entity – Learning Feb 24 '15 at 06:06
0

Just I changed lang like below before shell_exec

$locale = 'en_US.utf-8';
setlocale(LC_ALL, $locale);
putenv('LC_ALL='.$locale);
shell_exec('echo "password" | sudo -S /root/.cabal/bin/texmath latexfile > outputfile');

Then it generates file with special characters. Referred from SO

Community
  • 1
  • 1
Learning
  • 848
  • 1
  • 9
  • 32