How do I construct a conditional statement based on the hostname of the compiling computer in LaTeX? For example:


Why: I am working with a collaborator on a document that has dependencies on a couple files that are located in different places. I would like the document to automagically detect who is compiling it and use the appropriate file locations.


4 Answers 4


You could possibly detect it via \immediate\write18 but those might require OS specific code An easier method to accomplish this would be to test for the existence of a file via \IfFileExists and test say ~/<user>/.login.

The test example below will result in the equation being centered (assuming you save this as a file with .tex extension). However, if you change the test for a file that does not exist the equations will be left aligned...


\IfFileExists{\jobname.tex}{% Adjust file name here to ~/user/.login or some other known file

    y &= x

Hope this helps...


In luatex, you can use os.getenv('HOSTNAME') to get the host name at least on *nix systems. I don't know the environmental variable is also set on Windows or not. Thus, in ConTeXt you can use

   if os.getenv('HOSTNAME') == "...." then

or in LaTeX you can use

   if os.getenv('HOSTNAME') == "...." then

At least on Linux, hostname should be in /etc/hostname, so you can try to input that file, using the Heiko Oberdiek's catchfile package:


\def\myhname{myhostname }

\hname \myhname

\ifx\hname\myhname Blah \else Bloh\fi

should print your hostname and uses \ifx to compare it to a given value.


I would make it simple and place a short file into your local TEXMF tree or in the document folder which contains a different if-switch command on every host. This file can then be easily read using \input or \InputIfFileExists. Alternatively let the input file contain all required preamble code itself.

It takes a little effort but in this case it seems to be not an issue. You need to now the specific needs for each host anyway.

Fore example the host specific file can hold a numeric macro, e.g.:

% hostnumber.tex
\def\hostnumber{0}% for the first host, then 1, 2, 3

And then use:

\def\hostnumber{-1}% fall-back default
  % first host
  % second host
  % third host
  % ...
  % host number out of range (<0 or >max)
  % Insert fall-back code here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .