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.


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


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


\startsection[title={Sample Section}]

      [location=right, title={A sample figure}]

  \input knuth

    E = mc^2 

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

      First point

      Second point


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'>
    <sectiontitle>Sample Section</sectiontitle> 
      <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>
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.
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.
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.
          <m:math display="block">
        <formulacaption>(<formulanumber detail="formula">1</formulanumber>)</formulacaption> 
Einstein gave the expression&#xA0;(1). 
      <itemgroup detail="itemize" symbol="n">
          <itemcontent>First point</itemcontent>
          <itemcontent>Second point</itemcontent>

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 ;
  • @mu30 is possible to write the structure \immediate\write{text}?
    – RCV
    Dec 17, 2016 at 10:15
  • @mu30 I think you had implemented in your old office Digritech!!!. Could you help how to create the XML file through \write command?
    – RCV
    Feb 9, 2017 at 11:02

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:


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

Here is a formula:


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>
  <figure refnum="1" xml:id="S0.F1">
    <graphics graphic="ctan_lion" options="scale=0.5"/>
    <!-- %http://www.ctan.org/lion.html -->
  <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">
            <XMTok meaning="equals" role="RELOP">=</XMTok>
            <XMTok role="UNKNOWN" font="italic">e</XMTok>
              <XMTok meaning="times" role="MULOP">⁢</XMTok>
              <XMTok role="UNKNOWN" font="italic">m</XMTok>
                <XMTok role="SUPERSCRIPTOP" scriptpos="post2"/>
                <XMTok role="UNKNOWN" font="italic">c</XMTok>
                <XMTok meaning="2" role="NUMBER">2</XMTok>

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


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .