为什么自定义常量在 tikz 中不起作用?

为什么自定义常量在 tikz 中不起作用?
\documentclass{beamer}

\mode<presentation>
{
  \usetheme{AnnArbor}      % or try Darmstadt, Madrid, Warsaw, ...
  \usecolortheme{wolverine} % or try albatross, beaver, crane, ...
  \usefonttheme{default}  % or try serif, structurebold, ...
  \setbeamertemplate{navigation symbols}{}
  \setbeamertemplate{caption}[numbered]
} 

\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{color}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{patterns,arrows,decorations.pathreplacing,calc} 



\begin{document}



\begin{frame}{}

\begin{figure}[H]
\centering

\begin{tikzpicture}[xscale=5,yscale=5]

\draw [<->] (0,1)--(0,0)--(1,0);


\def\constal{2/3};

\def\constax{\constal}

\def\constax{(1-\constal)^2}

\def\constA{\constx/(\constx+\consty)};

\def\constB{(\constal-\constx)/(1-\constx-\consty)};

\def\constC{(1-2*\constal+\constx)/(\constx+\consty)};

\def\constD{\constal^2/(\constal^2+(1-\constal)^2)};

\def\constE{1/2};

\def\constF{(1-\constal)^2/(\constal^2+(1-\constal)^2)};

\def\constG{(\constal^2+(1-\constal)^2)/2};

\def\constH{2*\constal*(1-\constal)};

\def\constI{(\constx+\consty)/2};

\def\constJ{1-\constx-\consty};

\node [below] at (1,0) {$\beta_1$};
\node [left] at (0,1) {$\beta_4$};

\draw ({(\constD-\constC)/(\constA-\constC)},{0})--({(\constD-\constC)/(\constA-\constC)},{1});

\draw[domain=0:1] plot (\x, {-\constG/\constH*\x+(\constA*\constI-\constF*\constG)/(\constA*\constH)});

\end{tikzpicture}

\end{figure}

\end{frame}


\end{document}

我定义了几个常量,但它们在 tikz 中无法使用。错误是什么?

答案1

另一个选项(代替或补充\pgfmathsetmacro)是使用declare function。然后,当您想要打印值时,您需要一些技巧(我siunitx为此使用,它有很多格式选项)。

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usepackage{siunitx}
\begin{document}
\newcommand{\print}[2][round-mode=figures, round-precision=3]{%
    \pgfmathsetmacro{\rpval}{#2}%
    \num[#1]{\rpval}
}
\tikzset{declare function={
        a=2/3;
        b(\x)=(\x)^2 +1;
    }}
\begin{tikzpicture}[]
    \draw[cyan] (0,0) grid (2,2);
    \draw ({a}, 0) -- ({b(a)}, 1); % {} are needed only in the second
                                   % coord, but better keep consinstency
    \node[below]{a=\print{a}, b=\print{b(a)}};
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容