有沒有簡單的方法可以使用套件或簡單的 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}