Is there any GUI fast way for extracting content of .tex files? (Stripping all tags and meta-data)

Among TeX editors which one support export content to plain .txt file?

(I found some command-line tools and some RegExp solutions, but using them is not so convenience. It seems that this is an essential need, I wonder why there is not a known solution for it.)

  • It is basically a hard problem, the most reliable way is probably to use one of the tex to html convertors say latexml or tex4ht and then lose the html markup to get text. Consider for example getting the text content of mirrors.ctan.org/macros/plain/contrib/misc/xii.tex you basically need a full TeX system to parse the input. – David Carlisle Sep 13 '12 at 16:43
  • 2
    Another option is to convert the compile to document to pdf (or dvi), then extract the text from there. – Bruno Le Floch Sep 13 '12 at 16:44
  • @BrunoLeFloch Extracting form PDF do not produce desired result in particular when document is not Latin script – PHPst Sep 13 '12 at 17:18
  • 2
    detex file.tex > file.txt is so simple and faster that I never felt the need of a GUI for that. – Fran Sep 13 '12 at 17:23
  • @Fran Unfortunately it can not handle included documents at least on my Windows 7 64b – PHPst Sep 13 '12 at 17:41

I used the following approach to extract different paragraphs out of the tex files to write it in a separate text file (and do word counting). Using LuaLaTeX it is easy to set a hook after TeX did all the work for creating a paragraph (it means after expand all macros, calc the line breaks and created the whole paragraph). In the callback-function of the hook now one can write all words which appears in the resulting pdf in a separate text file. For pure text it works very well, but I don't know how it deals with special characters, tables, footnotes,... Maybe one of the LaTeX and LuaLaTeX experts can check this and improve it. With disabling the ligatures it did the job for me.

The approach only works with LuaLaTeX, but I use a switch (\ifluatex) to activate the function. When I need my pdf I compile the document with pdfLaTeX and when I need the extracted paragraphs in the separate text file I compile it with LuaLaTeX.


-- clear the file
file = io.open("output.txt", "w")

exportParagraph = false

function exportText (head)

    if exportParagraph == false then
        --if you return nil no pdf will be created
        -- return nil 
        return head        

    -- open the file in append-modus
    local out = io.open("output.txt", "a")
    local wordCounter = 0

    -- loop over all hboxes in the current paragraph
    for line in node.traverse_id (node.id("hlist"), head) do

        -- loop over each element in the line
        for item in node.traverse (line.list) do
            -- check if the element is a char
            if item.id == node.id("glyph") then
            -- check if the element is a 'space'
            elseif item.id == node.id("glue") then
                wordCounter = wordCounter + 1
                out:write(" ")
        -- a newline in the file after each (tex)line

    wordCounter = wordCounter - 1
    out:write("Words: "..wordCounter.."\n")

    -- a newline in the file after each paragraph 

    exportParagraph = false  

    --if you return nil no pdf will be created
    -- return nil
    return head    

function disableLigatures(head)
    -- disable ligatures

function SetExportParagraph(export)
    exportParagraph = export

callback.register("ligaturing", disableLigatures)
callback.register("post_linebreak_filter", exportText)



ff fi Lorem ipsum dolor sit amet, \textbf{consectetuer adipiscing elit. Ut purus elit,
vestibulum ut, placerat ac, adipiscing vitae, felis.} Curabitur dictum gravida
mauris. Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna.
Donec vehicula augue eu neque. Pellentesque habitant morbi tristique senectus
et netus et malesuada fames ac turpis egestas. Mauris ut leo. Cras viverra
metus rhoncus sem. Nulla et lectus vestibulum urna fringilla ultrices. Phasellus
eu tellus sit amet tortor gravida placerat. Integer sapien est, iaculis in, pretium
quis, viverra ac, nunc. Praesent eget sem vel leo ultrices bibendum. Aenean
faucibus. Morbi dolor nulla, malesuada eu, pulvinar at, mollis ac, nulla. Cur-
abitur auctor semper nulla. Donec varius orci eget risus. Duis nibh mi, congue
eu, accumsan eleifend, sagittis quis, diam. Duis eget orci sit amet orci dignissim

Nam dui ligula, fringilla a, euismod sodales, sollicitudin vel, wisi. Morbi
auctor lorem non justo. Nam lacus libero, pretium at, lobortis vitae, ultricies et,
tellus. Donec aliquet, tortor sed accumsan bibendum, erat ligula aliquet magna,
vitae ornare odio metus a mi. Morbi ac orci et nisl hendrerit mollis. Suspendisse
ut massa. Cras nec ante. Pellentesque a nulla. Cum sociis natoque penatibus et
magnis dis parturient montes, nascetur ridiculus mus. Aliquam tincidunt urna.
Nulla ullamcorper vestibulum turpis. Pellentesque cursus luctus mauris.

Nulla malesuada porttitor diam. Donec felis erat, congue non, volutpat at,
tincidunt tristique, libero. Vivamus viverra fermentum felis. Donec nonummy
pellentesque ante. Phasellus adipiscing semper elit. Proin fermentum massa
ac quam. Sed diam turpis, molestie vitae, placerat a, molestie nec, leo. Mae-
cenas lacinia. 

Nam ipsum ligula, eleifend at, accumsan nec, suscipit a, ipsum.
Morbi blandit ligula feugiat magna. Nunc eleifend consequat lorem. Sed lacinia
nulla vitae enim. Pellentesque tincidunt purus vel magna. Integer non enim.
Praesent euismod nunc eu purus. Donec bibendum quam in tellus. Nullam cur-
sus pulvinar lectus. Donec et mi. Nam vulputate metus eu enim. Vestibulum
pellentesque felis eu massa.

I found DetexGUI by Tom Ford.

Unfortunately its GUI version generate an error on my Windows 7 64b, and its command line version can not handle included documents.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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