2

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
3

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:

enter image description here

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%
            \advance\c@pgfmath@countb by1\relax%
            \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%
                \advance\c@pgfmath@counta by1\relax%
                \advance\c@pgfmath@countb by1\relax%
            \repeatpgfmathloop%
            \advance\c@pgfmath@counta by-1\relax%
            \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.
1

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

enter image description here

\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}

\begin{equation}  1 +   2 =   3 \end{equation}
\begin{equation}  2 +   3 =   5 \end{equation}
\begin{equation}  3 +   5 =   8 \end{equation}
\begin{equation}  5 +   8 =  13 \end{equation}
\begin{equation}  8 +  13 =  21 \end{equation}
\begin{equation} 13 +  21 =  34 \end{equation}
\begin{equation} 21 +  34 =  55 \end{equation}
\begin{equation} 34 +  55 =  89 \end{equation}
\begin{equation} 55 +  89 = 144 \end{equation}
\begin{equation} 89 + 144 = 233 \end{equation}

\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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.