5

Below code use positioning packet to put ruler line between two color lines. But the result not as expected!

\documentclass[border=1pt,varwidth=5cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\newcommand\colorrulemix[1]{\textcolor{#1!40!gray}{\rule{0.5cm}{0.5cm}} }
\newcommand\colorrule[1]{\textcolor{#1}{\rule{0.5cm}{0.5cm}} }
\begin{tikzpicture}[node distance=0]
    \foreach \name [count=\i] in {{red},{orange},{yellow},{green},{cyan},{blue},{purple}} {
        \node (P\i) at (\i*0.6cm,0) {\colorrule{\name}};
    }  
    \node[below=0.1 of P4] {\rule{4.5cm}{1pt}};
    \foreach \name [count=\i] in {{red},{orange},{yellow},{green},{cyan},{blue},{purple}} {
        \node [below=0.2 of P\i] (Q\i) {\colorrulemix{\name}};
    }
\end{tikzpicture}
\end{document}

enter image description here

6

The main reason why your approach does not quite work is that the nodes have some standard dimensions, most notably inner sep, which distorts the (vertical) position of your bar node. If you remove the inner sep, the bar will sit right below the baseline that is in the middle. You could then shift it down by half its thickness. It is, however, easy enough to draw a perfectly (I hope;-) centered bar with TikZ methods.

\documentclass[border=1pt,varwidth=5cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\newcommand\colorrulemix[1]{\textcolor{#1!40!gray}{\rule{0.5cm}{0.5cm}} }
\newcommand\colorrule[1]{\textcolor{#1}{\rule{0.5cm}{0.5cm}} }

Your picture:\\
\begin{tikzpicture}[node distance=0]
    \foreach \name [count=\i] in {{red},{orange},{yellow},{green},{cyan},{blue},{purple}} {
        \node (P\i) at (\i*0.6cm,0) {\colorrule{\name}};
    }  
    \node[below=0.1 of P4] {\rule{4.5cm}{1pt}};
    \foreach \name [count=\i] in {{red},{orange},{yellow},{green},{cyan},{blue},{purple}} {
        \node [below=0.2 of P\i] (Q\i) {\colorrulemix{\name}};
    }
\end{tikzpicture}

Your picture with \texttt{inner sep=0pt}:\\
\begin{tikzpicture}[node distance=0]
    \foreach \name [count=\i] in {{red},{orange},{yellow},{green},{cyan},{blue},{purple}} {
        \node (P\i) at (\i*0.6cm,0) {\colorrule{\name}};
    }  
    \node[below=0.1 of P4,inner sep=0pt] {\rule{4.5cm}{1pt}};
    \foreach \name [count=\i] in {{red},{orange},{yellow},{green},{cyan},{blue},{purple}} {
        \node [below=0.2 of P\i] (Q\i) {\colorrulemix{\name}};
    }
\end{tikzpicture}

Your picture with \texttt{inner sep=0pt} and a cross--check:\\
\begin{tikzpicture}[node distance=0]
    \foreach \name [count=\i] in {{red},{orange},{yellow},{green},{cyan},{blue},{purple}} {
        \node (P\i) at (\i*0.6cm,0) {\colorrule{\name}};
    }  
    \node[below=0.1 of P4,inner sep=0pt] {\rule{4.5cm}{1pt}};
    \foreach \name [count=\i] in {{red},{orange},{yellow},{green},{cyan},{blue},{purple}} {
        \node [below=0.2 of P\i] (Q\i) {\colorrulemix{\name}};
    }
    \path (Q1) -- (P1) coordinate[midway] (aux) ;
    \draw[red](aux) -- ++ (4,0);
\end{tikzpicture}

An alternative with really centered line:\\
\begin{tikzpicture}[node distance=0]
    \foreach \name [count=\i] in {{red},{orange},{yellow},{green},{cyan},{blue},{purple}} {
        \node (P\i) at (\i*0.6cm,0) {\colorrule{\name}};
    }  
    \foreach \name [count=\i] in {{red},{orange},{yellow},{green},{cyan},{blue},{purple}} {
        \node [below=0.2 of P\i] (Q\i) {\colorrulemix{\name}};
    }
    \path (Q4) -- (P4) coordinate[midway] (aux) ;
    \draw[line width=1pt]([xshift=-2.25cm]aux) -- ++ (4.5,0);
\end{tikzpicture}
\end{document}

enter image description here

4

You can arrange these nodes into a matrix and draw lines between rows:

\documentclass[tikz,border=1pt]{standalone} 
\usetikzlibrary{positioning, matrix}

\begin{document}
\begin{tikzpicture}[
    a/.style={fill=#1, minimum size=5mm, outer sep=0pt, inner sep=0pt, anchor=center},
    a/.default=red,
    b/.style={a, fill=#1!40!gray},
    b/.default=red,
    c/.style={a, fill=#1!20!gray},
    c/.default=red,
    t/.style={matrix of nodes, nodes in empty cells, 
        row sep=3mm, column sep=1mm, 
        row 1/.style={nodes=a},
        row 2/.style={nodes=b}, 
        row 3/.style={nodes=c}}]

\matrix[t] (A){
&|[a=orange]|&|[a=yellow]|&|[a=green]|&|[a=cyan]|&|[a=blue]|&|[a=purple]|\\
&|[b=orange]|&|[b=yellow]|&|[b=green]|&|[b=cyan]|&|[b=blue]|&|[b=purple]|\\
&|[c=orange]|&|[c=yellow]|&|[c=green]|&|[c=cyan]|&|[c=blue]|&|[c=purple]|\\
};

%Lines between rows
%With only two rows 
%\draw (A.west)--(A.east);
%For more than two rows:
\foreach \i [count=\ni] in {2,3}{ 
    \path (A-\ni-1.south) --coordinate (aux) (A-\i-1.north);
    \draw (A.west|-aux)--(A.east|-aux);
}

\end{tikzpicture}
\end{document}

enter image description here

3

First of all, it is useless to specify the node distance= 0 if you specify the < shift part > of the keys below of= as I explained here : TikZ: How does global/local [node distance] work?

Then, do not confuse the points of the path and the nodes. Nodes are not part of the path itself and have additional parameters such as the distance between the text and the border (inner sep) and the distance between the border and the outside (outer sep): read pages 218 and 219 of the manual 3.0.1a.

Thus, it is sufficient to specify these parameters globally

\begin{tikzpicture}[inner sep=0pt,outer sep=0pt] 

and do not forget that when you go down from 0.2, you have omitted to count the line thickness which is 1pt : \rule{4.5cm}{1pt}

It is therefore necessary to add this length.

\node[below=0.2cm+1pt of P\i] (Q\i) {\colorrulemix{\name}

The result and the final code are:

colors

\documentclass[border=1pt,varwidth=5cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\newcommand\colorrulemix[1]{\textcolor{#1!40!gray}{\rule{0.5cm}{0.5cm}} }
\newcommand\colorrule[1]{\textcolor{#1}{\rule{0.5cm}{0.5cm}} }
\begin{tikzpicture}[inner sep=0pt,outer sep=0pt]
    \foreach \name [count=\i] in {{red},{orange},{yellow},{green},{cyan},{blue},{purple}} {
        \node (P\i) at (\i*0.6cm,0) {\colorrule{\name}};
    }  
    \node[below=0.1 of P4] {\rule{4.5cm}{1pt}};
    \foreach \name [count=\i] in {{red},{orange},{yellow},{green},{cyan},{blue},{purple}} {
        \node [below=0.2cm+1pt of P\i] (Q\i) {\colorrulemix{\name}};
    }
\end{tikzpicture}
\end{document}

Translated with www.DeepL.com/Translator

  • 1
    I also use deepl translator. :-)+1. – Sebastiano Dec 4 '18 at 8:20
1

Another example to find middle point!

\documentclass[border=1pt,varwidth=5cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\newcommand\colorrulemix[1]{\textcolor{#1!40!gray}{\rule{0.5cm}{0.5cm}} }
\newcommand\colorrule[1]{\textcolor{#1}{\rule{0.5cm}{0.5cm}} }
\begin{tikzpicture}[inner sep=0]
    \foreach \name [count=\i] in {{red},{orange},{yellow},{green},{cyan},{blue},{purple}} {
        \node[] (P\i) at (\i*0.6cm,0) {\colorrule{\name}};
    }  
    \foreach \name [count=\i] in {{red},{orange},{yellow},{green},{cyan},{blue},{purple}} {
        \node [below=0.2 of P\i] (Q\i) {\colorrulemix{\name}};
    }
    \path (P1) -- coordinate (M) (Q7);
    \node[] at (M) {\rule{4.5cm}{1pt}};
    \foreach \i in {M,P1,P2,P3,P4,P5,P6,P7,Q1,Q2,Q3,Q4,Q5,Q6,Q7} {
        \draw[red,shift=(\i)] node[black] {\tiny $\i$} 
            (-.1,-.1) -- (.1,.1) (-.1,.1) -- (.1,-.1);
    }
\end{tikzpicture}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.