I think the pseudo code in the title says it all: How can TeX (or LaTeX) find out the $USER who is running pdflatex?
I'm collaborating want the output to look very slightly different when I run latex as opposed to when my collaborator does.
I think the pseudo code in the title says it all: How can TeX (or LaTeX) find out the $USER who is running pdflatex?
I'm collaborating want the output to look very slightly different when I run latex as opposed to when my collaborator does.
My humble attempt with LuaLaTeX:
\documentclass{article}
\usepackage{luacode}
\usepackage[T1]{fontenc}
\begin{luacode}
-- get the username variable,
-- or a default string in case
-- the variable is not found
function getUsername()
return os.getenv("USERNAME") or "Oh no!"
end
-- let's do a simple comparison
-- and print the test result
function checkUsername(username)
if username == getUsername() then
tex.print("Authenticated.")
else
tex.print("I'm calling the \\TeX\\ police.")
end
end
\end{luacode}
\newcommand\authenticate[1]{\luadirect{checkUsername(\luastring{#1})}}
\begin{document}
Trying with \verb|paulo|: \authenticate{paulo}
Trying with \verb|david|: \authenticate{david}
\end{document}
The output:

A method that should work on all operating system (but is basically the same as David's) and needs also -shell-escape:
\documentclass{article}
\usepackage{catchfile}
% #1 = control sequence to define
% #2 = variable to get the value of
\newcommand{\getvar}[2]{%
\CatchFileEdef#1{"|kpsewhich -var-value #2"}{\endlinechar=-1 }%
}
\def\me{enrico}
\getvar{\usrtest}{USER}
\begin{document}
\ifx\me\usrtest ME \else IMPOSTER\fi
\end{document}
With kpsewhich we can avoid quotes and dollars or %; the program should work the same on TeX Live and on MiKTeX. Of course the name of the variable to set might be different on the various operating systems: it's USER on Unix ones and USERNAME on Windows.
This needs
pdflatex --shell-escape

\documentclass{article}
\makeatletter
{\everyeof{\noexpand}
\xdef\usrtest{\@@input"|echo \string$USER""\expandafter}}
\makeatother
\def\me{davidc}
\begin{document}
\ifx\me\usrtest ME \else IMPOSTER\fi
\end{document}
echo %USERNAME% on Windows (Of course, the question does say `$USER).
– Joseph Wright
Apr 10 '13 at 08:05
\input is not supported by the 'release' version of XeTeX, although that will I think change.
– Joseph Wright
Apr 10 '13 at 08:13
As I've said, I don't think you can access the username directly (Edit: OK, so you can, see above), but the optional package should work nicely:
\documentclass{minimal}
\usepackage[bob]{optional} %\usepackage[joe]{optional}
\begin{document}
This document was compiled by \opt{joe}{Joe Schmoe}\opt{bob}{Bob Wilbur}!
\end{document}
\usepackage{local}and you can have non-sharedlocal.styin each of your~/texmf/tex/latex/misc/local.styand those files can do whatever you like in each case. No need for shell-escape or windows/unix difference worries. – David Carlisle Apr 10 '13 at 12:26