3

As a potential follow on from Can LaTeX be persuaded to produce text output? there are a number of partial solutions to generating text based output (e.g., plain ascii/utf-8 text, html, xml). From what I understand TeX4ht uses a combination of LaTeX and some magic to create a dvi file from which the text can be scraped. LaTeX2RTF seems to bypass LaTeX completely. There are also Lua based solutions where the boxes are written to an output file.

Naively, it seems a potentially easier solution would be to modify the tex-engine to produce text files. Obviously this would lose things like images and formatting might be limited. Is this something that could be implemented at the engine level?

  • What's 'text'? Seriously, there is not a hard line between formatting and text: for example, superscripts can be 'just text' or Unicode chars or ... – Joseph Wright Nov 22 '17 at 15:57
  • @JosephWright i don't really care. How about whatever character the regular tex engine puts into the pdf/dvi file. If it makes $x^2$ with a resize and vertical space, just add 2, if it uses a unicode character, use that one. It would be nice to keep interword and interparagraph spaces and tabs, but really I would take whatever it outputs. – StrongBad Nov 22 '17 at 16:01
  • traditionally this was done with dvi2tty – David Carlisle Nov 22 '17 at 16:10
  • 1
    You've already linked to my answer to my question where I explain that I use LuaTeX to do precisely this. It completely bypasses the PDF and just produces text. The lua code can be found at github.com/loopspace/latex-to-internet/blob/master/… It's actually more complicated than it need be since my current use-case is to produce HTML5+MathML output and so it does tag-checking. That could be removed to simply output the pure text. – Loop Space Nov 22 '17 at 16:34
5

In pdftex or luatex the back end is built in to the program but in classic tex (or xetex) the tex program just produces a device independent representation from which pdf or text etc can be constructed, using separate driver programs without needing to extend the main tex executable. texlive comes with dvi2tty.

So for example the standard latex test document small2e, pdflatex small2e makes

enter image description here

and latex small2e; dvi2tty small2e produces

1    Simple Text


Words are separated by one or more spaces. Paragraphs are separated by one
or more blank lines. The output is not affected by adding extra spaces or extra
blank lines to the input file.
   Double quotes are typed like this: "quoted text". Single quotes are typed
like this: `single-quoted text'.
   Long dashes are typed as three dash characters---like this.
   Emphasized text is typed like this: this is emphasized. Bold text is typed
like this: this is bold.



1.1   A Warning or Two

If you get too much space after a mid-sentence period---abbreviations like etc.
are the common culprits)---then type a backslash followed by a space after the
period, as in this sentence.
   Remember, don't type the 10 special characters (such as dollar sign and
backslash) except as directed!  The following seven are printed by typing a
backslash in front of them: $ & # % _{ and }. The manual tells how to make
other symbols.

Note that this isn't just the raw text some attempt at indicating the layout is made, and line breaks match those in the pdf version.

  • I should say of course that pdftex and luatex can also produce dvi output. – David Carlisle Nov 22 '17 at 16:23
  • It seems like dvi2tty is just scraping the text (and some formatting) from the dvi file. My question is really about if we could skip the dvi step. – StrongBad Nov 22 '17 at 16:23
  • @StrongBad but that's rather the point of my answer, the architecture of using dvi output is precisely to avoid having to add new back ends for different output formats. You could make the driver seamless like xetex running xdvipdfmx where it just appears that pdf is produced by xetex, so latex -txt small2e makes the text directly but that is just cosmetics. The dvi output is just a dump of tex's internal box model (more or less) so you have to generate that internally anyway. – David Carlisle Nov 22 '17 at 16:27

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.