8

I'm used to use both pdflatex and xelatex engine to compile my documents, xelatex in case of .tex files containing polyglossia package and pdflatex for the others

My question is there a method ( with latexmk or arara or any other program ) to compile my documents in a conditional manner

if tex file contain "polyglossia" then 
  use xelatex 
else 
  use pdflatex
fi 

NB I have windows OS on my pc.

  • 2
    why not just put an arara or editor comment at the top to say to use xetex? – David Carlisle Jun 19 '17 at 14:18
  • 3
    it is in general very unreliable just to look for polyglossia it might be commented out or in \ifxetex \usepackage{polyglossia}\else \usepackage{babel}\fi etc so an explicit comment at the top is always going to be more reliable. – David Carlisle Jun 19 '17 at 14:26
  • 3
    In theory arara 4.0 has this cool new feature where you can check if a file contains a specific string, meaning you questions would be answered with % arara: xelatex if found('tex', 'polyglossia') % arara: pdflatex if !found('tex', 'polyglossia'), however in reality this does not work, as the comment to instruct arara makes sure that "polyglossia" will be within the file, so it will always use xelatex ... – user36296 Jun 19 '17 at 21:32
  • 1
    @samcarter And it would be subject to the problems David mentioned. Or polyglossia might be loaded via another package or .... – cfr Jun 20 '17 at 1:42
  • 1
    Why do all your documents have to have the same comment? Changing it is one word in your source. What, really, could be easier than that? – cfr Jun 20 '17 at 1:43
9
+100

I'm on windows too. My editor, Winedt, has macro capabilities and it would be easy to write a macro to start the compilation which does want you ask for. It is also not every difficult to devise other methods with other scripts languages with similar effects.

But imho you would learn very fast to hate such automation.

It is so very easy to imagine documents where your test doesn't do what you want. Starting with documents which can be compiled with both engines, documents which should use xetex but use babel instead of polyglossia, documents where the relevant packages are buried in a preamble.tex, document where behind \end{document} are comments and rests of other documents, documents where some of the packages are commented or inside \if-clauses, documents without fontspec or polyglossia but which should be compiled with xetex as they use pstricks, ...

It is also easy to imagine situations where you don't want an automation, e.g. if you want to compare the output of a xelatex compilation with a lualatex or pdflatex or latex + dvipdfmx compilation.

It would imho cost you much more time to handle all this situations than it would cost you to add a comment about the compilation at the top. Also you as a human are much better in deciding about the correct compilation method than a script.


EDIT: How to achieve this in WinEdt?

As simple as drinking a glass of water... (almost)

Create a file named XeORPdf.edt in the WinEdt Appdata directory %b\Exec\TeX with the following contents:

// xelatex or pdflatex?

PushTagsandRegisters;

  GetPreamble("\begin{document}",9);
  FindInString("%!9","<@{ }\\usepackage@{\[**\]}\{@^{\}|%%|polyglossia}polyglossia{,| |\}}",7,8,11);
  IfNum(%!7,%!8,"<",!"Exe('%b\Exec\TeX\XeLaTeX.edt');",!"Exe('%b\Exec\TeX\PDFLaTeX.edt');");

PopTagsandRegisters;

End;

Then, in your local MainMenu.ini (open it through the options interface), add the following lines

  ITEM="XeORPdf"
    CAPTION="XeLaTeX or PDFLaTeX"
    IMAGE="Question"
    SAVE_INPUT=1
    MACRO="Exe('%b\Exec\TeX\XeORPdf.edt');"
    REQ_FILTER=:"%!M=TeX"|"%!M=TeX:STY"|"%!M=TeX:AUX"
  ITEM="-"

somewhere, let's say after the lines

MENU="TeX_Menu"
  CAPTION="Te&X"
  CONFIG_FILTER="Default;MiKTeX;TeX Live"

to have the new command on top of the "TeX" menu:

enter image description here

If you want to have it in the toolbar, too, simply add the line

  BUTTON="XeORPdf"

somewhere in your local Toolbar.ini.

Load the scripts with the "load script" icon in the options interface, or my using Options->maintenance->rebuild all.

Now if your .tex file contains a non-commented line like

\usepackage[...]{...,polyglossia,...}

xelatex is executed, otherwise pdflatex is.

  • Thanks Ulrike Fischer for your remarks, but most time I'm facing this situation files with polyglossia which need to be compiled with xelatex and others files mostly with fontenc compiled with pdflatex. For lualatex and pstricks never use them – Salim Bou Jun 22 '17 at 8:56
  • "most time" is not enough. – Ulrike Fischer Jun 22 '17 at 9:07
  • delete the edit if you don't like it :-) – karlkoeller Jun 22 '17 at 11:39
  • @karlkoeller: The edit is fine. And while I don't think that it is really useful I'm quite willing to let people try out for themselves. – Ulrike Fischer Jun 22 '17 at 11:55
  • I followed all the steps but "XeLaTeX or PDFLaTeX" did not appear in TeX menu. – Salim Bou Jun 22 '17 at 17:59
