
다중 부분 노드에 대해 계산된 값을 사용하려고 할 때 이상한 동작이 발생했습니다. 때로는 값을 사용할 수 없습니다(정의되지 않은 제어 순서 오류가 발생함).
MWE는 다음과 같습니다.
\documentclass[border=1mm] {standalone}
\usepackage{tikz}
\usetikzlibrary{shapes,calc}
\newcommand{\splitcalcsb}[2]{
\pgfmathsetmacro\calcOne{ #2 / #1 }
\pgfmathsetmacro\calcTwo{ \calcOne + #1 }
\node (d0)[shape=rectangle split, rectangle split parts=4, fill=red!20, draw] at (0,0)
{Detail 0%
\nodepart{two}\calcOne
\nodepart{three}\calcTwo
};
}
\newcommand{\splitcalcs}[2]{
\def\calcOne{\fpeval { #2 / #1 }}
\def\calcTwo{\fpeval{ #2 / #1 + #1 }}
% Uncomment these two lines to get proper functioning
% \global\let\calcOne=\calcOne
% \global\let\calcTwo=\calcTwo
\nodepart{two}\calcOne
\nodepart{three}\calcTwo
}
\begin{document}
\begin{tikzpicture}
%Uncomment only this line to avoid an error but Detail 1 uses the results of this macro call
%\splitcalcsb{3}{1}
\node (d1)[shape=rectangle split, rectangle split parts=4, fill=red!20, draw] at (4,0)
{Detail 1%
\splitcalcs{8}{4}
};
\splitcalcsb{9}{4}
\end{tikzpicture}
\end{document}
게시된 대로 코드를 실행하면 35행에서 오류가 발생합니다.
해당 줄의 주석 처리를 제거하면 \splitcalcsb{3}{1}
오류가 더 이상 발생하지 않지만 다음 이미지가 표시됩니다.
세부정보 1의 내용은 \splictcalcsb{3}{1}
호출이 아닌 호출 계산을 반영합니다 \splitcalcs{8}{4}
.
해당 줄의 주석 처리를 제거하면 \global\let
모든 것이 제대로 작동하지만 정의를 만드는 것은 피하고 싶습니다 \global
.
\pgfmathsetmacro
(a) 매크로에서 중간 계산을 정의하는 데 사용하고 (b) \nodepart
전역 변수를 사용하지 않고 동일한 매크로 호출에서 해당 계산 결과를 사용하는 방법을 아는 사람이 있습니까 ?
답변1
@egreg는 유사한 질문에 대한 답변에서 이것이 어떻게 수행되어야 하는지를 보여주었습니다.
매크로와 pgfmath를 사용하여 계산된 값을 분할 모양에 배치하려면 어떻게 해야 합니까?
\noexpand
및 을 사용합니다 \expanded
.
\documentclass[border=1mm] {standalone}
\usepackage{tikz}
\usetikzlibrary{shapes,calc}
\newcommand{\splitcalcs}[2]{
\pgfmathsetmacro\calcOne{ #2 / #1 }
\pgfmathsetmacro\calcTwo{ \calcOne + #1 }
\expanded{%
\noexpand\nodepart{two} \calcOne
\noexpand\nodepart{three} \calcTwo
}%
}
\begin{document}
\begin{tikzpicture}
\node (d1)[shape=rectangle split, rectangle split parts=4, fill=red!20, draw] at (4,0)
{Detail 1%
\splitcalcs{8}{4}
};
\end{tikzpicture}
\end{document}