foreach 루프 문제에 대한 두 개의 변수

foreach 루프 문제에 대한 두 개의 변수

해당 각도 등과 같은 정보를 쓰는 펄스열을 그리고 싶습니다. 하지만 이 줄을 추가한 이후로 컴파일조차 할 수 없기 때문에 foreach 루프 두 변수에 문제가 있는 것 같습니다.

\addplot[dirac] coordinates {(\temps,0.75)};

전체 코드는 다음과 같습니다.

\documentclass[tikz]{standalone}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[french]{babel}
\usepackage{pgfplots}

\pgfplotsset{
  dirac/.style={
    mark=triangle*, mark options={scale=2}, ycomb, scatter, blue,
    visualization depends on={y/abs(y)-1 \as \sign},
    scatter/@pre marker code/.code={\scope[rotate=90*\sign,yshift=-2pt]}
  }
}

\begin{document}
  \begin{tikzpicture}
  \tikzset{
    every pin/.style={fill=yellow!50!white,rectangle,rounded                corners=3pt,font=\scriptsize},
    small dot/.style={fill=black,circle,scale=0.2}
}

\begin{axis}[axis lines=middle,x=1,xmin=-25,xmax=375,y=100,ymin=0,ymax=1,
  title={Train d'impulsions à 40MHz},ylabel={Tx},xlabel={Temps/ns},
  every axis y label/.style={at={(ticklabel cs:1)},anchor=near ticklabel},
  every axis x label/.style={at={(ticklabel cs:1)},anchor=near ticklabel},
  ytickmin=2, xtickmax=350, axis y line=left
  ]

  \foreach \temps/\angle in {0/-21, 25/-18, 50/-15, 75/-12}{
     \edef\temp{\noexpand
     \addplot[dirac] coordinates {(\temps,0.75)};
     \node[small dot,pin={$\angle\degres$}]  at (25+\temps,70) {};}
     \temp
  }

    \end{axis}
  \end{tikzpicture}
\end{document}

도와주셔서 미리 감사드립니다. 변호를 위해 저는 Tikz-PGF의 초보자입니다

답변1

axis환경이 까지 일부 항목에 대한 평가를 연기하고 있기 때문에 그때까지는 \end{axis}변수 \temps및 변수 \angle가 더 이상 존재하지 않습니다. 이 경우 루프 본문에 지정된 \pgfplotsinvokeforeach모든 항목을 루프 카운터로 즉시 대체하는 것을 사용할 수 있습니다 . #1유일한 단점은 여러 루프 변수를 지원하지 않으므로 다음과 같이 하나씩 계산해야 한다는 것입니다.

\pgfplotsinvokeforeach{0,25,...,75}{
  \addplot[dirac] coordinates {(#1,0.75)};
  \node[small dot,pin={
    \pgfmathtruncatemacro{\angle}{#1/25*3-21}%
    $\angle\degres$
  }]  at (25+#1,70) {};
}

결과는 다음과 같습니다.

결과

pgfplots추가 힌트: 를 사용하여 플롯 좌표계에서 노드 좌표를 지정하는 방법을 보려면 매뉴얼 의 섹션 4.17 "사용자 정의 주석"을 살펴봐야 합니다 axis cs.


기술적 세부 사항

을(를) 사용하여 강제로 확장 \temps하려고 시도한 것 같습니다 . 하지만 그렇게 하려면 으로 확장하고 싶지 않은 항목을 보호해야 합니다 . 이는 매우 지루한 작업입니다. 축 환경 밖으로 핀 생성을 이동하면 작동하지만 플롯에 핀을 정렬하기가 어렵습니다.\angle\edef\noexpand

게다가 \temps매크로 이름을 선택하는 것은 좋지 않은 것 같지만 이유는 알 수 없습니다. 루프에서 제거해도 \node여전히 작동하지 않습니다.

\foreach \temps/\angle in {0/-21, 25/-18, 50/-15, 75/-12}{
   \addplot[dirac] coordinates {(\temps,0.75)};
}

Incomplete \iffalse; all text was ignored after line <N>나에게 알려지지 않은 이유로 실패합니다 . 그러나 이름 \temps을 바꾸면 \temp작동합니다!

그러나 여전히 루프 내부 \node에서 명령을 사용할 수는 없습니다 . 매크로는 환경의 끝에서만 평가되기 때문에 \temp여전히 정의되지 않았다고 불평합니다 . 한 가지 해결 방법은 다음 과 같이 수동으로 확장 하고 사용하는 것 입니다.\temp\nodeaxis\temp\angle\expandafter

\def\LoopBody#1#2{%
  \addplot[dirac] coordinates {(#1,0.75)};
  \node[small dot,pin={$#2\degres$}]  at (25+#1,70) {};
}
\let\EA=\expandafter% For shorter code
\foreach \temp/\angle in {0/-21, 25/-18, 50/-15, 75/-12}{
   \EA\EA\EA\LoopBody\EA\EA\EA{\EA\temp\EA}\EA{\angle}
}

이는 먼저 \angle로 확장 -21되고 그 다음에 \temp는 (등)으로 확장됩니다. 하지만 이는 이해하고 디버깅하기 어렵기 때문에 별로 추천할 수는 없습니다. 아마도 내부를 더 잘 이해하는 사람이 이것이 왜 필요한지에 대해 더 많은 통찰력을 줄 수 있을 것입니다.0\LoopBody{0}{-21}pgfplots

관련 정보