Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am writing Latex to XML. While compiling pdflatex we generate a xml file using \immediate\write{text} in tagged commands. But how can i get normal text to XML file. Can any one explain with samples.

share|improve this question
1  
Related: Converting ConTeXt document to HTML –  Marco Dec 4 '12 at 9:09

2 Answers 2

I don't know about the possible solutions with LaTeX and pdftex engine, but ConTeXt MkIV (which uses LuaTeX engine) supports an XML backend that is used to generate EPUB and tagged PDF.

To get the XML output from a file, you need to add

\setupbackend[export=yes]

As an example, consider a simple file with some figures, math, and lists.

\setupbackend[export=yes]
\setuppapersize[A5]
\starttext

\startsection[title={Sample Section}]

  \startplacefigure
      [location=right, title={A sample figure}]
      \externalfigure[cow][width=2cm]
  \stopplacefigure

  \input knuth

  \placeformula[eq:1]
  \startformula
    E = mc^2 
  \stopformula

  Einstein gave the expression~(\in[eq:1]).

  \startitemize[n]
    \startitem
      First point
    \stopitem

    \startitem
      Second point
    \stopitem
  \stopitemize
\stopsection

\stoptext

which generates the following PDF output

enter image description here

In addition, it generates the following XML file \jobname.export (notice that all the structural information is retained and math is exported to MathML)

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>

<!-- input filename   : test              -->
<!-- processing date  : Tue Dec  4 00:21:55 2012 -->
<!-- context version  : 2012.11.16 23:51  -->
<!-- exporter version : 0.30              -->


<document language="en" file="test" date="Tue Dec  4 00:21:55 2012" context="2012.11.16 23:51" version="0.30" xmlns:m="http://www.w3.org/1998/Math/MathML">
  <section detail="section" location='aut:1'>
    <sectionnumber>1</sectionnumber> 
    <sectiontitle>Sample Section</sectiontitle> 
    <sectioncontent>
      <float detail="figure" location='aut:2'>
        <floatcontent><image name="cow" id='image-1' width='2.000cm' height='1.455cm'></image></floatcontent>
        <floatcaption><floatlabel detail="figure">Figure </floatlabel><floatnumber detail="figure">1</floatnumber> <floattext>A sample figure</floattext></floatcaption>
      </float>
Thus, I came to the conclusion that the designer of a new system must not only be the implementer and first large--scale user; the designer should also write the first user manual.
      <break/>
The separation of any of these four components would have hurt TEX significantly. If I had not participated fully in all these activities, literally hundreds of improvements would never have been made, because I would never have thought of them or perceived why they were important.
      <break/>
But a system cannot be successful if it is too strongly influenced by a single person. Once the initial design is complete and fairly robust, the real test begins as people with many different viewpoints undertake their own experiments.
      <formula>
        <formulacontent>
          <m:math display="block">
            <m:mrow>
              <m:mi>๐ธ</m:mi>
              <m:mo>=</m:mo>
              <m:mi>๐‘š</m:mi>
              <m:msup>
                <m:mi>๐‘</m:mi>
                <m:mn>2</m:mn>
              </m:msup>
            </m:mrow>
          </m:math>
        </formulacontent>
        <formulacaption>(<formulanumber detail="formula">1</formulanumber>)</formulacaption> 
      </formula>
Einstein gave the expression&#xA0;(1). 
      <itemgroup detail="itemize" symbol="n">
        <item>
          <itemtag>1.</itemtag>
          <itemcontent>First point</itemcontent>
        </item>
        <item>
          <itemtag>2.</itemtag>
          <itemcontent>Second point</itemcontent>
        </item>
      </itemgroup>
    </sectioncontent>
  </section>
</document>

Two auxiliary css files are also generated, the first \jobname-style.css which contains css for the font setup and any ConTeXt defined environments and colors:

/* styles for file test.export */

document {
    font-size  : 12pt !important ;
    max-width  : 300pt !important ;
    text-align : justify !important ;
    hyphens    : inherited !important ;
}

and a \jobname-images.css file that contains information about the images used in the tex file.

/* images for file test.export */

image[id="image-1"] {
    display           : block ;
    background-image  : url(cow) ;
    background-size   : 100% auto ;
    background-repeat : no-repeat ;
    width             : 2.000cm ;
    height            : 1.455cm ;
}
share|improve this answer

I think there is also another way: LaTeXML: A LaTeX to XML Converter.

After having install it, one might proceed as follows.

Consider the following MWE called test_xml.tex:

\documentclass[a4paper,11pt]{article}
\usepackage{graphicx}

\begin{document}
Here is some text that precedes the image.
\begin{figure}
\includegraphics[scale=0.5]{ctan_lion} % http://www.ctan.org/lion.html
\end{figure}

Here is a formula:
\begin{equation}
e=mc^2
\end{equation}

\end{document}

We have one external module, graphicx, that could be bind directly: see (Loading Bindings) page 5 of the manual and the appendix B. Thus we just have to do with the terminal:

latexml --preload=graphicx.sty --preload=LaTeX.pool --destination=test_xml.xml test_xml

and the resultant test_xml.xml will be:

<?xml version="1.0" encoding="UTF-8"?>
<?latexml searchpaths=".,//home/claudio/Scrivania/prova/"?>
<?latexml package="graphicx"?>
<?latexml options="a4paper,11pt" class="article"?>
<?latexml package="graphicx"?>
<?latexml RelaxNGSchema="LaTeXML"?>
<document xmlns="http://dlmf.nist.gov/LaTeXML">
  <para xml:id="p1">
    <p>Here is some text that precedes the image.</p>
  </para>
  <figure refnum="1" xml:id="S0.F1">
    <graphics graphic="ctan_lion" options="scale=0.5"/>
    <!-- %http://www.ctan.org/lion.html -->
  </figure>
  <para xml:id="p2">
    <p>Here is a formula:</p>
    <equation refnum="1" xml:id="S0.E1">
      <Math mode="display" tex="e=mc^{2}" xml:id="S0.E1.m1" text="e = m * c ^ 2">
        <XMath>
          <XMApp>
            <XMTok meaning="equals" role="RELOP">=</XMTok>
            <XMTok role="UNKNOWN" font="italic">e</XMTok>
            <XMApp>
              <XMTok meaning="times" role="MULOP">โข</XMTok>
              <XMTok role="UNKNOWN" font="italic">m</XMTok>
              <XMApp>
                <XMTok role="SUPERSCRIPTOP" scriptpos="post2"/>
                <XMTok role="UNKNOWN" font="italic">c</XMTok>
                <XMTok meaning="2" role="NUMBER">2</XMTok>
              </XMApp>
            </XMApp>
          </XMApp>
        </XMath>
      </Math>
    </equation>
  </para>
</document>

Now, it is also possible to perform some postprocessing to obtain for example .xhtml or .html files (not only these files, of course, see the manual for reference).

For a .xhtml file:

latexmlpost --graphicimages --destination=test_xml.xhtml test_xml

For a .html file:

latexmlpost --format=html --graphicimages --destination=test_xml.html test_xml

These operations will convert formulas and images automatically (because of the --graphicimages option). The result will be something like:

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.