Newer
Older
XML / modules / maths.xml
<?xml version="1.0" encoding="utf-8"?>

<!--
	Basic maths stuff. Anything more complicated is... complicated. We should probably be using MathML, but we still have to mangle it into HTML regardless.
-->

<stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">


	<!--
		Inline and display formulae.
		
		@style: How to format the maths expression. Valid values are:
			'inline' [default] display expression inline
			'display' display expression in its own paragraph
	-->
	<template name="math-inline" match="math|math[@style='inline']">
		<common formats="/latex/xelatex/">
			<xsl:text>\(</xsl:text>
			<xsl:apply-templates />
			<xsl:text>\)</xsl:text>
		</common>
		<common formats="/html/xhtml/"><xsl:apply-templates /></common>
	</template>
	
	<template name="math-display" match="math[@style='display']">
		<common formats="/latex/xelatex/">
			<xsl:text>\[</xsl:text>
			<xsl:apply-templates />
			<xsl:text>\]</xsl:text>
		</common>
		<common formats="/html/xhtml/">
			<p style="text-align: center;"><xsl:apply-templates /></p>
		</common>
	</template>

	
	<!-- Equation arrays -->
	
	<template name="equation-array" match="equation-array">
		<common formats="/latex/xelatex/">
			<xsl:text>\begin{eqnarray*}</xsl:text>
			<xsl:call-template name="newline-internal" />
			<xsl:apply-templates />
			<xsl:text>\end{eqnarray*}</xsl:text>
		</common>
		<common formats="/html/xhtml/">
			<table class="equation" border="0" style="margin-left: auto; margin-right: auto;">
				<xsl:apply-templates />
			</table>
		</common>
	</template>

	<!--
		Hmm, LaTeX uses "&" as separators: how do we know if there's another cell coming?  Ah, but the LaTeX eqnarray can have no more than three columns, so there's no need to generalise.
	-->
	<template match="equation-array/row">
		<common formats="/latex/xelatex/">
			<xsl:apply-templates select="left" />
			<xsl:text disable-output-escaping="yes">	&amp;	</xsl:text>
			<xsl:apply-templates select="middle" />
			<xsl:text disable-output-escaping="yes">	&amp;	</xsl:text>
			<xsl:apply-templates select="right" />
			<xsl:text>	\\</xsl:text>
			<xsl:call-template name="newline-internal" />
			<xsl:text>\\</xsl:text>
			<xsl:call-template name="newline-internal" />
		</common>
		<common formats="/html/xhtml/">
			<tr>
				<td align="right"><xsl:apply-templates select="left" /></td>
				<td align="center"><xsl:apply-templates select="middle" /></td>
				<td align="left"><xsl:apply-templates select="right" /></td>
			</tr>
		</common>
	</template>
	

	<!--
		Roman (upright) text inside math environments. \mathrm sounds like it's probably more correct, but doesn't retain spaces (which \textup does).
	-->
	<template name="math-text" match="text">
		<common formats="/latex/xelatex/">
			<xsl:text>\textup{</xsl:text>
			<xsl:apply-templates />
			<xsl:text>}</xsl:text>
		</common>
		<common formats="/html/xhtml/"><xsl:apply-templates /></common>
	</template>
	
	
	<!--
		Calligraphic letters (upper case only for LaTeX).
	-->
	<template name="calligraphic-text" match="calligraphic|cursive">
		<common formats="/latex/xelatex/">
			<xsl:text>\ensuremath{\mathcal{</xsl:text>
			<xsl:apply-templates />
			<xsl:text>}}</xsl:text>
		</common>
		<common formats="/html/xhtml/">
			<font face="cursive"><xsl:apply-templates /></font>
		</common>
	</template>
	
	
	<!--
		Digit grouping separator character for large numbers. SI conventions say this should be a space (but we could make it a comma if desired).
	-->
	<template name="digit-group-separator" match="digit-group-separator|digitsep">
		<common><xsl:call-template name="thin-space" /></common>
	</template>
	

	<!-- Mathematical super- and subscript. -->
	<template name="superscript-math" match="math//superscript">
		<common formats="/latex/xelatex/">
			<xsl:text>\ensuremath{^{</xsl:text>
			<xsl:apply-templates />
			<xsl:text>}}</xsl:text>
		</common>
		<common formats="/html/xhtml/"><sup><xsl:apply-templates /></sup></common>
	</template>
	
	<template name="subscript-math" match="math//subscript">
		<common formats="/latex/xelatex/">
			<xsl:text>\ensuremath{_{</xsl:text>
			<xsl:apply-templates />
			<xsl:text>}}</xsl:text>
		</common>
		<common formats="/html/xhtml/"><sub><xsl:apply-templates /></sub></common>
	</template>
	
	
	<!--
		The equivalent of LaTeX's "log-like functions".
		
		@name: The name of the function. We assume the names used by LaTeX here, as they work out correct anyway. There's actually nothing to stop you using any name you like, except that it'll probably die horribly in LaTeX :). [required]
	-->
	<template name="log-like-function" match="function">
		<common formats="/latex/xelatex/">
			<xsl:text>\ensuremath{\</xsl:text>
			<xsl:value-of select="@name" />
			<xsl:text>}</xsl:text>
		</common>
		<common formats="/html/xhtml/"><xsl:value-of select="@name" /></common>
	</template>
	
	
	<!-- Any mathematical variable names. -->
	<template name="math-variable" match="variable|var">
		<common formats="/latex/xelatex/">
			<xsl:text>\ensuremath{\mathit{</xsl:text>
			<xsl:apply-templates />
			<xsl:text>}}</xsl:text>
		</common>
		<common formats="/html/xhtml/"><i><xsl:apply-templates /></i></common>
	</template>
	
	
	<!-- A displayed fraction. Is there a nicer way of doing this in HTML? -->
	<template name="math-fraction" match="fraction">
		<common formats="/latex/xelatex/">
			<xsl:text>\ensuremath{\frac{</xsl:text>
			<xsl:apply-templates select="numerator" />
			<xsl:text>}{</xsl:text>
			<xsl:apply-templates select="denominator" />
			<xsl:text>}}</xsl:text>
		</common>
		<html>
			<sup><xsl:apply-templates select="numerator" /></sup>
			<xsl:text disable-output-escaping="yes">&amp;frasl;</xsl:text>
			<sub><xsl:apply-templates select="denominator" /></sub>
		</html>
		<xhtml>
			<sup><xsl:apply-templates select="numerator" /></sup>
			<span class="unicode">
				<!-- U+2044 FRACTION SLASH -->
				<xsl:text>⁄</xsl:text>
			</span>
			<sub><xsl:apply-templates select="denominator" /></sub>
		</xhtml>
	</template>


</stylesheet>