3

The advantage of arara is that you can customise the build process on a per-document basis rather than relying on a tool that has to try to work out the build sequence. It seems like overkill to use a Java application to perform something that could easily be done in a script, but if you really want to use arara and the problem cases don't apply in your document, then here's one way to do achieve it.

Following on from @samcarter's comment, arara v4.0 allows conditions, but you need to be quite particular about the pattern¹. For example, rather that checking for any instance of polyglossia, check specifically for \usepackage{polyglossia} occurring at the start of a line:

% arara: xelatex if found("tex", "\\R\\\\usepackage\\{polyglossia\\}")
% arara: pdflatex unless found("tex", "\\R\\\\usepackage\\{polyglossia\\}")
\documentclass{article}

\usepackage{polyglossia}

\begin{document}
Test.
\end{document}

This runs xelatex, whereas

% arara: xelatex if found("tex", "\\R\\\\usepackage\\{polyglossia\\}")
% arara: pdflatex unless found("tex", "\\R\\\\usepackage\\{polyglossia\\}")
\documentclass{article}

%\usepackage{polyglossia}

\begin{document}
Test.
\end{document}

runs pdflatex and so will

% arara: xelatex if found("tex", "\\R\\\\usepackage\\{polyglossia\\}")
% arara: pdflatex unless found("tex", "\\R\\\\usepackage\\{polyglossia\\}")
\documentclass{article}

 \usepackage{polyglossia}

\begin{document}
Test.
\end{document}

(since there's a space character between the start of the line and \usepackage{polyglossia}).

There are some situations where this will go wrong, as already mentioned in Ulrike's answer. For example:

% arara: xelatex if found("tex", "\\R\\\\usepackage\\{polyglossia\\}")
% arara: pdflatex unless found("tex", "\\R\\\\usepackage\\{polyglossia\\}")
\documentclass{article}

\begin{document}
\begin{verbatim}
\usepackage{polyglossia}
\end{verbatim}
\end{document}

or

% arara: xelatex if found("tex", "\\R\\\\usepackage\\{polyglossia\\}")
% arara: pdflatex unless found("tex", "\\R\\\\usepackage\\{polyglossia\\}")
\documentclass{article}

\newif\ifusepoly
\usepolyfalse

\ifusepoly
\usepackage{polyglossia}
\else
\usepackage{babel}
\fi

\begin{document}
Test.
\end{document}

¹See the Pattern class documentation for a list of available regular expression constructs. It's a bit awkward as backslashes need escaping when supplying the expression as a string, so \R indicates a line break but this needs to be written as \\R to avoid being interpreted as a control character.

2

In case polyglossia may be hidden in some other packages or subfiles or another package needs xelatex, maybe you could use an explicit error message to detect if xelatex is needed.

The following example requires arara 4.0 and an additional run if there is not yet a .log file.

% !TeX program = txs:///arara
% arara: pdflatex: {interaction: nonstopmode} if !exists('log') 
% arara: pdflatex: {interaction: nonstopmode} if exists('log') && !found('log', 'cannot-use-pdftex') 
% arara: xelatex: {interaction: nonstopmode} if exists('log') && found('log', 'cannot-use-pdftex') 

\documentclass{article}
\usepackage{polyglossia}

\begin{document}
test
\end{document}

But make sure that your images or sections are not coincidentally called cannot-use-pdftex :)


This technique can also be used for documents, which do not need to be compiled with xelatex, but for which this is the "intended" compilation chain. For example the metropolis beamer theme has a fallback for the compilation with pdflatex, but will tell you:

You need to compile with XeLaTeX or LuaLaTeX to use the Fira fonts

  • Interesting solution...how can I test it with version 4 of arara ? Wait until version is released on tl? – Salim Bou Jun 22 '17 at 13:11
  • @SalimBou The source code and .jar files are available from github.com/cereda/arara – user36296 Jun 22 '17 at 13:15
0

Example using ifxetex and ifluatex that should work with any compiler:

\documentclass{article}
\usepackage{ifxetex,ifluatex}
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
  \usepackage[T1]{fontenc}
  \usepackage[utf8]{inputenc}
  \usepackage[spanish]{babel}
  \def\xxx{using \texttt{pdflatex}}
\else % if luatex or xelatex
  \ifxetex
    \usepackage{mathspec}
    \usepackage{polyglossia}
  \def\xxx{using \texttt{xetex}}
  \else
    \usepackage{fontspec}
    \usepackage{polyglossia}
    \def\xxx{using \texttt{luatex}}
  \fi
  \defaultfontfeatures{Ligatures=TeX,Scale=MatchLowercase}
\fi

\begin{document}
Test \xxx
\end{document}

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.