第二版

第二版

我有兩張圖(兩者應該是相同的),一張具有顯式繪圖和節點命令,另一張帶有foreach循環。顯然,我不理解 foreach 循環,因為我只能讓一個循環工作。我遵循了 tikz 教程,但它很快就超出了我的理解範圍。我試著透過反覆試驗來理解 tikz 邏輯,但在嘗試了看似簡單的事情後我變得有點灰心。我需要其他人的幫助。我請求大家給我一個正確的方向,讓我知道如何做到這一點。我需要如何設計循環來縮短第一個圖的程式碼。當這些行被註解時,第二個不起作用。

\documentclass{article}
\usepackage[margin=20mm]{geometry}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
\draw (0,0) rectangle +(10.5,8);
\draw (0.1,0.1) rectangle +(10.3,7.8);
\foreach \x in {1.5,4,6.5,9}
    \foreach \y in {1.5,4,6.5}
        \draw (\x,\y) circle (1);

\node at (1.5,1.5) {PreA30};
\node at (1.5,4) {PreB30};
\node at (1.5,6.5) {PreC30};

\node at (4,1.5) {PreA50};
\node at (4,4) {PreB50};
\node at (4,6.5) {PreC50};

\node at (6.5,1.5) {PreA70};
\node at (6.5,4) {PreB70};
\node at (6.5,6.5) {PreC70};

\node at (9,1.5) {PreA90};
\node at (9,4) {PreB90};
\node at (9,6.5) {PreC90};

\node at (1.5,-.5) {30\%};
\node at (4,-.5) {50\%};
\node at (6.5,-.5) {70\%};
\node at (9,-.5) {90\%};
\node at (-.5,1.5) {A};
\node at (-.5,4) {B};
\node at (-.5,6.5) {C};
\end{tikzpicture}

\begin{tikzpicture}
\draw (0,0) rectangle +(10.5,8);
\draw (0.1,0.1) rectangle +(10.3,7.8);

\foreach \x in {1.5,4,6.5,9}
    \foreach \y in {1.5,4,6.5}
        \draw (\x,\y) circle (1);

% the next block actually compiles, but the result is quite alarming
% I tried a different order, but that doesn't seem to matter at all
%\foreach \x in {1.5,4,6.5,9}
%   \foreach \y in {1.5,4,6.5}
%       \foreach \sam in {A,B,C}
%           \foreach \perc in {30,50,70,90}
%               \node at (\x,\y) {Pre\sam\perc};
%
%\foreach \x in {1.5,4,6.5,9}
%   \foreach \perc in {30,50,70,90}
%       \node at (\x,-.5) {\perc\%};
%
%\foreach \y in {1.5,4,6.5}
%   \foreach \sam in {A,B,C}
%       \node at {-.5,\y} {\sam};
\end{tikzpicture}

\end{document}

有了土撥鼠的回答,我們就得到了下圖所示的結果。第一個圖就是我們想要的。第二個圖在所有節點中都有文字疊加。我知道這可能比 LaTeX 更需要邏輯,但我仍然無法完全理解 for 迴圈的正確設計。 期望的和不期望的效果

答案1

您有四個巢狀循環,而您只需要兩個,但您需要有多個循環變數。這可以用例如來完成

\foreach \x/\perc in {1.5/30,4/50,6.5/70,9/90}

即變數清單中的每個項目包含兩個值,以 分隔/

關於您的原始程式碼,循環工作正常,唯一的問題是您在最後一個程式碼中使用了\node at {-.5,\y} {\sam};而不是。 \node at (-.5,\y) {\sam};(座標中用花括號代替圓括號。)修正了疊印問題,因為對於每對\x/\y值,您列印12 個節點:三個不同的字母(A、B、C)和四個不同的數字(30、 50) , 70, 90),給出 12 種組合。

\documentclass{article}
\usepackage[margin=20mm]{geometry}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
\draw (0,0) rectangle +(10.5,8);
\draw (0.1,0.1) rectangle +(10.3,7.8);
\foreach \x in {1.5,4,6.5,9}
    \foreach \y in {1.5,4,6.5}
        \draw (\x,\y) circle (1);

\node at (1.5,1.5) {PreA30};
\node at (1.5,4) {PreB30};
\node at (1.5,6.5) {PreC30};

\node at (4,1.5) {PreA50};
\node at (4,4) {PreB50};
\node at (4,6.5) {PreC50};

\node at (6.5,1.5) {PreA70};
\node at (6.5,4) {PreB70};
\node at (6.5,6.5) {PreC70};

\node at (9,1.5) {PreA90};
\node at (9,4) {PreB90};
\node at (9,6.5) {PreC90};

\node at (1.5,-.5) {30\%};
\node at (4,-.5) {50\%};
\node at (6.5,-.5) {70\%};
\node at (9,-.5) {90\%};
\node at (-.5,1.5) {A};
\node at (-.5,4) {B};
\node at (-.5,6.5) {C};
\end{tikzpicture}

\begin{tikzpicture}
\draw (0,0) rectangle +(10.5,8);
\draw (0.1,0.1) rectangle +(10.3,7.8);

\foreach \x/\perc in {1.5/30,4/50,6.5/70,9/90}
   { % here you need to use braces, because there is more than one thing in the loop
   \node at (\x,-0.5) {\perc\%};
   \foreach \y/\sam in {1.5/A,4/B,6.5/C}
       {
       \draw (\x,\y) circle (1);
       \node at (\x,\y) {Pre\sam\perc};
       }
   }

