13

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

\ifhostname{mymachine}
  \dostuff
\else
  \dootherstuff
\fi

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

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...

\documentclass{article}

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

\begin{document}
\begin{align*}
    y &= x
\end{align*}
\end{document}

Hope this helps...

6

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

\startluacode
   if os.getenv('HOSTNAME') == "...." then
      ...
   else
      ...
   end
\stopluacode

or in LaTeX you can use

\usepackage{luacode}
....
\begin{luacode}
   if os.getenv('HOSTNAME') == "...." then
      ...
   else
      ...
   end
\end{luacode}
3

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:

\documentclass{article}
\usepackage{catchfile}

\CatchFileDef{\hname}{/etc/hostname}{}
\def\myhname{myhostname }

\begin{document}
\hname \myhname

\ifx\hname\myhname Blah \else Bloh\fi
\end{document}

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

2

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
\InputIfFileExists{hostnumber}
\ifcase\hostnumber
  % first host
\or
  % second host
\or
  % third host
\or
  % ...
\else
  % host number out of range (<0 or >max)
  % Insert fall-back code here
\fi

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.