无法编译给定的代码

无法编译给定的代码

当我经过时,

如何用 LaTeX 自动绘制素数分解树状图?

并尝试编译以下代码以获取答案中给定的输出,但不幸的是遇到一些错误。

有人能帮帮我吗?我犯了什么错误。

代碼:

\documentclass[tikz]{standalone}
\usepackage{forest,mathtools,siunitx}
\makeatletter
\def\ifNum#1{\ifnum#1\relax
  \expandafter\pgfutil@firstoftwo\else
  \expandafter\pgfutil@secondoftwo\fi}
\forestset{
  num content/.style={
    delay={
      content/.expanded={\noexpand\num{\forestoption{content}}}}},
  pt@prime/.style={draw, circle},
  pt@start/.style={},
  pt@normal/.style={},
  start primeTree/.style={%
    /utils/exec=%
      % \pt@start holds the current minimum factor, we'll start with 2
      \def\pt@start{2}%
      % \pt@result will hold the to-be-typeset factorization, we'll start with
      % \pgfutil@gobble since we don't want a initial \times
      \let\pt@result\pgfutil@gobble
      % \pt@start@cnt holds the number of ^factors for the current factor
      \def\pt@start@cnt{0}%
      % \pt@lStart will later hold "l"ast factor used
      \let\pt@lStart\pgfutil@empty,
    alias=pt-start,
    pt@start/.try,
    delay={content/.expanded={$\noexpand\num{\forestove{content}}
                            \noexpand\mathrlap{{}= \noexpand\pt@result}$}},
    primeTree},
  primeTree/.code=%
    % take the content of the node and save it in the count
    \c@pgf@counta\forestove{content}\relax
    % if it's 2 we're already finished with the factorization
    \ifNum{\c@pgf@counta=2}{%
      % add the factor
      \pt@addfactor{2}%
      % finalize the factorization of the result
      \pt@addfactor{}%
      % and set the style to the prime style
      \forestset{pt@prime/.try}%
    }{%
      % this simply calculates content/2 and saves it in \pt@end
      % this is later used for an early break of the recursion since no factor
      % can be greater then content/2 (for integers of course)
      \edef\pt@content{\the\c@pgf@counta}%
      \divide\c@pgf@counta2\relax
      \advance\c@pgf@counta1\relax % to be on the safe side
      \edef\pt@end{\the\c@pgf@counta}%
      \pt@do}}

