# Tricolored Rectangle Using Tikz

I am trying to create a tricolored rectangle in which I can specify the point where the color changes (i.e. not necessarily equally spaced like the French flag). I have included a MWE below that colors the rectangle uniformly and an image of the desired result.

\documentclass[tikz,margin=2mm]{standalone}
\begin{document}
\begin{tikzpicture}
\node [rectangle, minimum width=2cm, fill=blue] (A) {}
\end{tikzpicture}
\end{document}


• like this ? \documentclass[tikz,border=5mm]{standalone} \begin{document} \begin{tikzpicture}[thick,fill opacity=.5] \def\a{1} \def\b{3} \def\c{2} \draw[fill=red!] (0,0) rectangle +(\a,1) coordinate (B); \draw[fill=green] (B) rectangle +(\b,-1) coordinate (C); \draw[fill=blue] (C) rectangle +(\c,1); \end{tikzpicture} \end{document} Oct 14, 2021 at 8:58
• It is quite different if you want to draw a rectangle with colors (in that case @BlackMild comment is the answer) or if you want a node with the three-colored background Oct 14, 2021 at 9:16
• @Rmano Is it much more difficult to draw a node with a three-colored background?
– M B
Oct 14, 2021 at 9:18
• You mean: "... like the flag of French Cameroon" !! Oct 14, 2021 at 9:51
• @BlackMild I need it to connect to another rectangle. Is it natural to use nodes to do this?
– M B
Oct 14, 2021 at 23:01

It's not very difficult to make a \pic which draws the rectangle and its three parts. This way you can put nodes in it at your desired positions.

For example, the code:

\documentclass[border=2mm,tikz]{standalone}

\tikzset
{% USAGE:
% #1 = rectangle width
% #2 = rectangle height
% #3 = relative position of the first  division (0-1), between left   and middle rectangles
% #4 = relative position of the second division (0-1), between middle and right  rectangles
% #5 = left   part color
% #6 = middle part color
% #7 = right  part color
pics/tricolor/.style n args={7}{%
code={%
\path[fill=#5] (0,0)     rectangle (#1*#3,#2);
\path[fill=#6] (#1*#3,0) rectangle (#1*#4,#2);
\path[fill=#7] (#1*#4,0) rectangle (#1,#2);
\draw (0,0) rectangle (#1,#2);
\draw (#1*#3,0) --+ (0,#2);
\draw (#1*#4,0) --+ (0,#2);
\coordinate (-left)   at (0.5*#1*#3       ,0.5*#2);
\coordinate (-middle) at ({0.5*#1*(#3+#4)},0.5*#2);
\coordinate (-right)  at ({0.5*#1*(#4+1)} ,0.5*#2);
}}
}

\begin{document}
\begin{tikzpicture}
\pic (A) {tricolor={6}{1}{0.15}{0.6}{green!70!black}{red}{yellow}};
\node at (A-left)   {left};
\node at (A-middle) {middle};
\node at (A-right)  {right};

\pic (B) at (0.5,-4) {tricolor={5}{3}{0.33}{0.67}{blue!80}{white}{red}};
\node at (B-left)   {Vive};
\node at (B-middle) {la};
\node at (B-right)  {France};
\end{tikzpicture}
\end{document}


Will produce:

Edit: Introducing rounded corners, as requested. I draw first the left and right rectangles bigger and with rounded corners (as an option passed to the \pic) and then the center rectangle over them and without rounded corners.

Note that if the radii of the corners is bigger than the width of the center rectangle it will not look good.

\documentclass[border=2mm,tikz]{standalone}

\tikzset
{% USAGE:
% #1 = rectangle width
% #2 = rectangle height
% #3 = relative position of the first  division (0-1), between left   and middle rectangles
% #4 = relative position of the second division (0-1), between middle and right  rectangles
% #5 = left   part color
% #6 = middle part color
% #7 = right  part color
pics/tricolor/.style n args={7}{%
code={%
\path[fill=#5]                   (0,0)     rectangle (#1*#4,#2);
\path[fill=#7]                   (#1*#3,0) rectangle (#1,#2);
\path[fill=#6,rounded corners=0] (#1*#3,0) rectangle (#1*#4,#2); % this rectangle without rounded corners
\draw (0,0) rectangle (#1,#2);
\draw (#1*#3,0) --+ (0,#2);
\draw (#1*#4,0) --+ (0,#2);
\coordinate (-left)   at (0.5*#1*#3       ,0.5*#2);
\coordinate (-middle) at ({0.5*#1*(#3+#4)},0.5*#2);
\coordinate (-right)  at ({0.5*#1*(#4+1)} ,0.5*#2);
}}
}

\begin{document}
\begin{tikzpicture}
\pic[rounded corners=0.5cm] (B) at (0.5,-4) {tricolor={5}{3}{0.33}{0.67}{blue!80}{white}{red}};
\node at (B-left)   {Vive};
\node at (B-middle) {la};
\node at (B-right)  {France};
\end{tikzpicture}
\end{document}


• Great response! Is it possible to make the flag with curved edges (e.g. rounded corners=0.5ex)?
– M B
Oct 14, 2021 at 9:53
• @MB, yes it's possible with little changes. See my edit. Oct 14, 2021 at 10:10
• just note that rounded corners does not work well with the scale option Oct 14, 2021 at 11:24
• One small question: how can I adjust the width and length of the flag? I'm having trouble finding where this can be adjusted.
– M B
Oct 14, 2021 at 23:05
• @jsbibra, it's done. It was not well explained, thanks for pointing it. Oct 15, 2021 at 6:12

You can use a matrix with equal height nodes

\documentclass[border=2mm, tikz]{standalone}
\usetikzlibrary{matrix}

\begin{document}
\begin{tikzpicture}
\matrix (A) [matrix of nodes, inner sep=0pt, draw, nodes={anchor=center, inner sep=0.3333em, minimum height=1cm}, row sep=-\pgflinewidth]
{|[fill=red!30]|This is\strut & |[fill=green!30]| a tricolor\strut & |[fill=blue!30]| node (or matrix)\\};
\end{tikzpicture}
\end{document}


This is a rework of my previous answer in https://tex.stackexchange.com/a/566081/38080... with a rectangle is easier. Just use the calc intermediate coordinates (remember: ($(A)!x!(B)$) is a point on the line from (A) to (B), where x is the relative position: x=0 is the same as (A), x=0.5 is midway etc...)

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,calc,backgrounds}
\begin{document}
\begin{tikzpicture}[
]
% draw the node with no background
\node[draw] (R) {divided by 20\%, 30\%, 50\%};
% and after that...
\begin{scope}[on background layer]
\fill [green!20] (R.south west) rectangle ($(R.north west)!.2!(R.north east)$);
\fill [orange!20] ($(R.south west)!.2!(R.south east)$) rectangle ($(R.north west)!.5!(R.north east)$);
\fill [purple!20] ($(R.south west)!.5!(R.south east)$) rectangle (R.north east);
\end{scope}
\end{tikzpicture}
\end{document}


You can easily wrap this in a macro if you like.

• This is helpful. Is it possible to make the rectangle have curved edges (e.g. rounded corners=0.5ex)?
– M B
Oct 14, 2021 at 10:02
• Possible, yes, but a bit more complex for the coloring (probably needing a clip) Oct 14, 2021 at 11:23