% second loop for y-labels
\foreach \y/\sam in {1.5/A,4/B,6.5/C}
   \node at (-0.5,\y) {\sam};

\end{tikzpicture}
\end{document}

在此輸入影像描述

第二版

還有更多可用選項\foreach,如第 83 章所述手冊。對於像這樣具有規則間隔的節點的情況,使用該count選項可能是有意義的,它為您提供了循環變數的計數器。此外,您也可以用來evaluate進行計算。

這樣,每個循環就可以有一個循環變量,並根據循環中的迭代計算座標。輸出如上。

\documentclass{article}
\usepackage[margin=20mm]{geometry}
\usepackage{tikz}
\usetikzlibrary{fit, backgrounds}
\begin{document}

\begin{tikzpicture}

\foreach [count=\i, evaluate={\x=\i*2.5}] \perc in {30,50,70,90}
   { % here you need to use braces, because there is more than one thing in the loop
   \foreach [count=\j, evaluate={\y=\j*2.5}] \sam in {A,B,C}
       {
       \node [circle,draw,minimum size=2cm] (\sam\perc) at (\x,\y) {Pre\sam\perc};
       \ifnum \i=1
         \node [left=5mm] at (\sam30.west) {\sam};
       \fi
       } % inner loop ends here
   % so this node is only in outer loop
   \node [below=5mm] at (A\perc.south) {\perc\%};
   }

\begin{scope}[on background layer]
  % fit both around same nodes, with different inner sep
  \node[draw,fit=(A30)(C90),inner sep=3mm, fill=blue!20] (frame) {}; % outer frame, larger inner sep
  \node[draw,fit=(A30)(C90),inner sep=2mm, fill=red!20] (frame) {}; % inner frame, smaller inner sep
\end{scope}
\end{tikzpicture}
\end{document}

與上面的填充背景框一樣

答案2

我知道這個問題是關於 的foreach,但是如果有人在理解foreach使用方面遇到問題,可以在matrix幫助下輕鬆重現此圖:

\documentclass[tikz,border=2mm]{standalone} 
\usetikzlibrary{positioning, matrix, fit}

\begin{document}
\begin{tikzpicture}

\matrix[matrix of nodes,
            nodes={anchor=center, 
                circle, draw, minimum size=2cm},
            column 1/.style={%
                nodes={rectangle, minimum size=0pt, draw=none}},
            row 4/.style={%
                nodes={rectangle, minimum size=0pt, draw=none}},
            column sep=5mm, row sep=5mm]
(top)
{
C & PreC30 & PreC50 & PreC70 & PreC90 \\
B & PreB30 & PreB50 & PreB70 & PreB90 \\
A & PreA30 & PreA50 & PreA70 & PreA90 \\
  & 30\% & 50\% & 70\% & 90\% \\
};
\node[draw, double, fit=(top-1-2) (top-3-5), inner sep=3mm] {};
\end{tikzpicture}
\end{document}

在此輸入影像描述

答案3

如果使用\foreach循環,則需要將循環內的所有內容放入一組中,即用{和包圍它,}除非循環中只有一個命令/組。將其應用到您的程式碼中會產生

\documentclass{article}
\usepackage[margin=20mm]{geometry}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
\draw (0,0) rectangle +(10.5,8);
\draw (0.1,0.1) rectangle +(10.3,7.8);
\foreach \x in {1.5,4,6.5,9}
    \foreach \y in {1.5,4,6.5}
        \draw (\x,\y) circle (1);

\node at (1.5,1.5) {PreA30};
\node at (1.5,4) {PreB30};
\node at (1.5,6.5) {PreC30};

\node at (4,1.5) {PreA50};
\node at (4,4) {PreB50};
\node at (4,6.5) {PreC50};

\node at (6.5,1.5) {PreA70};
\node at (6.5,4) {PreB70};
\node at (6.5,6.5) {PreC70};

\node at (9,1.5) {PreA90};
\node at (9,4) {PreB90};
\node at (9,6.5) {PreC90};

\node at (1.5,-.5) {30\%};
\node at (4,-.5) {50\%};
\node at (6.5,-.5) {70\%};
\node at (9,-.5) {90\%};
\node at (-.5,1.5) {A};
\node at (-.5,4) {B};
\node at (-.5,6.5) {C};
\end{tikzpicture}

\begin{tikzpicture}
\draw (0,0) rectangle +(10.5,8);
\draw (0.1,0.1) rectangle +(10.3,7.8);

\foreach \x in {1.5,4,6.5,9}
    {\foreach \y in {1.5,4,6.5}
        \draw (\x,\y) circle (1);}

% the next block actually compiles, but the result is quite alarming
% I tried a different order, but that doesn't seem to matter at all
\foreach \x in {1.5,4,6.5,9}
  {\foreach \y in {1.5,4,6.5}
      \foreach \sam in {A,B,C}
          {\foreach \perc in {30,50,70,90}
              \node at (\x,\y) {Pre\sam\perc};}}

\foreach \x in {1.5,4,6.5,9}
  {\foreach \perc in {30,50,70,90}
      \node at (\x,-.5) {\perc\%};}

\foreach \y in {1.5,4,6.5}
  {\foreach \sam in {A,B,C}
      \node at (-.5,\y) {\sam};}
\end{tikzpicture}

\end{document}

該代碼現在為您提供了我認為您想要獲得的內容。

相關內容