# Is there a way to detect from inside a package that MiKTeX is used?

Is there a way to detect that the MiKTeX TeX LaTeX executable is used? Something like \ifmiktex or even \IfFileExists{<some file only present in MiKTeX>}{}{}?

The reason is that in one of my packages I have to re-execute LaTeX internally on an different .tex file, which of course requires the shell escape aka. write18 feature to be enabled by the user. However, I want to support this feature recursively, i.e. the other .tex file can itself call an own LaTeX sub-process. For this to work I have to add the correct command line option to the internally call of the (pdf)latex executable. Now this option is -shell-escape for TeXLive, but --enable-write18 for MiKTeX.

Here some example code how I plan to implement this feature. The missing part is the definition of \ifmiktex.

\ifpdf
\def\latexcmd{pdflatex }%
\else
\def\latexcmd{latex }%
\fi

\ifmiktex
\immediate\write18{\latexcmd --enable-write18 \options \file}%
\else
\immediate\write18{\latexcmd -shell-escape \options \file}%
\fi

-
MiKTeX should also understand -shell-escape if I am right at 2.8. Did you test it? –  Herbert Jan 27 '11 at 12:41
@Herbert: I still have an old MikTeX 2.4 and it also understands -shell-escape. –  Philippe Goutet Jan 27 '11 at 13:04
@Herbert, honestly no, not yet. I first need to install Windows again on one of my computers ... I had a look at the official list of MikTeX command line arguments and there the support of -shell-escape wasn't mentioned. –  Martin Scharrer Jan 27 '11 at 13:40
would passing both options to pdflatex do any harm? –  Martin H Jan 27 '11 at 17:13
@Martin H: Good idea. A quick test with TeXLive under Ubuntu Linux shows that it works. Also, TeXLive seems to accept --enable-write18 as well! –  Martin Scharrer Jan 27 '11 at 17:26

MiKTeX supports both optional arguments --enable-write18 and also --shell-escape

a possible Test for MiKTeX

\documentclass{article}

\newif\ifMiKTeX
\makeatletter
\def\testMiKTeX{\expandafter\testMiKTeX@i\pdftexbanner\@nil}
\def\testMiKTeX@i#1(#2)#3\@nil{\testMiKTeX@ii#2..\@nil}
\def\testMiKTeX@ii#1.#2.#3\@nil{\ifx\relax#2\relax\else\MiKTeXtrue\fi}
\makeatother
\testMiKTeX

\begin{document}

\ifMiKTeX we use MiK\TeX \else we use \TeX Live\fi

\end{document}

-
The resulting text is "we use MiKTeX". Note that there is a \@nil missing at the end of the parameter text of \testMiKTeX@ii. –  Martin Scharrer Feb 2 '11 at 20:18
thanks, than I can sue it ... –  Herbert Feb 2 '11 at 20:22
Thanks for pointing out this way. I'm going to build on it and might publish the results later. –  Martin Scharrer Feb 3 '11 at 11:33
While -shell-escape works fine with both TeX Live (under Linux and Windows) and MiKTeX, the required quotes used in the \write18 call may be different (e.g. think \write18{pdflatex -jobname '<name>' '<file>'} I used single quotes which do not work with MiKTeX, but with TeX Live under Windows. However, if I use double quotes per default then file names with spaces are no longer supported. If I assume only filenames and other input without spaces than this isn't a problem, but I like to support as much as possible. –  Martin Scharrer May 5 '12 at 18:21

Here a general proof-of-concept solution which provides \ifmiktex and \iftexlive switches. It is based on Herberts suggestion to use the output of \pdftexbanner. Once finished, it could be published as, say ifdist package or similar.

Please note that the \pdftexbanner expands to text with catcode "other", therefore \@onlevel@sanitze is used to also give the macro parameter text the same catcode.

### Update 2012/05/05:

Reworked and tested the code for Tex Live Linux, TeX Live Windows and MiKTeX. Provided warning for XeLaTeX which is not supported. I put it in the form of a package. Will Robertson might integrate it into ifplatform.

\ProvidesPackage{ifdistro}[2012/05/05 v1.0 Provides if-switches for MiKTeX and TeX Live]

\newif\ifmiktex
\newif\iftexlive

\begingroup
\makeatletter
\def\MiKTeX{MiKTeX}
\@onelevel@sanitize\MiKTeX
\def\TeXLive{TeX Live}
\@onelevel@sanitize\TeXLive
\def\WebIIC{Web2C}
\@onelevel@sanitize\WebIIC

% Test if \pdftexbanner exists, which is the case for latex, pdflatex and lualatex
% but not xelatex.
\expandafter\ifx\csname pdftexbanner\endcsname\relax
\PackageWarning{ifdistro}
Switches \expandafter\string\csname ifmiktex\endcsname\space
and \expandafter\string\csname iftexlive\endcsname\space
will not be set\@gobble}% gobbles the "on line X" part
\else

% Scans for 'MiKTeX' string with catcode 12 (other)
\expandafter\def\expandafter\testmiktex\expandafter#\expandafter1\MiKTeX#2\relax{%
\ifx\empty#2\empty
\global\miktexfalse
\else
\global\miktextrue
\fi
}
\expandafter\expandafter
\expandafter\testmiktex\expandafter\pdftexbanner\MiKTeX\relax\relax
\ifmiktex\else
% Scans for 'TeX Live' string with catcode 12 (other)
\expandafter\def\expandafter\testtexlive\expandafter#\expandafter1\TeXLive#2\relax{%
\ifx\empty#2\empty
\global\texlivefalse
\else
\global\texlivetrue
\fi
}
\expandafter\expandafter
\expandafter\testtexlive\expandafter\pdftexbanner\TeXLive\relax\relax
\iftexlive\else
% Scans for 'Web2C' string with catcode 12 (other)
% This string is used by TeX Live under MS Windows
\expandafter\def\expandafter\testtexlive\expandafter#\expandafter1\WebIIC#2\relax{%
\ifx\empty#2\empty
\global\texlivefalse
\else
\global\texlivetrue
\fi
}
\expandafter\expandafter
\expandafter\testtexlive\expandafter\pdftexbanner\TeXLive\relax\relax
\fi\fi

\fi

\endgroup

-
Looks good! Would it make sense to incorporate this inside ifplatform.sty? I'd be happy to add you as an author to the package if you like. –  Will Robertson Feb 2 '11 at 3:47
It won't work for XeTeX though. –  AlexG Feb 2 '11 at 8:01
@Will: I also think ifplatform would be a good place. However, I like to extract the version number as well. Give me some time to refine it first. –  Martin Scharrer Feb 2 '11 at 13:52
@WillRobertson: I found myself in need again for this. I didn't do any work on it since last time, but might rework it now. For XeTeX it would be possible to scan the first line of the log file. –  Martin Scharrer May 5 '12 at 15:57
For some reason TeX Live does use Web2C 2011 not TeX Live 2011 under Windows. –  Martin Scharrer May 5 '12 at 15:57

One good approach is to test whether a particular application exist or not. For example, if Yap (dvi viewer of MiKTeX) exists, you know that you are using MiKTeX. This approach has a good advantage that works with any other engine but the disadvantage is that write18 should be enabled.

-
you can have MiKTeX and TeXLive installed on the same machine and you can use Yap for TeXlive and ... –  Herbert Feb 2 '11 at 10:58
I just gave an example. Of course, you can make it much smarter by considering various possibilities! –  IRAN Feb 2 '11 at 11:17