PGF의 TikZ \ foreach 튜플
Oct 21 2020
데이터 목록을 플로팅하고 범례 항목에 따라 추가하고 싶습니다. 패키지 \foreach로 구현되고 pgfforTikZ로로드 된 튜플 에 사용하려고했습니다 . 불행히도 레이블을 설명하는 두 번째 변수 \clabel는 LaTeX에 대해 정의되지 않은 것으로 보입니다. 이 구조는 외부에서 사용하면 작동하는 것 같습니다 \begin{axis} ... \end{axis}. TikZ / PGF와 관련하여 중요한 점이 있습니까?
최소한의 예
\documentclass[tikz,border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{
compat=1.16,
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[]
\foreach \c/\clabel in {1/a, 2/b}
{
\addplot {\c^2};
\addlegendentry{ \clabel }
};
\end{axis}
\end{tikzpicture}
\end{document}
답변
1 StevenB.Segletes Oct 21 2020 at 18:47
플롯이 완료된 후 범례 자체가 생성되므로에 \clabel대한 인수 로 확장해야합니다 \addlegendentry. 이 시점에서의 로컬 값 \clabel이 손실되었습니다.
\documentclass[tikz,border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{
compat=1.16,
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[]
\foreach \c/\clabel in {1/a, 2/b}
{
\addplot {\c^2};
\expandafter\addlegendentry\expandafter{\clabel};
}
\end{axis}
\end{tikzpicture}
\end{document}