10

I'm trying to draw two rectangles (using \fill and a cycle) where only the north/south corners are rounded. I want the blue rectangle below to have rounded corners only on the top, and I want the red rectangle on the bottom to have rounded corners only on the bottom.

Code:

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{figure}[h]
\centering

\begin{tikzpicture}
\fill [blue,rounded corners=10, draw]
  (0,0) --
  ++(5,0) --
  ++(0,5) --
  ++(-5,0) --
  cycle
  {};
\fill [red,rounded corners=10, draw]
  (0,0) --
  ++(5,0) --
  ++(0,-5) --
  ++(-5,0) --
  cycle
  {};
\end{tikzpicture}

\end{figure}

\end{document}

Output:
enter image description here

Can someone help me with this?

These questions might hint on solutions, but I couldn't get it to work with \fill or \draw: Rounded corners on only one side of a TikZ node
Draw a rectangle with rounded ends in TikZ
TikZ rectangular node with different rounded corners

L42
  • 405
  • tcolorbox does this out of the box, but perhaps, you want do do other things with your boxes –  Apr 27 '16 at 12:31
  • 1
    See this answer: http://tex.stackexchange.com/a/211345/586 Also, why not use a node? – Torbjørn T. Apr 27 '16 at 12:38
  • Thank you! As a beginner, can I ask: why use a node? I only need the shape, not any text. – L42 Apr 27 '16 at 13:01
  • 1
    Well, there are pros and cons to both I suppose. With nodes you can give the shapes names, which give access to the anchors, that can be used for e.g. relative positioning, and it lets you use the fit library (see manual). You may not need any of that for your use, of course. – Torbjørn T. Apr 27 '16 at 13:07
  • Thanks! For the figure I had in mind I only needed the shape. I will look at the manual more closely in the future. – L42 Apr 27 '16 at 13:08

5 Answers5

10

Thank you for your suggestions! I liked the method proposed in the answer Torbjørn T. linked to the most, so I'm answering here myself.

Code:

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{figure}[h]
\centering

\begin{tikzpicture}
\fill [blue,draw]
  (0,0) --
  ++(5,0) {[rounded corners=10] --
  ++(0,5) --
  ++(-5,0)} --
  cycle
  {};
\fill [red,draw]
  (0,0) --
  ++(5,0) {[rounded corners=10] --
  ++(0,-5) --
  ++(-5,0)} --
  cycle
  {};
\end{tikzpicture}

\end{figure}

\end{document}

Output:
enter image description here

L42
  • 405
  • 1
    I like this one. The other answers seem to provide alternatives ways of achieving the same effect without directly answering the question from the title. This answer generalizes better to other contexts in which a similar effect is wanted, but the tricks from the other answers cannot be applied. – DCTLib Apr 28 '16 at 10:27
7

One box and a path picture:

\documentclass[tikz,border=5]{standalone}
\tikzset{bicolor/.style args={#1 and #2}{
  path picture={
    \tikzset{rounded corners=0}
    \fill [#1] (path picture bounding box.west)
      rectangle (path picture bounding box.north east);
    \fill [#2] (path picture bounding box.west)
      rectangle (path picture bounding box.south east);
}}}
\begin{document}
\begin{tikzpicture}
\path [bicolor={blue and red}, rounded corners=2ex] 
  (0,0) rectangle (2,4);
\end{tikzpicture}
\end{document}  

enter image description here

Mark Wibrow
  • 70,437
3
\documentclass{article}

\usepackage[most]{tcolorbox}



\begin{document}




\begin{figure}[h]

\centering
\begingroup % for `\offinterlineskip` 
\offinterlineskip
\tcbset{arc=0.5cm,auto outer arc}
\begin{tcolorbox}[nobeforeafter,after=\par\nointerlineskip,sharp corners=south,height=4cm,colback=blue,boxrule=0pt,width=4cm]
\end{tcolorbox}
\begin{tcolorbox}[nobeforeafter,after=\par\nointerlineskip,sharp corners=north,height=4cm,colback=red,boxrule=0pt,width=4cm]
\end{tcolorbox}
\endgroup
\end{figure}

\end{document}

enter image description here

  • Using the bicolor option, this could be done even with one box alone –  Apr 27 '16 at 12:46
3
\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{figure}[h]
\centering

\begin{tikzpicture}
\path[clip,rounded corners=10](0,-5) rectangle (5,5);
\fill [blue]
  (0,0) --
  ++(5,0) --
  ++(0,5) --
  ++(-5,0) --
  cycle
  {};
\fill [red]
  (0,0) --
  ++(5,0) --
  ++(0,-5) --
  ++(-5,0) --
  cycle
  {};
\end{tikzpicture}

\end{figure}

\end{document}

enter image description here

AboAmmar
  • 46,352
  • 4
  • 58
  • 127
3

Another solution. Same rectangle is drawn twice, but the second one filled with another color and clipped to just show half of it.

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{figure}[h]
\centering

\begin{tikzpicture}
\fill [blue,rounded corners=10, draw]
  (0,0) rectangle (5,10);
\begin{scope}
\clip (0,0) rectangle (5,5);
\fill [red,rounded corners=10, draw]
  (0,0) rectangle (5,10);
\end{scope}
\end{tikzpicture}

\end{figure}

\end{document}

enter image description here

Ignasi
  • 136,588