This PHP script generates LaTeX code for a matrix.
Generate a matrix in LaTeX in a very simple html form.
Date Created:Monday February 16th, 2009 12:48 PM
Date Modified:Saturday February 21st, 2009 09:34 PM
<?
/*
* Author: Dan Lynch
*/
echo "<html>\n<body>\n";
function no_whitespace($str) {
return str_replace(array("\n", "\r", "\t", " ", "\o", "\xOB"), '', $str);
}
function no_returns($title) {
$title = str_replace("\n"," ",$title);
$title = str_replace("\t"," ",$title);
$title = str_replace("\r"," ",$title);
return $title;
}
function no_spaces_on_ends($title) {
if ( $title == '' ) return False;
$title = str_replace("\n"," ",$title);
$title = str_replace("\t"," ",$title);
$title = str_replace("\r"," ",$title);
if (substr($title, 0, 1) == ' ' || substr($title, -1) == ' ') {
if ( substr($title, 0, 1) == ' ' ) $title = substr($title, 1);
if ( substr($title, -1) == ' ' ) $title = substr($title, 0, strlen($title) - 1 );
return no_spaces_on_ends($title);
} else return $title;
}
function contains($str, $content, $ignorecase=true){
if ($ignorecase){
$str = strtolower($str);
$content = strtolower($content);
}
return ereg($str,$content) ? true : false;
}
if ($_POST['latex'] ==1) {
$matrix = $_POST['matrix'];
$matrix = str_replace("\n",';',$matrix);
if (contains(";",$matrix)) $matrix = explode(';', $matrix);
else $matrix = array($matrix);
foreach($matrix as $row) {
$row = no_spaces_on_ends($row);
$elements = explode(' ',$row);
$count = sizeof($elements);
$c = max($c, $count);
if ($row == '') continue;
foreach($elements as $k => $e) {
if ($_POST['astext'] == 1) {
$e = '\\mbox{'.no_spaces_on_ends($e).'}';
$checked =" checked";
} else {
$e = no_whitespace($e);
$checked ='';
}
if ($k + 1 == $count) $l .= $e ." ". '\\\\'."\n";
else $l .= $e . " & ";
}
}
if ($_POST['determinant'] ==1) {
$dchecked = " checked";
$lbrace='\left|';
$rbrace='\right|';
} else if ($_POST['parens'] ==1) {
$parens = " checked";
$lbrace='\left(';
$rbrace='\right)';
} else if ( $_POST['case'] == 1) {
$case = " checked";
} else {
$parens = '';
$dchecked = '';
$case = '';
$lbrace='\left[';
$rbrace='\right]';
}
$r = range(0,$c-1);
foreach($r as $n) $b .="r";
$contents = '$'."\n";
if ($_POST['case'] == 1) {
$contents .= '\begin{cases}'."\n";
} else {
$contents.= $lbrace."\n";
$contents.= '\begin{array}{'.$b.'}'."\n";
}
$contents.= $l;
if ($_POST['case'] == 1) {
$contents .= '\end{cases}'."\n";
} else {
$contents.= '\end{array}'."\n";
$contents.= $rbrace."\n";
}
$contents.= '$'."\n";
} else {
echo "enter in data either separating rows by a return or semi-colon. For example, the following data are equivalent:\n\n";
echo "<pre>";
echo <<<EOF
2 3 4 2 3 4; 5 6 7; 7 8 9
5 6 7
7 8 9
EOF;
echo "</pre>";
echo "input is not limited to numbers. Any latex command or text may be entered.\n\n";
}
echo "<pre style=\"border:1px solid black;\">".htmlspecialchars($contents)."</pre>";
echo "<pre style=\"border:1px solid black;\">".htmlspecialchars(no_returns($contents))."</pre>";
echo "</body></html>";
?>
<form name="input" action=""
method="post">
Matrix Data:
<textarea name="matrix" cols=40 rows=20><?=$_POST['matrix']?></textarea><br>
<?
if ($_POST['astext'] == 1) $checked = ' checked';
else $checked = '';
?>
Entries as Text: <input type="checkbox" name="astext" value="1"<?=$checked;?>>
<br>
Determinant Style: <input type="checkbox" name="determinant" value="1"<?=$dchecked;?>>
<br>
Parenthesis Style: <input type="checkbox" name="parens" value="1"<?=$parens;?>>
<br>
Cases (1-2 columns only): <input type="checkbox" name="case" value="1"<?=$case;?>>
<input type="hidden" name="latex" value="1"><br>
<input type="submit" value="Submit">
</form>
Downloads:
Download: latex.php 4 KB
Please login or Click Here to register for downloads
Generate LaTeX Matrices by Dan Lynch
is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
Based on a work at www.3daet.com
Permissions beyond the scope of this license may be available at http://www.3daet.com
