如何在 \def 或 \newcommand 命令中存储数字?

如何在 \def 或 \newcommand 命令中存储数字?

在下面的代码中,我通过命令将中心存储到点 I \incenter(A,B,C)(I)

我想通过命令将 in-radius 存储为实数 \inr \inradius(A,B,C)(inr),然后稍后我可以使用\draw (I) circle(\inr);。但是,它不起作用。

有人可以帮忙吗?

在此处输入图片描述

\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{calc}
\begin{document}
% a point on the in-bisector (PGF manual, page 1008)    
\def\bisector(#1,#2,#3)(#4){
\path let 
\p1=($(#2)-(#1)$),\p2=($(#2)-(#3)$), 
\n1={veclen(\x1,\y1)},\n2={veclen(\x2,\y2)}
in ($(#1)!scalar(\n1/(\n1+\n2))!(#3)$) coordinate (#4);
}

% In-center
\def\incenter(#1,#2,#3)(#4){
\bisector(#1,#2,#3)(p2)
\bisector(#1,#3,#2)(p3)
\coordinate (#4) at (intersection of #2--p2 and #3--p3);
}

% In-radius (expression is from marmot's suggestion)
\def\inradius(#1,#2,#3)(#4){
\path let 
\p1=($(#2)-(#1)$), 
\p2=($(#3)-(#1)$),
\p3=($(#2)-(#3)$),
\n1={.5* 
(veclen(\x1,\y1)+veclen(\x2,\y2)+veclen(\x3,\y3))},
\n2={sqrt(((\n1-veclen(\x1,\y1))/\n1))*sqrt((\n1- 
veclen(\x2,\y2))*(\n1-veclen(\x3,\y3)))},
in 
\pgfextra{\xdef\inr{\n2}};
%\pgfmathsetmacro{#4}{\n2}; % <<<I want to store to #4 
}

\begin{tikzpicture}
\path
(-1,0) coordinate (B)
(4,0) coordinate (C)
(0,3.5) coordinate (A);

\bisector(C,A,B)(M)
\bisector(A,B,C)(N)
\bisector(B,C,A)(P)
\incenter(A,B,C)(I)
\inradius(A,B,C)(inr)

\draw[blue] (A)--(M) (B)--(N) (C)--(P); 
\draw (A)--(B)--(C)--cycle;
\fill[red] (I) circle(1.5pt);
\draw[red] (I) circle(\inr);

\path 
(A) node[above]{A} 
(B) node[below left]{B}
(C) node[below right]{C}
(M) node[below]{M}
(N) node[above right]{N}
(P) node[above left]{P};
\end{tikzpicture}
\end{document}

答案1

你只需要

\xdef#4{\n2}

或者

\expandafter\xdef\csname #4\endcsname{\n2}

取决于你是否想使用

\inradius(A,B,C)(\inr)

或者

\inradius(A,B,C)(inr)

完整代码:

\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{calc}
\begin{document}
% a point on the in-bisector (PGF manual, page 1008)    
\def\bisector(#1,#2,#3)(#4){
\path let 
\p1=($(#2)-(#1)$),\p2=($(#2)-(#3)$), 
\n1={veclen(\x1,\y1)},\n2={veclen(\x2,\y2)}
in ($(#1)!scalar(\n1/(\n1+\n2))!(#3)$) coordinate (#4);
}

% In-center
\def\incenter(#1,#2,#3)(#4){
\bisector(#1,#2,#3)(p2)
\bisector(#1,#3,#2)(p3)
\coordinate (#4) at (intersection of #2--p2 and #3--p3);
}

% In-radius (expression is from marmot's suggestion)
\def\inradius(#1,#2,#3)(#4){
\path let 
\p1=($(#2)-(#1)$), 
\p2=($(#3)-(#1)$),
\p3=($(#2)-(#3)$),
\n1={.5* 
(veclen(\x1,\y1)+veclen(\x2,\y2)+veclen(\x3,\y3))},
\n2={sqrt(((\n1-veclen(\x1,\y1))/\n1))*sqrt((\n1- 
veclen(\x2,\y2))*(\n1-veclen(\x3,\y3)))},
in 
\pgfextra{\xdef#4{\n2}};
%\pgfmathsetmacro{#4}{\n2}; % <<<I want to store to #4 
}

\begin{tikzpicture}
\path
(-1,0) coordinate (B)
(4,0) coordinate (C)
(0,3.5) coordinate (A);

\bisector(C,A,B)(M)
\bisector(A,B,C)(N)
\bisector(B,C,A)(P)
\incenter(A,B,C)(I)
\inradius(A,B,C)(\inr)

\draw[blue] (A)--(M) (B)--(N) (C)--(P); 
\draw (A)--(B)--(C)--cycle;
\fill[red] (I) circle(1.5pt);
\draw[blue,dashed] (I) circle(\inr);

\path 
(A) node[above]{A} 
(B) node[below left]{B}
(C) node[below right]{C}
(M) node[below]{M}
(N) node[above right]{N}
(P) node[above left]{P};
\end{tikzpicture}
\end{document}

在此处输入图片描述

或者如果你不想添加反斜杠

\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{calc}
\begin{document}
% a point on the in-bisector (PGF manual, page 1008)    
\def\bisector(#1,#2,#3)(#4){
\path let 
\p1=($(#2)-(#1)$),\p2=($(#2)-(#3)$), 
\n1={veclen(\x1,\y1)},\n2={veclen(\x2,\y2)}
in ($(#1)!scalar(\n1/(\n1+\n2))!(#3)$) coordinate (#4);
}

% In-center
\def\incenter(#1,#2,#3)(#4){
\bisector(#1,#2,#3)(p2)
\bisector(#1,#3,#2)(p3)
\coordinate (#4) at (intersection of #2--p2 and #3--p3);
}

% In-radius (expression is from marmot's suggestion)
\def\inradius(#1,#2,#3)(#4){
\path let 
\p1=($(#2)-(#1)$), 
\p2=($(#3)-(#1)$),
\p3=($(#2)-(#3)$),
\n1={.5* 
(veclen(\x1,\y1)+veclen(\x2,\y2)+veclen(\x3,\y3))},
\n2={sqrt(((\n1-veclen(\x1,\y1))/\n1))*sqrt((\n1- 
veclen(\x2,\y2))*(\n1-veclen(\x3,\y3)))},
in 
\pgfextra{\expandafter\xdef\csname #4\endcsname{\n2}};
%\pgfmathsetmacro{#4}{\n2}; % <<<I want to store to #4 
}

\begin{tikzpicture}
\path
(-1,0) coordinate (B)
(4,0) coordinate (C)
(0,3.5) coordinate (A);

\bisector(C,A,B)(M)
\bisector(A,B,C)(N)
\bisector(B,C,A)(P)
\incenter(A,B,C)(I)
\inradius(A,B,C)(inr)

\draw[blue] (A)--(M) (B)--(N) (C)--(P); 
\draw (A)--(B)--(C)--cycle;
\fill[red] (I) circle(1.5pt);
\draw[blue,dashed] (I) circle(\inr);

\path 
(A) node[above]{A} 
(B) node[below left]{B}
(C) node[below right]{C}
(M) node[below]{M}
(N) node[above right]{N}
(P) node[above left]{P};
\end{tikzpicture}
\end{document}

相关内容