我有以下问题
如果没有必要,我想避免\foreach
在宏中使用循环,但我不知道如何在 tikz 图片中进行测试,我理解为什么 \ifthenelse 无法工作。在我的示例中,如果刻度设置为 0,则我不想使用 foreach 循环。
\documentclass[tikz]{standalone}
\pgfkeys{/tikz/.cd,
CylCol/.store in=\CylCol,
CylCol=gray,
CylFillCol/.store in=\CylFillCol,
CylFillCol=blue,
CylFill/.store in=\CylFill,
CylFill=0,
CylRatio/.store in=\CylRatio,
CylRatio=.08,
CylGrad/.store in=\CylGrad,
CylGrad=5,
CylSecondGrad/.store in=\CylSecondGrad,
CylSecondGrad=0,
}
\newcommand{\TikzCylindre}[1][]{%
\begin{scope}[#1]
% Grisé des surfaces du cylindre.
\shade[left color=\CylCol!30, right color=\CylCol!5]%
(-1,1)--(-1,0) arc (180:360:1 and \CylRatio) --(1,0) -- (1,1);
\shade[left color=\CylCol!5, right color=\CylCol!30]%
(0,1) ellipse (1 and \CylRatio);
\draw[\CylCol!50] (1,0) arc (0:180:1 and \CylRatio)--(-1,0) ;
% Remplissage du cylindre
\shade[left color=\CylFillCol!40, right color=\CylFillCol!10]%
(-1,\CylFill)--(-1,0) arc (180:360:1 and \CylRatio) --(1,0) -- (1,\CylFill);
\fill[color=\CylFillCol!25] (0,\CylFill) ellipse (1 and \CylRatio);
\draw[\CylFillCol!50!black!50] (1,0) arc (0:180:1 and \CylRatio)--(-1,0) ;
\draw[\CylFillCol!50!black!50] (0,\CylFill) ellipse (1 and \CylRatio) ;
% dessin des bords du cylindre
\draw[semithick] (-1,1)--(-1,0) arc (180:360:1 and \CylRatio)--(1,0)--(1,1);
\draw[semithick] (0,1) ellipse (1 and \CylRatio);
\begin{scope}[shift={(0,-\CylRatio)}]
\pgfmathtruncatemacro\CylMaxGrad{\CylGrad-1}
\foreach \y in {1,2,...,\CylMaxGrad}
{\draw[semithick] (0,\y/\CylGrad)
arc (270:260:1 and \CylRatio) ;
\draw[semithick] (0,\y/\CylGrad)
arc (270:280:1 and \CylRatio) (0.2,\y/\CylGrad)
node[right,yslant=\CylRatio](\y){\footnotesize\y};
};
\pgfmathtruncatemacro\CylMaxGrad{\CylSecondGrad-1}
\foreach \y in {1,2,...,\CylMaxGrad}
{\draw(0,\y/\CylSecondGrad) arc (270:265:1 and \CylRatio) ;
\draw (0,\y/\CylSecondGrad) arc (270:275:1 and \CylRatio) ;
};
\end{scope} ;
\end{scope}
}
\begin{document}
\begin{tikzpicture}
\TikzCylindre[x=1.5cm,y=4cm,CylFill=.8,CylSecondGrad=10] ;
\end{tikzpicture}
\end{document}
欢迎任何其他改进。
答案1
您最初的问题是:
首先:我需要做一些事情,例如:\foreach \y in {1,2,...,\max-1},但 \max-1 不起作用
第二:如果没有必要,我想避免在宏中使用 \foreach 循环,但我不知道如何在 tikz 图片中进行测试,我理解为什么 \ifthenelse 无法工作。在我的示例中,如果刻度设置为 0,则我不想使用 foreach 循环。
对于您的两个问题中的第一个,一种方法是引入一个新的计数器mytemp
并按如下方式使用它:
\setcounter{mytemp}{\CylGrad}
\addtocounter{mytemp}{-1}
\foreach \y in {1,2,...,\themytemp}
或者,正如 Alain 建议的那样,避免使用反击,而是使用
\foreach \y in {1,2,...,\the\numexpr\CylGrad-1}
对于第二个问题,使用\ifnum
\ifnum\CylGrad>0
\foreach \y in {1,2,...,\the\numexpr\CylGrad-1}
{\draw[semithick] (0,\y/\CylGrad)
arc (270:260:1 and \CylRatio) ;
\draw[semithick] (0,\y/\CylGrad)
arc (270:280:1 and \CylRatio) (0.2,\y/\CylGrad)
node[right,yslant=\CylRatio](\y){\footnotesize\y};
};
\fi
以下是您的 MWE 对这些内容的演示:
\documentclass[tikz]{standalone}
\pgfkeys{/tikz/.cd,
CylCol/.store in=\CylCol,
CylCol=gray,
CylFillCol/.store in=\CylFillCol,
CylFillCol=blue,
CylFill/.store in=\CylFill,
CylFill=0,
CylRatio/.store in=\CylRatio,
CylRatio=.08,
CylGrad/.store in=\CylGrad,
CylGrad=5,
CylSecondGrad/.store in=\CylSecondGrad,
CylSecondGrad=0,
}
\newcommand{\TikzCylindre}[1][]{%
\begin{scope}[#1]
% Grisé des surfaces du cylindre.
\shade[left color=\CylCol!30, right color=\CylCol!5]%
(-1,1)--(-1,0) arc (180:360:1 and \CylRatio) --(1,0) -- (1,1);
\shade[left color=\CylCol!5, right color=\CylCol!30]%
(0,1) ellipse (1 and \CylRatio);
\draw[\CylCol!50] (1,0) arc (0:180:1 and \CylRatio)--(-1,0) ;
% Remplissage du cylindre
\shade[left color=\CylFillCol!40, right color=\CylFillCol!10]%
(-1,\CylFill)--(-1,0) arc (180:360:1 and \CylRatio) --(1,0) -- (1,\CylFill);
\fill[color=\CylFillCol!25] (0,\CylFill) ellipse (1 and \CylRatio);
\draw[\CylFillCol!50!black!50] (1,0) arc (0:180:1 and \CylRatio)--(-1,0) ;
\draw[\CylFillCol!50!black!50] (0,\CylFill) ellipse (1 and \CylRatio) ;
% dessin des bords du cylindre
\draw[semithick] (-1,1)--(-1,0) arc (180:360:1 and \CylRatio)--(1,0)--(1,1);
\draw[semithick] (0,1) ellipse (1 and \CylRatio);
\begin{scope}[shift={(0,-\CylRatio)}]
\ifnum\CylGrad>0
\foreach \y in {1,2,...,\the\numexpr\CylGrad-1}
{\draw[semithick] (0,\y/\CylGrad)
arc (270:260:1 and \CylRatio) ;
\draw[semithick] (0,\y/\CylGrad)
arc (270:280:1 and \CylRatio) (0.2,\y/\CylGrad)
node[right,yslant=\CylRatio](\y){\footnotesize\y};
};
\fi
\ifnum\CylSecondGrad>0
\foreach \y in {1,2,...,\the\numexpr\CylSecondGrad}
{\draw(0,\y/\CylSecondGrad) arc (270:265:1 and \CylRatio) ;
\draw (0,\y/\CylSecondGrad) arc (270:275:1 and \CylRatio) ;
};
\fi
\end{scope} ;
\end{scope}
}
\begin{document}
\begin{tikzpicture}
\TikzCylindre[x=1.5cm,y=4cm,CylFill=.8,CylSecondGrad=10] ;
\end{tikzpicture}
\end{document}