views:

87

answers:

1

I have a text string, for ex. 'A vehicle travels from A to B, distance {$d} km at constant speed. While returning back to A on same path it {$variation} its speed by {$v} km/hr. The total time of journey is {$t} hours. Find the original speed of vehicle.'

The variables in the curly brackets are to be replaced by appropriate latex equation. I'm using php's preg_replace to replace the variables with latex commands. Unfortunately, my latex commands are coming as it is. It is not processed by mathjax.

For ex, above text becomes 'A vehicle travels from A to B, distance 1 km at constant speed. While returning back to A on same path it increased its speed by (\frac{3}{2}) km/hr. The total time of journey is 1 hours. Find the original speed of vehicle.' The frac is shown as it is.

What is wrong here? Please ask me if you need any more info. Thanks

A: 

I'm guessing you aren't quoting the replacement text properly. Replacing just the first two variables, tested using spaweditor's regex tool:

<?php
$string = 'A vehicle travels from A to B, distance {$d} km at constant speed. While returning back to A on same path it {$variation} its speed by {$v} km/hr. The total time of journey is {$t} hours. Find the original speed of vehicle.';
$patns = array();
$patns[0] = '/\{\$d\}/';
$patns[1] = '/\{\$variation\}/';
$repns = array();
$repns[0] = '1 km';
$repns[1] = '\\(\\frac{3}{2}\\)';
echo preg_replace($patns, $repns, $string);
?>

If this doesn't work, show the full example of how you are embedding the text in the page.

Postscript The point being, the latex command for inline maths is \( ... \) - yours is missing the backslashes.

Charles Stewart
yes. you are right.. i missed the inline maths command.. thanks
robert