tuple ของ TikZ ใน PGF

Oct 21 2020

ฉันต้องการพล็อตรายการข้อมูลและเพิ่มตามรายการตำนาน ฉันพยายามใช้\foreachสำหรับ tuples ซึ่งติดตั้งในpgfforแพ็คเกจและโหลดด้วย TikZ ขออภัยตัวแปรที่สองที่อธิบายป้ายกำกับ\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}