매크로와 pgfmath를 사용하여 계산된 값을 분할 모양에 배치하려면 어떻게 해야 합니까?

매크로와 pgfmath를 사용하여 계산된 값을 분할 모양에 배치하려면 어떻게 해야 합니까?

여러 노드 부분으로 구성된 복잡한 분할 모양이 있습니다. 노드 부분에 계산된 값을 입력하고 싶습니다.

\pgfmathsetmacro를 사용하여 이를 수행하려고 하면 정의되지 않은 제어 시퀀스 오류가 발생합니다.

코드는 다음과 같습니다.

\documentclass[letterpaper, 12 pt] {article}

\usepackage{tikz}
\usetikzlibrary{shapes,calc}

\newcommand{\splitcalcs}[2]{
  \pgfmathsetmacro\calcOne{ #2 / #1 }
  \pgfmathsetmacro\calcTwo{ \calcOne + #1 }
   \nodepart{two}\calcOne
   \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}

명령어 안에 노드 선언을 넣으면 다 잘 되니까 평가 시점과 관련이 있는 것 같은데 고칠 만큼 아는 게 없네요.

작동하는(바람직하지 않은) 코드는 다음과 같습니다.

\documentclass[letterpaper, 12 pt] {article}

\usepackage{tikz}
\usetikzlibrary{shapes,calc}

\newcommand{\splitcalcsb}[2]{
  \pgfmathsetmacro\calcOne{ #2 / #1 }
  \pgfmathsetmacro\calcTwo{ \calcOne + #1 }
  \node (d1)[shape=rectangle split, rectangle split parts=4, fill=red!20, draw] at (4,0) 
{Detail 1%
   \nodepart{two}\calcOne
   \nodepart{three}\calcTwo
  };
    }

\begin{document} 

\begin{tikzpicture}

  \splitcalcsb{8}{4}

 \end{tikzpicture}
\end{document}

답변1

\nodepart새로운 셀 정렬이 시작되고 이전에 수행된 할당이 너무 잊혀진 것 같습니다 .

\nodepart실행되기 전에 필요한 부분을 확장할 수 있습니다 . 시기 적절하지 않은 확장을 피하기 위해 \noexpand앞에 필요합니다 .\nodepart

\documentclass[letterpaper, 12pt] {article}

\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 \splitcalcs{8}{4}};
\end{tikzpicture}

\end{document}

\expanded상당히 최신의 TeX 배포판이 필요합니다 . "전통적인" 방법으로도 동일한 결과를 얻을 수 있습니다.

  \begingroup\edef\x{\endgroup
    \noexpand\nodepart{two}\calcOne
    \noexpand\nodepart{three}\calcTwo
  }\x

여기에 이미지 설명을 입력하세요

답변2

이러한 목적을 위해 xfp편리할 수 있습니다. (경고: xfpTi가 있기 때문에 조심하지 않고는 좌표 계산을 대체할 수 없습니다.케이Z는 단위로 스칼라와 길이를 구별합니다.)

\documentclass[letterpaper, 12 pt] {article}
\usepackage{xfp}
\usepackage{tikz}
\usetikzlibrary{shapes,calc}

\newcommand{\splitcalcs}[2]{%
   \nodepart{two}\fpeval{#2/#1}
   \nodepart{three}\fpeval{1+#2/#1}
  }

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

여기에 이미지 설명을 입력하세요

답변3

아하, 문제는 "1부"에 국한된다는 \calcOne것 입니다. 처음에 \calcTwo넣으면 \nodepart{one}똑같은 결과를 얻을 수 있습니다. (이후의 모든 것을 "3부"에 넣음) \nodepart{two}을 포함하여 이후의 모든 내용을 "2부"에 배치합니다 .\nodepart{three}

\documentclass[letterpaper, 12 pt] {article}

\usepackage{tikz}
\usetikzlibrary{shapes,calc}

\newcommand{\splitcalcs}[2]{%
  \pgfmathparse{ #2 / #1 }%
  \global\let\calcOne=\pgfmathresult
  \pgfmathparse{ \calcOne + #1 }%
  \global\let\calcTwo=\pgfmathresult
  \nodepart{two}\calcOne
  \nodepart{three}\calcTwo
  }

\begin{document} 

\begin{tikzpicture}

  \node (d1)[shape=rectangle split, rectangle split parts=4, fill=red!20, draw] at (4,0) 
{Detail%
  \splitcalcs{8}{4}};
 \end{tikzpicture}
\end{document}

관련 정보