# Setting up environment to automatically and randomly label equations with symbols

So, using the equation environment and \label{ ... }, it is easy to get equations which are automatically numbered in a document. If we want to customize the symbol which labels the document, we can use \tag{ ... }.

However, is there a nice way to set the environment up so that, within a specified document, equations are automatically labeled with random symbols? Here, the set of potential symbols would be specified beforehand somewhere in the document, and different equations would be assigned different symbols.

• Welcome! Please provide a minimal working example i.e. code for a small document demonstrating the issue and showing what you've tried to solve it. Why do they need to be random especially? If they are really random, they will likely not be unique and LaTeX will complain. – cfr Nov 17 '16 at 3:08

The pgf package provides the commands \pgfmathdeclarerandomlist and \pgfmathrandomitem for working with random lists. As you presumably want to use the equation labels at most once, the best option is to delete the list items whenever they are used, which is hard. Fortunately, Mark Wibrow gives a way of doing this in his answer to the question How do I generate in LaTeX a list of random questions avoiding repetitions?.

Armed with Mark's code, it is quite easy to do what you want using \tag to add a random label from a user specified list of labels:

Here is the code:

\documentclass{article}
\usepackage{pgf}
\usepackage{amsmath}

\newcommand\EquationLabels[1]{%
\pgfmathsetseed{\number\day\number\time}% set a "random" seed based on time
\pgfmathdeclarerandomlist{equationlabels}{#1}%
}

\newenvironment{Equation}{\equation}%
{\pgfmathrandomitemwithoutreplacement{\rtag}{equationlabels}\tag{\rtag}\endequation}

% Mark Wibrow: https://tex.stackexchange.com/questions/113987/how-do-i-generate-in-latex-a-list-of-random-questions-avoiding-repetitions
\makeatletter
\def\pgfmathrandomitemwithoutreplacement#1#2{%
\pgfmath@ifundefined{pgfmath@randomlist@#2}{\pgfmath@error{Unknown random list #2'}}{%
\edef\pgfmath@randomlistlength{\csname pgfmath@randomlist@#2\endcsname}%
\ifnum\pgfmath@randomlistlength>0\relax%
\pgfmathrandominteger{\pgfmath@randomtemp}{1}{\pgfmath@randomlistlength}%
\def\pgfmath@marshal{\def#1}%
\expandafter\expandafter\expandafter\pgfmath@marshal\expandafter\expandafter\expandafter{\csname pgfmath@randomlist@#2@\pgfmath@randomtemp\endcsname}%
% Now prune.
\c@pgfmath@counta=\pgfmath@randomtemp\relax%
\c@pgfmath@countb=\c@pgfmath@counta%
\pgfmathloop%
\ifnum\c@pgfmath@counta=\pgfmath@randomlistlength\relax%
\else%
\def\pgfmath@marshal{\expandafter\global\expandafter\let\csname pgfmath@randomlist@#2@\the\c@pgfmath@counta\endcsname=}%
\expandafter\pgfmath@marshal\csname pgfmath@randomlist@#2@\the\c@pgfmath@countb\endcsname%
\repeatpgfmathloop%
\expandafter\xdef\csname pgfmath@randomlist@#2\endcsname{\the\c@pgfmath@counta}%
\else%
\pgfmath@error{Random list #2' is empty}%
\fi%
}}
\makeatletter

\begin{document}

% set some labels
\EquationLabels{{$\spadesuit$}{$\heartsuit$}{$\diamondsuit$}{$\clubsuit$}}

\begin{Equation}1+1=2\end{Equation}

\begin{Equation}1+2=3\end{Equation}

\begin{Equation}1+3=4\end{Equation}

\begin{Equation}1+4=5\end{Equation}

% a longer list of labels
\EquationLabels{abcdefghijklmnopqrstuvwxyz}

\begin{Equation}1+1=2\end{Equation}

\begin{Equation}1+2=3\end{Equation}

\begin{Equation}1+3=4\end{Equation}

\begin{Equation}1+4=5\end{Equation}

\end{document}


A few remarks:

• The hard part is done by Wibrow's code for \pgfmathrandomitemwithoutreplacement
• the equations are typeset inside a normal equation environment
• the labels for the equations are set using \EquationLabels
• if there are more equations than labels then you get an uninformative error from PGF Math Error saying that the random list equationlabels is empty.

You can set the predetermined symbol numbering sequence using \ifcase as a redefinition of \theequation:

\documentclass{article}

\renewcommand{\theequation}{%
\ifcase\value{equation}%
\or $\spadesuit$% 1
\or $\heartsuit$% 2
\or $\clubsuit$% 3
\or $\diamondsuit$% 4
\or $\star$% 5
\or $\alpha$% 6
\or $\beta$% 7
\or $\gamma$% 8
\or $\epsilon$% 9
\else \arabic{equation}% 10...
\fi
}

\begin{document}

$$1 + 2 = 3$$
$$2 + 3 = 5$$
$$3 + 5 = 8$$
$$5 + 8 = 13$$
$$8 + 13 = 21$$
$$13 + 21 = 34$$
$$21 + 34 = 55$$
$$34 + 55 = 89$$
$$55 + 89 = 144$$
$$89 + 144 = 233$$

\end{document}

• Instead of resorting to plain numbers for more equations than number of symbols, package alphalph can be used for unique symbol numbering with tags consisting of more than one symbol. – Heiko Oberdiek Nov 17 '16 at 11:26