11

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 don't think you can access envvars from LaTeX. If a regular conditional, or variable set in your .tex won't do, perhaps a custom Makefile? There was a relatex question here: http://stackoverflow.com/questions/1208161/to-get-the-current-user-in-latex – Ingmar Apr 10 '13 at 07:53
  • 2
    You might want to start by looking at http://tex.stackexchange.com/questions/1728/accessing-environment-variables-within-a-document, which covers accessing system environment variables – Joseph Wright Apr 10 '13 at 07:54
  • 1
  • @ Joseph: Indeed, I saw that a bit later, but wondered if it really has to be "so complicated", i.e., need such tricky "contortions" ;-) and so kept my question, hoping for a "nicer" answer. – Martin Mächler Apr 10 '13 at 10:19
  • 1
    While the answers (including mine) have looked at interesting ways of getting the environment variables, for the use case described I would not do that. The shared document can just use \usepackage{local} and you can have non-shared local.sty in each of your ~/texmf/tex/latex/misc/local.sty and 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

4 Answers4

10

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:

Call teh cops

Paulo Cereda
  • 44,220
7

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.

egreg
  • 1,121,712
  • Thank you, Enrico, (and David !). Do I understand correctly that your solution would not need '--shell-escape'? {{and an aside: I'd wish something like this \getvar{} should be part of a standard LaTeX package...}} – Martin Mächler Apr 10 '13 at 10:14
  • 1
    @MartinMächler No, this needs --shell-escape as well – egreg Apr 10 '13 at 11:47
6

This needs

pdflatex --shell-escape

enter image description here

\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}
David Carlisle
  • 757,742
  • 2
    And Unix! You'd need echo %USERNAME% on Windows (Of course, the question does say `$USER). – Joseph Wright Apr 10 '13 at 08:05
  • Might also be worth noting that the pipe syntax for \input is not supported by the 'release' version of XeTeX, although that will I think change. – Joseph Wright Apr 10 '13 at 08:13
  • @JosephWright The support of "pipe" inputs in XeTeX has been announced for the next version (currently under testing) by Khaled Hosny. – egreg Apr 10 '13 at 08:14
  • @egreg Yes, I know, hence the comment. Even once it's released it may be some time before people update their XeTeX though (the shaper change may put people off until a lot of testing is done). – Joseph Wright Apr 10 '13 at 08:15
0

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}
Ingmar
  • 6,690
  • 5
  • 26
  • 47