13

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
doncherry
  • 54,637
Martin Scharrer
  • 262,582
  • 6
    MiKTeX should also understand -shell-escape if I am right at 2.8. Did you test it? –  Jan 27 '11 at 12:41
  • 1
    @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
  • 2
    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
  • @Herbert, although your comment is not the “real answer” to the question, it solves Martin's problem. Could you then post it as an answer? – Juan A. Navarro Feb 01 '11 at 10:36
  • @Juan, @Herbert: I'm going to test -shell-escape it with a new version of MikTeX as soon I find time. I also have now an idea how we might be able to define the \ifmiktex directive. – Martin Scharrer Feb 01 '11 at 10:49
  • @Juan: I did it –  Feb 01 '11 at 11:03
  • @Martin: you should drop Christian Schenk a line if he could define a \def\pdfmiktex{1} in the MikTeX specific pdftex/luatex/context. –  Feb 01 '11 at 19:13
  • @Herbert: Yes, or a trivial ifmiktex package would be possible which is just \let\ifmiktex\iffalse by default but \let\ifmiktex\iftrue in the MikTeX installation. – Martin Scharrer Feb 01 '11 at 19:17
  • @Martin, yes also a good idea. I also thought of something like pdflatex "\ifmiktex...\else...\fi \input{...}" –  Feb 01 '11 at 19:26
  • @Martin: can you try what the output of a pdflatex run with a MikTeX is, when you use \pdftexbanner in the documents body part? I have no MiKTeX running –  Feb 01 '11 at 22:23
  • @Herbert: The output is "This is MiKTeX-pdfTeX 2.9.3962 (1.40.11)" with a freshly installed MiKTeX. – Martin Scharrer Feb 01 '11 at 23:34
  • @Martin: then you can use this to test if you are in MiKTeX or TeXLive –  Feb 02 '11 at 08:29
  • @Martin: can you please test my code which I added to the answer –  Feb 02 '11 at 10:11

3 Answers3

13

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 02 '11 at 20:18
  • Thanks for pointing out this way. I'm going to build on it and might publish the results later. – Martin Scharrer Feb 03 '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 05 '12 at 18:21
7

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}
        {Primitive \string\pdftexbanner\space not found!\MessageBreak
         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
Martin Scharrer
  • 262,582
  • 2
    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 02 '11 at 03:47
  • 1
    It won't work for XeTeX though. – AlexG Feb 02 '11 at 08: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 02 '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 05 '12 at 15:57
  • For some reason TeX Live does use Web2C 2011 not TeX Live 2011 under Windows. – Martin Scharrer May 05 '12 at 15:57
  • MikTeX comes in two version, 32bit and 64bit. The latter does not have biber executable. So it would be nice also to have a check for x64 version of MikTeX. – Igor Kotelnikov Feb 17 '13 at 13:35
0

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.

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