This is a follow-up to Can LaTeX be persuaded to produce text output?. Having discovered that pdftotext is the way to go, I'm curious as to whether it is possible to have TeX invoke that command on the PDF after the PDF has been finished (presumably via some write18 hackery). So this is close to Is it possible to compile a *TeX document with a single command? except that I simply want to run the equivalent of

pdflatex doc.tex; pdftotext doc.pdf

but wholly from within TeX. So it's sort of what I understand xelatex does with its producing the dvi first and then converting it to pdf.

This is just idle curiosity, I fully realise that the shell command I gave above is almost certainly the easiest way of doing this, and that in explaining to others what to do (as I expect to have to do with this project) then the above will be simpler than "Run pdflatex with the -enable-shell-escape option.".

To make this different from Taco's answer to Is it possible to compile a *TeX document with a single command?, I'd like to be able to hide this from the user in a style file.

  • I doubt that it is possible, but maybe if you can detach the process called by write18 from pdftex, and make it sleep just a little bit until pdftex is done, that may work. – Bruno Le Floch Jun 21 '11 at 22:34
  • I would attach the Latex source to the PDF and process that. – Emre Jun 21 '11 at 22:39
  • @Bruno: i.e., set a flag to ignore the initial code, then write18 to compile the document (while ignoring the initial code), then write18 the pdftotext command. Sounds good. Do you even need to sleep? I thought write18 halted the compilation until it returned. – Will Robertson Jun 22 '11 at 9:07
  • @Will: what I hoped was to just run pdftex once, and from within it trigger a command to be run after the end of this single run. Hence hoping to detach the command so that pdftex finishes nicely and produces the pdf. Not sure why you want to ignore initial code if you have two independent sessions of pdftex. – Bruno Le Floch Jun 22 '11 at 12:48
  • 1
    @Bruno, @Will: I had a go at implementing what you seem to be suggesting and got a working system. See my answer below. – Loop Space Jun 22 '11 at 13:23

You can use :

pdflatex -halt-on-error doc.tex && pdftotext doc.pdf

If pdflatex succeeds, then pdftotext is invoked, note that I replaced ; by &&.

In the other hand you can use the command make, you create a file called Makefile with the following and you are done(Use tabs, no spaces):

doc.txt: doc.pdf
    pdftotext doc.pdf
doc.pdf: doc.tex
    pdflatex doc.tex

From now and on, you should run just make

  • 1
    I concur. Don't try to make LaTeX do what other tools such as make do so much better. Use LaTeX to build your document, but not to manage the whole build process. – David Hammen Jun 22 '11 at 2:11
  • @David: the advatage of using only TeX is that you need fewer tools to install. – topskip Jun 22 '11 at 3:23
  • 2
    @Patrick — Right; especially for users who just click "compile" (esp. on Windows) you can't assume the presence or possibility of other toolchains. LuaTeX makes (or will make) things much easier in this regard. – Will Robertson Jun 22 '11 at 9:05
  • Thanks for the answer, but this question was a "Is this even possible?" and not a "Should I do it?". – Loop Space Jun 23 '11 at 13:19
  • And welcome to TeX.SX Igor ;-) – ℝaphink Oct 5 '11 at 13:51

Following up on the comments between Bruno and Will, and mixing in a little of Passing parameters to a document, I came up with the following:


\expandafter\ifx\csname itexflag\endcsname\relax
\immediate\write18{pdflatex "\noexpand\def\noexpand\itexflag{} \noexpand\input{\jobname.tex}"; pdftotext \jobname.pdf}

This is some text.

It has to be invoked with pdflatex -shell-escape. I suppose that one should put && to test for success of the pdflatex run rather than ;. Unlike what Will says, I don't throw away the rest of the file but proceed with processing it. Mainly that's because I wasn't sure how to do throw it away! But also because I worried that the first pdflatex run (which should produce no output) might clobber the PDF produced by the second pdflatex (which should produce output, but which ends before the first one does). Although my experiments suggest that this is not the case, nonetheless I see no danger in allowing the compilation to proceed. (Of course, if I really am only interested in the text output then I don't care if there's a PDF there or not, but there are benefits to having the PDF as well as the text: for example, it's easier to read the text from the PDF than via less on the terminal.)

  • This is what I was thinking of pretty much albeit somewhat simpler :-) -- as you say, a couple different ways of programming this logic. – Will Robertson Jun 22 '11 at 13:35
  • @Will: Do you mean that this was simpler than you thought, or that you have a simpler method than this? If the latter, please post it! – Loop Space Jun 22 '11 at 19:10
  • I suspect the former, since your solution is very neat (and it produces the correct pdf in both runs, I guess). Using something like this could take care of running pdftex enough times to get cross references right, like latexmk... – Bruno Le Floch Jun 22 '11 at 21:38
  • Aye, yours is simpler than the one in my head :-) – Will Robertson Jun 23 '11 at 0:42
  • @Will, @Bruno: I figured out a way to make it terminate the current run (\makeatletter\expandafter\@@end\fi) but then realised that although that doesn't clobber the PDF, it does clobber the log. I suppose one could change the jobname and do some hackery that way, but all in all it's probably best to let it take its course. – Loop Space Jun 23 '11 at 13:18

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.