0

Using Saxon 12.8, I can compile an XML source to a TEX file. I am using the XSL file for Latex from TEIC/Stylesheets to do this.

My XML source however has some math that will need the amsmath package. For example, I might have

<formula notation="latex" type="inline">\tfrac{3}{4}</formula>

I cannot figure out the proper way to get Saxon to include "\usepackage{amsmath}" in the generated tex source so that it will compile. Sure, I could manually edit the generated TEX file but that seems like an error-prone and tedious hack.

I've tried overriding the XSL file

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
    version="3.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:tei="http://www.tei-c.org/ns/1.0"
    exclude-result-prefixes="tei">

<!-- Import the main TEI→LaTeX stylesheet -->
<xsl:import href="/home/myusername/saxon-he/Stylesheets/profiles/tei/latex/to.xsl"/>

<!-- Inject amsmath into the LaTeX preamble -->
<xsl:template name="latex-preamble">
    <xsl:call-template name="latex-preamble"/>
    <xsl:text>\usepackage{amsmath}</xsl:text>
</xsl:template>

<!-- Handle formula elements -->
<xsl:template match="tei:formula[@notation='latex']">
    <xsl:choose>
        <!-- If explicitly marked as block/display -->
        <xsl:when test="@rend='display'">
            <xsl:text>&#10;\[</xsl:text>
            <xsl:value-of select="."/>
            <xsl:text>\]&#10;</xsl:text>
        </xsl:when>
        <!-- Default: inline -->
        <xsl:otherwise>
            <xsl:text>$</xsl:text>
            <xsl:value-of select="."/>
            <xsl:text>$</xsl:text>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

</xsl:stylesheet>

But it doesn't work (that is, the relavent override section above doesn't include the amsmath package so the genderated TEX file doesn't compile, complaining about undefined control sequences). This seems like it should be really easy to do. I just want to tell saxon I'll need to include the line "\usepackage{amsmath}" in my generated TEX source. How do I do this?

3
  • What "it doesn't work" refer to exactly? Do you get an error? If so, which one exactly? Do you get the wrong result? If so, which one exactly? It might also help if you link to the exact XSLT your sample imports. Commented Aug 30 at 10:27
  • 1
    It appears this is more related to using those particular TEI or TEI to LaTeX stylesheets than to Saxon. I guess it depends on which configuration or extension options those stylesheets have and of course to a certain extent on a knowledge of the target format LaTeX Commented Aug 30 at 10:45
  • 1
    As far as I can take it from the documentation, there are templates latexPackages and latextSetup and latexBegin that you can override, although there are warnings like "The basic LaTeX setup which you should not really tinker with unless you really understand why and how. ". Let's hope someone with TEI and TEI to LaTeX skills comes along to help you out. Commented Aug 30 at 10:58

2 Answers 2

1

I also think putting <?tex \usepackage{amsmath}?> into the source document would do the trick.

Sign up to request clarification or add additional context in comments.

Comments

1

I found not one but two ways. The main issue seems to be that "latex-preamble" no longer works and "latexPreambleHook" should be used instead. So a change happened that broke backwards compatibility.

Here are the solutions that work:

  1. You can simply pass a parameter named "userpackage" when running saxon like so:
java -jar ~/saxon-he/saxon-he-12.8.jar -s:mydocument.xml -xsl:override.xsl -o:mydocument.tex userpackage="amsmath"

The "userpackage" parameter gets passed to a hook called "latexPreambleHook" in the latex xslt file (which I'm importing from within my override xslt file).

  1. Perhaps more elegantly than passing a parameter to saxon, you can use that hook in your override.xsl file to add to the latex header directly. For example,
<xsl:template name="latexPreambleHook">
    <xsl:text>\usepackage{amsmath}</xsl:text>
    <!-- add more commands if needed -->
</xsl:template>

Finding these solutions took hours of reading obtuse documentation and a lot of trial and error. :-(

Anyway, the generated TEX file now uses the amsmath package and successfully compiles as desired.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.