是否有一种简单的方法可以使用包或简单的 LaTeX 脚本在 XeLaTeX 中生成像随机 uuid 这样的唯一标识符,而不需要执行任何外部命令?
答案1
由于 XeTeX 目前没有原生的随机数支持,我们需要random.tex
;也binhex.tex
有帮助。
\documentclass{article}
\input{random}
\input{binhex}
\makeatletter
\newcount\temp@uuid
\newcommand{\makeUUID}[1]{%
\@ifdefinable{#1}{\make@UUID{#1}}%
}
\newcommand{\make@UUID}[1]{%
\setrannum{\temp@uuid}{0}{"10000}%
\edef#1{\nhex{4}{\temp@uuid}}%
\setrannum{\temp@uuid}{0}{"10000}%
\edef#1{#1\nhex{4}{\temp@uuid}-}%
\setrannum{\temp@uuid}{0}{"10000}%
\edef#1{#1\nhex{4}{\temp@uuid}-}%
\setrannum{\temp@uuid}{0}{"10000}%
\edef#1{#1\nhex{4}{\temp@uuid}-}%
\setrannum{\temp@uuid}{0}{"10000}%
\edef#1{#1\nhex{4}{\temp@uuid}-}%
\setrannum{\temp@uuid}{0}{"10000}%
\edef#1{#1\nhex{4}{\temp@uuid}}%
\setrannum{\temp@uuid}{0}{"10000}%
\edef#1{#1\nhex{4}{\temp@uuid}}%
\setrannum{\temp@uuid}{0}{"10000}%
\edef#1{#1\nhex{4}{\temp@uuid}}%
% remove the following three lines if you're happy with uppercase hex digits
\lowercase\expandafter{%
\expandafter\def\expandafter#1\expandafter{#1}%
}%
}
\makeatother
\makeUUID\foo
\makeUUID\baz
\begin{document}
\ttfamily
\foo
\baz
\end{document}