3

You all know the feeling when needing to compile twice before the desired output has been achieved.

I am using TeXShop and am a bit tired of clicking on the Typeset-button, waiting for everything to compile, and then clicking on the Typeset-button again, to wait for a second compilation ...

Any solution so as to make TeXShop compile two or more times automatically please?

O0123
  • 1,773
  • Define a new command that calls pdflatex twice. Or use a tool like arara or latexmk – Johannes_B Oct 04 '17 at 03:17
  • why would you ever want to do this? – David Carlisle Oct 04 '17 at 11:52
  • @DavidCarlisle To save time. – O0123 Oct 04 '17 at 12:03
  • making everything take twice as long does not save time. – David Carlisle Oct 04 '17 at 12:03
  • the normal cycle is that you write some words, run latex, some ref may be ? but it doesn't matter. write some more words run latex again the refs resolve to [3] or whatever. write some more words, run latex.... running latex twice at each step just makes the typesetting stage take twice as long and has no advantages – David Carlisle Oct 04 '17 at 12:06
  • @DavidCarlisle It actually can have an advantage. I am working with a code that, whenever I alter a bit of text, almost the whole page will become unreadable after one time compiling, but will be perfectly readable after two times compiling. This is because certain (and very many) tooltips are involved, which are not placed where they should be placed (after one compilation). – O0123 Oct 04 '17 at 12:10
  • perhaps I should have said almost never any use. It seems surprising that it's necessary (for example your tex code could probably spot that the position information is not available and not output them at all rather than output them in the wrong place) but anyway my comment was really to your opening "You all know the feeling " as no I think most people do not know that feeling (or should not know that) If you have a very special document with custom requirements you may need this in those special circumstances, but this is far from being a general issue. – David Carlisle Oct 04 '17 at 12:15
  • @DavidCarlisle I see. Coincidentally, I just happened to have asked a new question in which you can see what sort of visual problems can occur if you compile only once (although not the main topic of that question though). One needs to modify the code a bit: it becomes apparent if one copies the line starting with \Chinese{疊。。【疊 about twenty or more times. – O0123 Oct 04 '17 at 12:54

2 Answers2

4

The best tool for this particular case (which wasn't obvious from your initial question) would to use the TeXShop arara engine and use arara rules to compile the file.

See:

In this case you can simply put the following lines at the top of your document:

% !TEX TS-program = arara
% arara: pdflatex
% arara: pdflatex { synctex: true }

The first line tells TeXShop to use the arara engine, and the next two lines tell arara to process the file twice. Since results of the first compilation will never be viewed pdflatex can be run with no options; on the second run, the synctex option is added so that you have synchronization between source and preview. Obviously you can adjust the tool to run lualatex or xelatex, etc. if that's the engine you are using.

For other use cases (particularly resolving citations and references), the latexmk approach is very helpful.

TeXShop comes with a pdflatexmk engine which will compile your document (and also run bibtex or biber and other tools) as many times as is required, but not more.

This is better than blindly requiring the document to be compiled twice. The easiest way to use it is to add:

% !TEX TS-program = pdflatexmk

to the first line of your document. There are other versions for different engines (xelatexmk, lualatexmk, etc.)

If the engine doesn't show up in the pulldown Typeset menu, then you need to move it from the Inactive folder to the Engines folder within ~/Library/TeXShop.

See:

Of course the two approaches can be combined, so if you are using arara you could have the first compilation be plain pdflatex and the second compilation use latexmk and have the best of both worlds:

% !TEX TS-program = arara
% arara: pdflatex
% arara: pdflatexmk { synctex: true }
Alan Munn
  • 218,180
  • So, how many times will this "compile"? – O0123 Oct 04 '17 at 03:59
  • @VincentMiaEdieVerheyen latexmk will compile as many times as needed to fully resolve references and bibliography citations. It's not a fixed number of times, but as many as is required. For arara you tell it within each document the actual compilations you want by adding one arara directive line per compilation. – Alan Munn Oct 04 '17 at 04:18
  • I have inserted % !TEX TS-program = lualatexmk at the top of my document, yet I still have to manually compile twice to get the desired result. Nothing related to references or bibliographies though. – O0123 Oct 04 '17 at 04:26
  • Also, to make the answer complete, you have mentioned that the arara engine could also be used, so then could you also specify which arara-rules should then be used please? – O0123 Oct 04 '17 at 07:28
  • @VincentMiaEdieVerheyen It wasn't clear from your question why you wanted the two compilations. For your particular use case, the arara solution is probably better. I've updated the answer. – Alan Munn Oct 04 '17 at 13:40
2

Create a file similar to the following named MyPdfLatex.engine in the directory ~/Library/TeXShop/Engines/ and make it executable via chmod +x MyPdfLatex.engine:

#!/bin/sh
bfname="$1"
pdflatex  --file-line-error --shell-escape  -recorder --synctex=1 "\input{$bfname}"
pdflatex  --file-line-error --shell-escape  -recorder --synctex=1 "\input{$bfname}"

You should customize the pdflatex command line above for your particular case. Then, upon next restart of TeXShop, select MyPdfLatex instead of LaTeX. Then, when you Typeset and you sill see it compile twice.

If you want the pulldown engine to default to MyPdfLatex, go to Preferences/Typesetting panel and set the Default Command to MyPdfLatex.

Peter Grill
  • 223,288
  • Have you confirmed that this works when simply changing the two instances of pdflatex into lualatex as well please? I can't get it to work just yet. – O0123 Oct 04 '17 at 07:17
  • I have figured it out now. Just copied everything from LuaLaTeX.engine and copied the last line twice. – O0123 Oct 04 '17 at 07:36