This PHP script generates Stem Plots in LaTeX using PStricks.
Stem Plots in LaTeX.
Date Created:Saturday February 21st, 2009 12:18 PM
Date Modified:Saturday February 21st, 2009 09:33 PM
<?
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) {
# defaults
$extra = $_POST['extra'];
$yaxis = $_POST['yaxis'];
$xaxis = $_POST['xaxis'];
$radius = $_POST['radius'];
$len = $radius * 1.25;
$bound = $radius * 1.25;
$iradius = $radius + 0.3;
#### HEADER
$content .= <<<EOF
\documentclass{article}
\usepackage{pst-all}
\begin{document}
\psset{unit=0.5in}
\begin{pspicture}(-1,-1)($radius,$radius)
EOF;
$mag = 1;
$min = 100000000;
$max = -10000000;
$ymax = -10000000;
###### ADD NEW DATA
$matrix = $_POST['matrix'];
$matrix = str_replace("\n",';',$matrix);
if (contains(";",$matrix)) $matrix = explode(';', $matrix);
else $matrix = array($matrix);
$len = count($matrix);
$extra = number_format($extra * $radius * 2 / $len, 3, '.', '');
foreach($matrix as $row) {
$row = no_spaces_on_ends($row);
if ($row == '') continue;
$elements = explode(' ', $row);
$px1 = number_format($elements[0] * 2 * $radius / $len , 3, '.', '');
$x1 = $elements[0];
$y1 = $elements[1];
if ($px1 < $min) $min = $px1;
if ($px1 > $max) $max = $px1;
if ($y1 > $ymax) $ymax = $y1;
$content .= '% line'."\n";
$content .= '\psline{-*}('.$px1.',0)('.$px1.','.$y1.')'."\n";
### place values
if (strlen(str_replace('.','',$y1)) > 1) $xx = $px1 + 0.18 * 2;
else $xx = $px1 + 0.25;
if ($y1 != 0) $content .= '% value'."\n";
if ($y1 != 0) $content .= '\rput('.$xx.','.$y1.'){('.$y1.')}'."\n";
if ($y1 >= 0) $yy = -0.4;
else $yy = 0.4;
$content .= '% position'."\n";
$content .= '\rput('.$px1.','.$yy.'){'.$x1.'}'."\n";
} # end foreach
$min = $min - $extra;
$max = $max + $extra;
$mbound = $max - 0.5;
$ymax = $ymax + $extra;
$content .= <<<EOF
% axes
\psline{->}($min,0)($max,0)
\\rput($max,-0.3){ $xaxis }
\\rput($mbound,$ymax){ $yaxis }
EOF;
$content .= <<<EOF
\end{pspicture}
\end{document}
EOF;
} 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";
# defaults
$yaxis = '$Im$';
$xaxis = '$Re$';
$radius = 3;
$quad1 = '$i^0$';
$quad2 = '$i^1$';
$quad3 = '$i^2$';
$quad4 = '$i^3$';
$extra = 1;
}
echo "<pre style=\"border:1px solid black;\">".htmlspecialchars($content)."</pre>";
echo "</body></html>";
$extra = $_POST['extra'];
?>
<form name="input" action=""
method="post">
Matrix Data:
<textarea name="matrix" cols=40 rows=20><?=$_POST['matrix']?></textarea><br>
Scale (radius): <input type="text" name="radius" size="3" value="<?=$radius;?>">
<br>
y-axis: <input type="text" name="yaxis" value="<?=$yaxis;?>">
<br>
x-axis: <input type="text" name="xaxis" value="<?=$xaxis;?>">
<br>
additional length of x-axis: <input type="text" name="extra" value="<?=$extra;?>">
<br>
<input type="hidden" name="latex" value="1"><br>
<input type="submit" value="Submit">
</form>
Downloads:
Download: plots.php 4 KB
Please login or Click Here to register for downloads
LaTeX PStricks Stem Plots 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
