tikz 适合圆角:颜色交叉角

tikz 适合圆角:颜色交叉角

母语: https://www.latex4technics.com?note=zzvqsa

\documentclass{standalone}
\usepackage[table]{xcolor}
\usepackage{tikz}
\begin{document}
\usetikzlibrary{fit} 

\begin{tikzpicture}


\node[fill=green,inner sep=0pt] (content) {
    \begin{tabular}{lr}
        \cellcolor{blue!25} & \cellcolor{red!25} 
    \end{tabular}
}; % node
\node[draw,inner sep=0pt,rounded corners,fit=(content)](a){};

\end{tikzpicture}
\end{document}

在此处输入图片描述

问题:彩色尖角不能被圆角裁剪问题:怎么办?

答案1

您可以使用path picturemultifill此处定义的样式采用颜色列表并用这些颜色填充节点背景。它会均等地分割填充,但可以将其概括为允许任意分数。

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[multifill/.style={path picture={
  \edef\icnt{0}%
  \edef\ppbb{path picture bounding box}%
  \tikzset{step/.list={#1}}%
  \foreach\mycolor [count=\istep]in {#1}
  {\pgfmathsetmacro{\myxfl}{(\istep-1)/\icnt} 
  \pgfmathsetmacro{\myxfr}{\istep/\icnt} 
  \fill[\mycolor,rounded corners=false] 
    ($(\ppbb.south west)!\myxfl!(\ppbb.south east)$)
    rectangle ($(\ppbb.north west)!\myxfr!(\ppbb.north east)$);}
    }},step/.code={\edef\icnt{\the\numexpr\icnt+1}}]
 \path node[draw,minimum width=2cm,minimum height=1cm,
    rounded corners,multifill={blue!20,red!20}] {}
    (4,0) node[draw,minimum width=2.5cm,minimum height=1cm,
    rounded corners,multifill={blue!20,red!20,orange!20}]   {};
    
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

如果使用\draw而不是附加节点,则可以“白化”角落。

使用even odd rule填充,如果您绘制一个矩形然后又绘制另一个矩形,则只有内部“边框”会被填充。另请参阅使用 TikZ 绘制带圆孔的圆盘

\documentclass[border=5pt]{standalone}
\usepackage[table]{xcolor}
\usepackage{tikz}
\begin{document}
\usetikzlibrary{fit}

\begin{tikzpicture}

\node[fill=green,inner sep=0pt, ] (content) {
    \begin{tabular}{lr}
        \cellcolor{blue!25} & \cellcolor{red!25}
    \end{tabular}
}; % node
\path [fill=white, rounded corners, even odd rule]
    (content.north west) rectangle (content.south east)
    ([xshift=-3pt, yshift=3pt]content.north west) rectangle
    ([xshift=3pt, yshift=-3pt]content.south east)
    ;
\draw [rounded corners]
    (content.north west) rectangle (content.south east);

\end{tikzpicture}
\end{document}

在此处输入图片描述

答案3

为什么不rectangle splitshapes.multipart库中使用?

矩形分割

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{shapes.multipart,fit}

\begin{document}
    \begin{tikzpicture}
        \node[rounded corners,rectangle split,rectangle split horizontal,
    rectangle split parts=2,rectangle split part fill={blue!25,red!25}] (A) {};
        \node[rounded corners,fit=(A),draw,inner sep=0pt] {};
    \end{tikzpicture}
\end{document}

相关内容