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.

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.)

share|improve this question
1  
It seems there are some questions where you didn't accept any of the given answers. Please go back and look if your problem is solved. Your future questions are much more likely to receive attention if your accept rate is high. –  canaaerus Sep 13 '12 at 16:37
    
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
show 5 more comments

2 Answers 2

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.

\documentclass{article}
\usepackage{lipsum}
\usepackage{filecontents}
\usepackage{ifluatex}

\begin{filecontents*}{luaFunctions.lua}
-- clear the file
file = io.open("output.txt", "w")
file:write()

exportParagraph = false

function exportText (head)

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

    -- 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
                out:write(string.char(item.char))
            -- check if the element is a 'space'
            elseif item.id == node.id("glue") then
                wordCounter = wordCounter + 1
                out:write(" ")
            end
        end
        -- a newline in the file after each (tex)line
        out:write("\n")        
    end

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

    -- a newline in the file after each paragraph 
    out:write("\n")  

    assert(out:close())  
    exportParagraph = false  

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


function disableLigatures(head)
    -- disable ligatures
end

function SetExportParagraph(export)
    exportParagraph = export
end

callback.register("ligaturing", disableLigatures)
callback.register("post_linebreak_filter", exportText)
\end{filecontents*}

\ifluatex
    \directlua{dofile("luaFunctions.lua")}
\fi

\def\exportParagraph{%
    \ifluatex
        \directlua{SetExportParagraph(true)}
    \fi
}

\begin{document}
\exportParagraph 
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
rutrum.

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.

\exportParagraph
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.
\end{document}
share|improve this answer
add comment

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

share|improve this answer
add comment

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.