設定環境以自動隨機地用符號標記方程

設定環境以自動隨機地用符號標記方程

因此,使用方程式環境 和\label{ ... },可以輕鬆獲得在文件中自動編號的方程式。如果我們想自訂標記文檔的符號,我們可以使用\tag{ ... }.

然而,是否有一種很好的方法來設定環境,以便在指定的文檔中,方程式會自動用隨機符號標記?這裡,潛在符號集將在文件中的某處預先指定,並且不同的方程式將被分配不同的符號。

答案1

PGF包提供了用於處理隨機列表的命令\pgfmathdeclarerandomlist和。\pgfmathrandomitem由於您可能希望最多使用一次方程式標籤,因此最好的選擇是在使用清單項目時刪除它們,但這很困難。幸運的是,Mark Wibrow 在回答這個問題時給了一個方法如何在 LaTeX 中產生隨機問題清單以避免重複?

有了 Mark 的程式碼,就可以輕鬆執行您想要的操作,\tag從使用者指定的標籤清單中新增隨機標籤:

在此輸入影像描述

這是代碼:

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

幾點說明:

  • 最困難的部分是由 Wibrow 的程式碼完成的\pgfmathrandomitemwithoutreplacement
  • 方程式在正規方程式環境中排版
  • 方程式的標籤使用以下命令設置\EquationLabels
  • 如果方程式多於標籤,那麼您會從 PGF Math Error 中收到無訊息錯誤,指出隨機清單equationlabels為空。

答案2

\ifcase您可以使用以下指令的重新定義來設定預定的符號編號序列\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}

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

相關內容