xml2tex at tug 2014

36
xml2tex The easy way to define XML-to-LaTeX converters Created by / Keiichiro Shikano @golden_lucky

Upload: keiichiro-shikano

Post on 18-Dec-2014

330 views

Category:

Technology


3 download

DESCRIPTION

At TUG 2014 in Portland on July 28. Introducing xml2tex, a framework for using XML syntax as a source of LaTeX.

TRANSCRIPT

Page 1: xml2tex at TUG 2014

xml2texThe easy way to define

XML-to-LaTeX convertersCreated by / Keiichiro Shikano @golden_lucky

Page 2: xml2tex at TUG 2014

What xml2tex is NOT.xml2tex is NOT a markup.xml2tex is NOT a ready-to-use converter application.

Page 3: xml2tex at TUG 2014

xml2tex is a framework to give XMLsyntax a nice presentation layer

using LaTeX.

Page 4: xml2tex at TUG 2014

xml2tex is a framework to give XMLsyntax a nice presentation layer

using LaTeX.xml2tex is a framework for usingXML syntax as a source of LaTeX.

Page 5: xml2tex at TUG 2014

Overview of xml2tex

Page 6: xml2tex at TUG 2014

Accusation”Are you idiot?

Why on earth are you going to use ugly XML syntax insteadsome more concise syntax?“

Page 7: xml2tex at TUG 2014

Our Way of Creating a BookFirst of all, we don't want to use WYSIWYG application tocreate a book being sold.

We usually maintainmanuscripts using VCS(github) to the very last.It makes working togethersmoother over the Internet.LaTeX is one of the best toolsfor typesetting in this kind ofenvironment.

Taking advantage of xml2tex, we could use XML asmanuscripts.

XM

Page 8: xml2tex at TUG 2014

Alternative approachesUse XSLT and XSL-FO for typesetting.Use XSLT for getting LaTeX.Convert them into LaTeX (once and for all).Or better yet, convert them into other standard markups ormarkdowns (and use the corresponding environment likeDocBook, Sphinx, pandoc, TeXML, and so on).

Page 9: xml2tex at TUG 2014

Pros of using XML (which LaTeX also has)

We need a variety of meta data to create a book; editor'scomments, index entries, and .We need to be able to achieve rich enough page layout for a

.

original texts

commercial book

Page 10: xml2tex at TUG 2014

We usually let the translator put down the correspondingJapanese text below each original paragraphs like this.

A real example of XML source fortranslating projects

Page 11: xml2tex at TUG 2014

A real example of unusual pagelayout

Page 12: xml2tex at TUG 2014

A real example of using meta data

Page 13: xml2tex at TUG 2014

Pros of using XML, instead of LaTeX

Authors of technical books tend to have HTML literacy,provided that there's no excessive information against human.Easy to confirm the appearance of structured text just byWeb browsers, provided that there's an appropriate CSS.It's technically possible to generate EPUB as well as PDF.

Page 14: xml2tex at TUG 2014

Cons of using XMLIf there's excessive information just for machine, it becomeshard to edit the manuscript.If there's no appropriate CSS, you have to rely only on theabstract structure of documents.It's technically possible, but not trivial to generate EPUB.

Page 15: xml2tex at TUG 2014

More to the point...Creating PDFs from XML often requires some proprietarysoftware and XSLT!

Page 16: xml2tex at TUG 2014

What we need is an easy way to get XML-to-LaTeX converter, as needed.

Without any priorinformation for thestructure,Without any restriction forthe structure,Without explicitly writingXML parser every time,

With a profound support for generating LaTeX documents.(XSLT won't be the best solution!)

TeX

Page 17: xml2tex at TUG 2014

xml2tex — our approach

Page 18: xml2tex at TUG 2014

How to get a LaTeX representationof this XML?

Note that it was converted from a DTP application.ppp

Page 19: xml2tex at TUG 2014

Things you can tell just by looking at the XML

It at least has the elements named , , , , ,

, , and .It at least has a XML character entity .

is the processing instruction, which generallyseems to be useless in LaTeX.Characters like , and may need to be escaped.

Page 20: xml2tex at TUG 2014

All you need to tell xml2tex

That's it!Save these lines into a file, and it can be used as a kind of

specification for xml2tex.

Page 21: xml2tex at TUG 2014

Converting XML with the rule file

where is the rule file defined before.

Page 22: xml2tex at TUG 2014

pdfLaTeX result

Page 23: xml2tex at TUG 2014

Generating LaTeX syntax from thedocument tree is defined as a rule.

1. Preceding string, or a thunk which returns a precedingstring. Possible example is .

2. A procedure from string to another string. is one ofsuch procedures. It takes a string and returns a string inwhich special characters in LaTeX are escaped properly.

3. Following string, or a thunk which returns a followingstring. Possible example is .

Page 24: xml2tex at TUG 2014

Let the defined rule map the contentfrom XML element to LaTeX syntax,

based on the tag name.

Just putting down these definitions for each XML tags isenough to convert the XML into LaTeX.

Page 25: xml2tex at TUG 2014

Supportive features in defining rules

generates a rule for creating a LaTeX command with the contents.

generates a rule for creating a LaTeX environment with the contents.

generates a rule for putting down all the contents withnecessary escaping.

generates a rule for discarding the contents.ts.

Page 26: xml2tex at TUG 2014

Default rule is "through"

The elements you haven't define any explicit rule are indicatedwhile the conversion. It helps you try detecting the unknown

elements within the given XML file.L e.fil

Page 27: xml2tex at TUG 2014

Supportive features in adoring tree

returns True if the node is directly under or ...There's a lot more similar functions like (takes theparent name), , , and so on.

returns a string value if the node has an attribute of the.

Page 28: xml2tex at TUG 2014

An example of and

-functions can be used within .Also note that takes a procedure insteadof a fixed string for its first argument.

Page 29: xml2tex at TUG 2014

An example of (getting attribute value)

Note that is a syntax for a string interpolation. Itactually a feature of Gauche, a Scheme programminglanguage on which xml2tex works.

Page 30: xml2tex at TUG 2014

More practical example —HTML tables

Page 31: xml2tex at TUG 2014

Possible rule to convert HTML tableto LaTeX's tabular environment

It requires transformation of the tree, before applying a ruledefined by .To transform the tree, use keyword within

.

Page 32: xml2tex at TUG 2014

Results

1 2 3 4A BC D

Page 33: xml2tex at TUG 2014

Practical example 2 —Footnotes

Page 34: xml2tex at TUG 2014

Possible rule to make LaTeX's from at the

bottom

This time you need to get hold of a subtree, and attach it toanother node.Things has been rather messy, but still works.ill wo

Page 35: xml2tex at TUG 2014

Results

Page 36: xml2tex at TUG 2014

ConclusionXML is not bad for making books.

Simple markups/markdowns won't offer you a rich layout.LaTeX will provide a concrete presentation layer for XML documents.

However, using XSLT for converting XML to LaTeX is a hard way, because XSLT is a toolfor getting another XML from a XML.

One major missing-link is a complete and lightweight wayof getting LaTeX from XML.

xml2tex would be a solution with its declarative style of defining whatever conversion rules.ConTeXt's XML support is probably another solution.

It requires some experiences on XML, as well as LaTeX.In addition to that, xml2tex requires Scheme experience.