Debian Codegraph @

xsl/equation.xsl

raw
1 <?xml version='1.0'?>
2 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version='1.0'>
3
4 <!--############################################################################
5 XSLT Stylesheet DocBook -> LaTeX
6 ############################################################################ -->
7
8 <xsl:param name="tex.math.in.alt" select="'latex'"/>
9 <xsl:param name="alt.use" select="0"/>
10 <xsl:param name="equation.default.position">[H]</xsl:param>
11
12
13 <xsl:template match="inlineequation|informalequation" name="equation">
14 <xsl:choose>
15 <xsl:when test="alt and $tex.math.in.alt='latex'">
16 <xsl:apply-templates select="alt" mode="latex"/>
17 </xsl:when>
18 <xsl:when test="alt and (count(child::*)=1 or $alt.use='1')">
19 <!-- alt is simply some text -->
20 <xsl:apply-templates select="alt"/>
21 </xsl:when>
22 <xsl:otherwise>
23 <xsl:apply-templates select="*[not(self::alt)]"/>
24 </xsl:otherwise>
25 </xsl:choose>
26 </xsl:template>
27
28 <xsl:template match="equation">
29 <xsl:variable name="delim">
30 <xsl:call-template name="pi.texmath_delimiters">
31 <xsl:with-param name="node" select="descendant::alt"/>
32 </xsl:call-template>
33 </xsl:variable>
34
35 <xsl:choose>
36 <xsl:when test="title">
37 <xsl:text>&#10;\begin{dbequation}</xsl:text>
38 <!-- float placement preference -->
39 <xsl:choose>
40 <xsl:when test="@floatstyle != ''">
41 <xsl:value-of select="@floatstyle"/>
42 </xsl:when>
43 <xsl:otherwise>
44 <xsl:value-of select="$equation.default.position"/>
45 </xsl:otherwise>
46 </xsl:choose>
47 <xsl:text>&#10;</xsl:text>
48 <xsl:call-template name="equation"/>
49 <xsl:text>&#10;\caption{</xsl:text>
50 <xsl:call-template name="normalize-scape">
51 <xsl:with-param name="string" select="title"/>
52 </xsl:call-template>
53 <xsl:text>}&#10;</xsl:text>
54 <xsl:call-template name="label.id"/>
55 <xsl:text>&#10;\end{dbequation}&#10;</xsl:text>
56 </xsl:when>
57 <xsl:when test="$delim='user'">
58 <!-- The user provide its own environment -->
59 <xsl:call-template name="equation"/>
60 </xsl:when>
61 <xsl:otherwise>
62 <!-- This is an actual LaTeX equation -->
63 <xsl:text>&#10;\begin{equation}&#10;</xsl:text>
64 <xsl:call-template name="label.id"/>
65 <xsl:call-template name="equation"/>
66 <xsl:text>&#10;\end{equation}&#10;</xsl:text>
67 </xsl:otherwise>
68 </xsl:choose>
69 </xsl:template>
70
71
72 <xsl:template match="alt|mathphrase">
73 <xsl:apply-templates/>
74 </xsl:template>
75 <xsl:template match="equation/title"/>
76
77 <!-- Direct copy of the content -->
78
79 <xsl:template match="alt" mode="latex">
80 <xsl:variable name="delim">
81 <xsl:call-template name="pi.texmath_delimiters"/>
82 </xsl:variable>
83
84 <xsl:variable name="tex">
85 <xsl:variable name="text" select="normalize-space(.)"/>
86 <xsl:variable name="len" select="string-length($text)"/>
87 <xsl:choose>
88 <xsl:when test="$delim='user'">
89 <xsl:copy-of select="."/>
90 </xsl:when>
91 <xsl:when test="ancestor::equation[not(child::title)]">
92 <!-- Remove any math mode in an equation environment -->
93 <xsl:choose>
94 <xsl:when test="starts-with($text,'$') and
95 substring($text,$len,$len)='$'">
96 <xsl:copy-of select="substring($text, 2, $len - 2)"/>
97 </xsl:when>
98 <xsl:when test="(starts-with($text,'\[') and
99 substring($text,$len - 1,$len)='\]') or
100 (starts-with($text,'\(') and
101 substring($text,$len - 1,$len)='\)')">
102 <xsl:copy-of select="substring($text, 3, $len - 4)"/>
103 </xsl:when>
104 <xsl:otherwise>
105 <xsl:copy-of select="."/>
106 </xsl:otherwise>
107 </xsl:choose>
108 </xsl:when>
109 <!-- Test to be DB5 compatible, where <alt> can be in other elements -->
110 <xsl:when test="ancestor::equation or
111 ancestor::informalequation or
112 ancestor::inlineequation">
113 <!-- Keep the specified math mode... -->
114 <xsl:choose>
115 <xsl:when test="(starts-with($text,'\[') and
116 substring($text,$len - 1,$len)='\]') or
117 (starts-with($text,'\(') and
118 substring($text,$len - 1,$len)='\)') or
119 (starts-with($text,'$') and
120 substring($text,$len,$len)='$')">
121 <xsl:copy-of select="$text"/>
122 </xsl:when>
123 <!-- ...Or wrap in default math mode -->
124 <xsl:otherwise>
125 <xsl:copy-of select="concat('$', $text, '$')"/>
126 </xsl:otherwise>
127 </xsl:choose>
128 </xsl:when>
129 <xsl:otherwise/>
130 </xsl:choose>
131 </xsl:variable>
132 <!-- Encode it properly -->
133 <xsl:call-template name="scape-encode">
134 <xsl:with-param name="string" select="$tex"/>
135 </xsl:call-template>
136 </xsl:template>
137
138 </xsl:stylesheet>