标尺线不在颜色条的中间

标尺线不在颜色条的中间

下面的代码使用定位包将标尺线放在两条颜色线之间。但结果并不像预期的那样!

\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}

在此处输入图片描述

答案1

你的方法不太奏效的主要原因是节点有一些标准尺寸,最明显的是内分隔符,这会扭曲条形节点的(垂直)位置。如果你移除内分隔符,条形将位于中间基线的正下方。然后你可以将其向下移动一半厚度。然而,用 Ti 绘制一个完美(我希望;-)居中的条形很容易Z 方法。

\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}

在此处输入图片描述

答案2

你可以将这些节点排列成一个矩阵,并在行之间画线:

\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}

在此处输入图片描述

答案3

首先,node distance= 0如果您指定了< shift part >键,那么指定是没有用的below of=,正如我在此处解释的那样:TikZ:全局/本地[节点距离]如何工作?

然后,不要混淆points路径的 和nodes。节点不是路径本身的一部分,并且具有附加参数,例如文本与边框之间的距离(inner sep)以及边框与外部之间的距离(outer sep):阅读手册 3.0.1a 的第 218 和 219 页。

因此,全局指定这些参数就足够了

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

并且不要忘记,当您从 往下看时0.2,您已经忽略了计算线条粗细,即1pt\rule{4.5cm}{1pt}

因此有必要添加这个长度。

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

结果和最终代码如下:

颜色

\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}

使用 www.DeepL.com/Translator 翻译

答案4

另一个寻找中间点的例子!

\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}

在此处输入图片描述

相关内容