I was learning foreach command in TIKZ and I want to plot the style and the color shown as below.
After consulting the doc of pgfplots, I achieve it by the code as below:
\documentclass{ctexart}
\usepackage{tikz}
\usepackage{xfp}
\usepackage{pgfplots}
\newcounter{cnt}
\setcounter{cnt}{1}
\begin{document}
\begin{tikzpicture}
\pgfmathtruncatemacro{\N}{4}
\foreach \x[
count=\n,
evaluate=\n as \u using int(2*\n-1),
evaluate=\n as \v using int(2*\n),
] in {1,...,\N}{
% \draw (1-\n,1-\n) --++ (\u,0) --++ (0,\u) --++ (-\v,0) --++ (0,-\v);
% \draw [thick,color = red] (1-\n,1-\n) --++ (\u,0);
% \draw [thick,color = yellow] (1-\n+\u,1-\n) --++ (0,\u);
% \draw [thick,color = blue] (1-\n+\u,1-\n+\u) --++ (-\v,0);
% \draw [thick,color = green] (1-\n+\u-\v,1-\n+\u) --++ (0,-\v);
\draw [thick,color = red] (1-\n,1-\n) --++(\u,0);
\pgfgetlastxy{\macrox}{\macroy}
\draw [thick,color = yellow] (\macrox,\macroy) --++ (0,\u);
\pgfgetlastxy{\macrox}{\macroy}
\draw [thick,color = blue] (\macrox,\macroy) --++ (-\v,0);
\pgfgetlastxy{\macrox}{\macroy}
\draw [thick,color = green] (\macrox,\macroy) --++ (0,-\v);
}
\end{tikzpicture}
\end{document}
Unfortunely, when I enlarge the pdf, and I found that the corner of two path didn't connect with thick lines(Shown as below)
To use the \foreach and change the color in the loop I have to use four \draw commands, it cause the problem. Although it can be fixed with \draw (1-\n,1-\n) --++ (\u,0) --++ (0,\u) --++ (-\v,0) --++ (0,-\v);
But it couldn't change the color.
It is a dilemma! Maybe it can be configured to set the tail of each path to be an acute Angle of 45°, which in the junction it can reach a better lookings. But I don;t know how to code by PGF/TIKZ. Thanks!
\usepackage{pgfplots}
. PGFPlots is for plotting graphs with data/functions.