帶有 pgfplots 的嵌套 ifnum 條件組圖?

帶有 pgfplots 的嵌套 ifnum 條件組圖?

我想使用groupplots, 並設定一個變數來有條件地繪製繪圖。這是最小的非工作範例:

\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\usepgfplotslibrary{groupplots}
\def\a{1}
\begin{document}
\begin{tikzpicture
  \begin{groupplot}[group style={group size=1 by \a},height=3cm,width=6cm]
    \ifnum\a>0{ %
    \nextgroupplot[title=One]
    \addplot coordinates {(0,0) (1,1) (2,2)};
    \ifnum\a>1{ %
    \nextgroupplot[title=Two]
    \addplot coordinates {(0,2) (1,1) (2,0)};
    \ifnum\a>2{ %
    \nextgroupplot[title=Three]
    \addplot coordinates {(0,2) (1,1) (2,1)};
    \ifnum\a>3{ %
    \nextgroupplot[title=Four]
    \addplot coordinates {(0,2) (1,1) (1,0)};
    }\fi
    }\fi
    }\fi
    }\fi
  \end{groupplot}
\node (title) at (0,0) {THE Title};
\end{tikzpicture}
\end{document}

這失敗了:

! Extra }, or forgotten \endgroup.
l.24     }
          \fi

不確定是因為嵌套的\ifnums 還是因為\nextgroupplot;如果第一個\nextgroupplot被註釋,現在的錯誤是:

! Undefined control sequence.
l.11     \addplot
                  coordinates {(0,0) (1,1) (2,2)};

這樣的組圖條件繪製可以完成嗎?

答案1

放棄在構造中使用組groupplot。我添加了一些縮進,並更正了小類型(}在環境開頭丟失tikzpicture):

在此輸入影像描述

\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\usepgfplotslibrary{groupplots}
\def\a{3}
\begin{document}
\begin{tikzpicture}
  \begin{groupplot}[group style={group size=1 by \a},height=3cm,width=6cm]
    \ifnum\a>0
      \nextgroupplot[title=One]
      \addplot coordinates {(0,0) (1,1) (2,2)};
      \ifnum\a>1
        \nextgroupplot[title=Two]
        \addplot coordinates {(0,2) (1,1) (2,0)};
        \ifnum\a>2
          \nextgroupplot[title=Three]
          \addplot coordinates {(0,2) (1,1) (2,1)};
          \ifnum\a>3
            \nextgroupplot[title=Four]
            \addplot coordinates {(0,2) (1,1) (1,0)};
          \fi
        \fi
      \fi
    \fi
  \end{groupplot}
\end{tikzpicture}
\end{document}

相關內容