%%% our main "function"
\def\pt@do{%
  % let's test if the current factor is already greather then the max factor
  \ifNum{\pt@end<\pt@start}{%
    % great, we're finished, the same as above
    \expandafter\pt@addfactor\expandafter{\pt@content}%
    \pt@addfactor{}%
    \def\pt@next{\forestset{pt@prime/.try}}%
  }{%
    % this calculates int(content/factor)*factor
    % if factor is a factor of content (without remainder), the result will
    % equal content. The int(content/factor) is saved in \pgf@temp.
    \c@pgf@counta\pt@content\relax
    \divide\c@pgf@counta\pt@start\relax
    \edef\pgf@temp{\the\c@pgf@counta}%
    \multiply\c@pgf@counta\pt@start\relax
    \ifNum{\the\c@pgf@counta=\pt@content}{%
      % yeah, we found a factor, add it to the result and ...
      \expandafter\pt@addfactor\expandafter{\pt@start}%
      % ... add the factor as the first child with style pt@prime
      % and the result of int(content/factor) as another child.
      \edef\pt@next{\noexpand\forestset{%
        append={[\pt@start, pt@prime/.try]},
        append={[\pgf@temp, pt@normal/.try]},
        % forest is complex, this makes sure that for the second child, the
        % primeTree style is not executed too early (there must be a better way).
        delay={
          for descendants={
            delay={if n'=1{primeTree, num content}{}}}}}}%
    }{%
      % Alright this is not a factor, let's get the next factor
      \ifNum{\pt@start=2}{%
        % if the previous factor was 2, the next one will be 3
        \def\pt@start{3}%
      }{%
        % hmm, the previos factor was not 2,
        % let's add 2, maybe we'll hit the next prime number
        % and maybe a factor
        \c@pgf@counta\pt@start
        \advance\c@pgf@counta2\relax
        \edef\pt@start{\the\c@pgf@counta}%
      }%
      % let's do that again
      \let\pt@next\pt@do
    }%
  }%
  \pt@next
}

%%% this builds the \pt@result macro with the factors
\def\pt@addfactor#1{%
  \def\pgf@tempa{#1}%
  % is it the same factor as the previous one
  \ifx\pgf@tempa\pt@lStart
    % add 1 to the counter
    \c@pgf@counta\pt@start@cnt\relax
    \advance\c@pgf@counta1\relax
    \edef\pt@start@cnt{\the\c@pgf@counta}%
  \else
    % a new factor! Add the previous one to the product of factors
    \ifx\pt@lStart\pgfutil@empty\else
      % as long as there actually is one, the \ifnum makes sure we do not add ^1
      \edef\pgf@tempa{\noexpand\num{\pt@lStart}\ifnum\pt@start@cnt>1 
                                           ^{\noexpand\num{\pt@start@cnt}}\fi}%
      \expandafter\pt@addfactor@\expandafter{\pgf@tempa}%
    \fi
    % setup the macros for the next round
    \def\pt@lStart{#1}% <- current (new) factor
    \def\pt@start@cnt{1}% <- first time
  \fi
}
%%% This simply appends "\times #1" to \pt@result, with etoolbox this would be
%%% \appto\pt@result{\times#1}
\def\pt@addfactor@#1{%
  \expandafter\def\expandafter\pt@result\expandafter{\pt@result \times #1}}

%%% Our main macro:
%%% #1 = possible optional argument for forest (can be tikz too)
%%% #2 = the number to factorize
\newcommand*{\PrimeTree}[2][]{%
  \begin{forest}%
    % as the result is set via \mathrlap it doesn't update the bounding box
    % let's fix this:
    tikz={execute at end scope={\pgfmathparse{width("${}=\pt@result$")}%
                         \path ([xshift=\pgfmathresult pt]pt-start.east);}},
    % other optional arguments
    #1
    % And go!
    [#2, start primeTree]
  \end{forest}}
\makeatother
\begin{document}
\PrimeTree{36}
\PrimeTree{90}
\PrimeTree{112}
\PrimeTree{612}
\PrimeTree{7875}
\PrimeTree{22230}
\PrimeTree{1073741824}
\PrimeTree{2147483644}
\end{document}

错误:

line 143: Extra }, or forgotten $. \PrimeTree{36}
line 143: Extra }, or forgotten $. \PrimeTree{36}
line 143: Extra }, or forgotten $. \PrimeTree{36}
line 143: Missing $ inserted. \PrimeTree{36}
line 143: Missing } inserted. \PrimeTree{36}
line 143: Missing } inserted. \PrimeTree{36}
line 143: Missing } inserted. \PrimeTree{36}
line 145: Extra }, or forgotten $. \PrimeTree{112}
line 145: Extra }, or forgotten $. \PrimeTree{112}
line 145: Extra }, or forgotten $. \PrimeTree{112}
line 145: Missing $ inserted. \PrimeTree{112}
line 145: Missing } inserted. \PrimeTree{112}
line 145: Missing } inserted. \PrimeTree{112}
line 145: Missing } inserted. \PrimeTree{112}
line 146: Extra }, or forgotten $. \PrimeTree{612}
line 146: Extra }, or forgotten $. \PrimeTree{612}
line 146: Extra }, or forgotten $. \PrimeTree{612}
line 146: Missing $ inserted. \PrimeTree{612}
line 146: Missing } inserted. \PrimeTree{612}
line 146: Missing } inserted. \PrimeTree{612}
line 146: Missing } inserted. \PrimeTree{612}
line 147: Extra }, or forgotten $. \PrimeTree{7875}
line 147: Extra }, or forgotten $. \PrimeTree{7875}
line 147: Extra }, or forgotten $. \PrimeTree{7875}
line 147: Missing $ inserted. \PrimeTree{7875}
line 147: Missing } inserted. \PrimeTree{7875}
line 147: Missing } inserted. \PrimeTree{7875}
line 147: Missing } inserted. \PrimeTree{7875}
line 149: Extra }, or forgotten $. \PrimeTree{1073741824}
line 149: Extra }, or forgotten $. \PrimeTree{1073741824}
line 149: Extra }, or forgotten $. \PrimeTree{1073741824}
line 149: Missing $ inserted. \PrimeTree{1073741824}
line 149: Missing } inserted. \PrimeTree{1073741824}
line 149: Missing } inserted. \PrimeTree{1073741824}
line 149: Missing } inserted. \PrimeTree{1073741824}
line 150: Extra }, or forgotten $. \PrimeTree{2147483644}
line 150: Extra }, or forgotten $. \PrimeTree{2147483644}
line 150: Extra }, or forgotten $. \PrimeTree{2147483644}
line 150: Missing $ inserted. \PrimeTree{2147483644}
line 150: Missing } inserted. \PrimeTree{2147483644}
line 150: Missing } inserted. \PrimeTree{2147483644}
line 150: Missing } inserted. \PrimeTree{2147483644}

相关内容