4

I was learning foreach command in TIKZ and I want to plot the style and the color shown as below.

the ideal result

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)

The problem of the Junction and the docs

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!

2
  • You do not want or need PGFPlot for this. Please do not consult the doc for PGFPlots and do also not \usepackage{pgfplots}. PGFPlots is for plotting graphs with data/functions. Commented Jul 5 at 19:52
  • Sorry, a little similar between pgfplots and tikz. I mistook them. Thanks!
    – Explorer
    Commented Jul 6 at 5:30

3 Answers 3

4
\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}[
ultra thick,
left/.tip={Butt Cap[sep=0pt -1]Rectangle[sep=0pt -0.5, slant=-1 ,width=0pt 1, length=0pt 2]},
right/.tip={Butt Cap[sep=0pt -1]Rectangle[sep=0pt -0.5, slant=1 ,width=0pt 1, length=0pt 2]},
]
\draw[green, -right] (0,0.5) -- (0,1);
\draw[blue, left-right] (0,1) -- (1,1);
\draw[yellow, left-right] (1,1) -- (1,0);
\draw[red, left-] (1,0) -- (0.5,0);
\end{tikzpicture}
\end{document}

Multi colored square

4
  • Magnificent. Can you explain why you set the various parameters sep, slant, length, width to these values and not to others?
    – AndréC
    Commented Jul 6 at 16:06
  • slant=±1 is to make the rectangle tip ±45 deg. width=0pt 1 sets the width or the tip to the same as the line width. The other parameters are found experimentally and are not critical to be exact. Give the tips an other color to see the effect. Commented Jul 6 at 17:58
  • Thank you, can you explain this in your wonderful reply for the benefit of as many readers as possible?
    – AndréC
    Commented Jul 6 at 19:08
  • @hpekristiansen, Interesting options that I didn't know about.
    – J Leon V.
    Commented 2 days ago
2

This is an option using areas, since the finish you require could not be done with separate draw instructions, as far as I know, but you can quickly add a couple of additional points to generate a trapezoidal shape and instead of using the last coordinates you can define For each one, a temporary storage coordinate, as in this case it is temp, then draw from this the trapezoid that follows, then change from line to fill, with this you could even use fadings or gradients for more effects.

RESULT PAGE 1:

enter image description here

RESULT PAGE 2:

enter image description here

MWE:

\documentclass[tikz,border=5pt]{standalone}
\usetikzlibrary{shadings}
\begin{document}
    \begin{tikzpicture}
        \def\N{4}
        \def\Thickness{4pt}
        \foreach \x[
            count=\n,
            evaluate=\n as \u using int(2*\n-1),
            evaluate=\n as \v using int(2*\n),
        ] in {1,...,\N}{
            \fill [color = red] (1-\n,1-\n)--++(45:\Thickness)--++(\u,0) --++(-45:\Thickness) coordinate (temp) -- cycle;
            \fill [color = yellow] (temp)--++(135:\Thickness) --++ (0,\u)--++(45:\Thickness) coordinate (temp) -- cycle;
            \fill [color = blue] (temp)--++(-135:\Thickness)--++ (-\v,0)--++(135:\Thickness) coordinate (temp) -- cycle;
            \fill [color = green] (temp)--++(-45:\Thickness)  --++ (0,-\v)--++(-135:\Thickness) -- cycle;
        }
    \end{tikzpicture}
    
    \begin{tikzpicture}
        \def\N{4}
        \def\Thickness{4pt}
        \foreach \x[
        count=\n,
        evaluate=\n as \u using int(2*\n-1),
        evaluate=\n as \v using int(2*\n),
        ] in {1,...,\N}{
            \shade [left color=red, right color=yellow] (1-\n,1-\n)--++(45:\Thickness)--++(\u,0) --++(-45:\Thickness) coordinate (temp) -- cycle;
            \shade [top color=blue, bottom color=yellow] (temp)--++(135:\Thickness) --++ (0,\u)--++(45:\Thickness) coordinate (temp) -- cycle;
            \shade [left color=green, right color=blue] (temp)--++(-135:\Thickness)--++ (-\v,0)--++(135:\Thickness) coordinate (temp) -- cycle;
            \shade [top color=green, bottom color=red] (temp)--++(-45:\Thickness)  --++ (0,-\v)--++(-135:\Thickness) -- cycle;
        }
    \end{tikzpicture}
\end{document}
1

There is no native way of making bevelled segments with Tikz. To hide this broken line effect, I increase the length of certain strokes by half the thickness of your line, which measures 0.8pt (thick), so I make it 0.4pt longer.

\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 -.4pt) --++ (0,\u);
            \pgfgetlastxy{\macrox}{\macroy}
            \draw [thick,color = blue] (\macrox +.4pt,\macroy) --++ (-\v,0);
            \pgfgetlastxy{\macrox}{\macroy}
            \draw [thick,color = green] (\macrox,\macroy +.4pt) --++ (0,-\v);
        }
        
        
    \end{tikzpicture}
\end{document}

enter image description here

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .