8

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}

enter image description here

M B
  • 152
  • 1
    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}`

    – Black Mild Oct 14 '21 at 08: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 – Rmano Oct 14 '21 at 09:16
  • @Rmano Is it much more difficult to draw a node with a three-colored background? – M B Oct 14 '21 at 09:18
  • Well, it's different. There is one answer of mine for circles here: https://tex.stackexchange.com/a/566081/38080 that you can adapt – Rmano Oct 14 '21 at 09:21
  • @Rmano This is rather difficult to adapt to the case of a rectangle... – M B Oct 14 '21 at 09:27
  • 1
    You mean: "... like the flag of French Cameroon" !! – hpekristiansen Oct 14 '21 at 09:51
  • @MB why do you want to draw it as a node? need its bounding anchors to connect nodes? – Black Mild Oct 14 '21 at 15:29
  • 1
    @BlackMild I need it to connect to another rectangle. Is it natural to use nodes to do this? – M B Oct 14 '21 at 23:01

3 Answers3

10

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: enter image description here

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}

enter image description here

Juan Castaño
  • 28,426
7

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}

enter image description here

Ignasi
  • 136,588
  • I think this will be the best answer if you add some styles for columns, e.g. see the @Rmano's answer in this thread https://tex.stackexchange.com/a/618926/140722 – Black Mild Oct 15 '21 at 11:34
5

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}

enter image description here

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

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