59

I'm calling pdflatex from within my (C++) program using system(), needless to say all the garbage pdflatex puts on screen is a bit irritating in this case.

So...how do I encourage pdflatex to forego the lengthy outputs? It would be even better if only errors would be visible...

Kevin Reid
  • 37,492
  • 13
  • 80
  • 108
NomeN
  • 17,140
  • 7
  • 32
  • 33

2 Answers2

68

Unfortunately (La)TeX doesn't really abide by the rules of stdout and sterr, owing (I assume) to its origins in the early 80s. But there are some switches you can invoke to alter the amount of information being shown.

Execute latex with either the -interaction=nonstopmode or -interaction=batchmode switches for non-halting behaviour even in the case of a syntax error. nonstopmode will print all usual lines, it just won't stop. batchmode will suppress all but a handful of declarative lines ("this is pdfTeX v3.14...").

These can also be invoked from within the document with \batchmode and \nonstopmode, but this is less useful for the situation you're describing.

Will Robertson
  • 62,540
  • 32
  • 99
  • 117
  • What do you mean by "doesn't really abide by the rules of `stdout` and `stderr`?" Does it use other output streams? – Matthew Leingang Jan 26 '11 at 13:11
  • @Matthew it's been a while since I looked at this, but my memory is that trying to redirect only, say, the error messages somewhere doesn't work at all. But I could be mistaken; sorry to be vague. – Will Robertson Jan 27 '11 at 05:01
  • @Matthew Leingang: by accident I found [Re: silencing latex (quiet/batch mode)?](http://newsgroups.derkeiler.com/Archive/Comp/comp.text.tex/2007-09/msg00520.html); I believe that clarifies? Cheers! – sdaau May 18 '11 at 07:53
  • 1
    one problem with `-interaction=batchmode` is that now errors do not show on the screen any more as well. One has to go look in the log file to see them, which is not convenient having to do each time to find if the file compiled OK or not. – Nasser Apr 15 '22 at 16:42
24

To simply ignore all output, redirect pdflatex stdout to /dev/null:

system("pdflatex yourdocument >/dev/null");

You may want to add \nonstopmode at the beginning of your document to instruct tex to keep going even when encountering errors.

To get the error messages, pipe pdflatex output to your program and look for errors around rows starting with !, e.g.

FILE *outputf = popen("pdflatex yourdocument", "r");

// ... read and analyze output from outputf ...

pclose(outputf);
laalto
  • 150,114
  • 66
  • 286
  • 303
  • 2
    I'm not sure if pdflatex does, but many command-line programs write to both stdout (which will be captured by your ">/dev/null") and stderr (which won't). To silence *both*, add " 2>&1" after your existing system() string. – j_random_hacker Jun 24 '09 at 12:25
  • 2
    Also: On Windows, the necessary device is just called "NUL" (note 1 "L"), not "/dev/null". The "2>&1" part works as for *nix. – j_random_hacker Jun 24 '09 at 12:27
  • The problem with this is that if you have an error, pdflatex stops, waiting for input, but you would never understand it. Will Robertson's answer is better (although pdflatex still outputs a couple lines) – Shahbaz Feb 06 '12 at 14:35