Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

How do I make a tikz rectangle node with given lower-left and upper-right corners, having a given border color and fill color? I know it seems like a basic question, but I cannot find an example in the manual! I tried this:

\documentclass[tikz,border=0.25cm]{standalone}
\begin{document}
\begin{tikzpicture}
\node [shape=rectangle, thin, color=red, fill=red, fill opacity=0.4, minimum width=0.5599999999999998, minimum height=3.3999999999999995, above right] (A) at (1.4000000000000001, -3.0999999999999996) {};
\end{tikzpicture}
\end{document}
share|improve this question
1  
Does it have to be a node? If not you can do something like \filldraw [draw=blue,fill=red] (0,0) rectangle (3,4);? –  Torbjørn T. Feb 17 at 22:00
    
@TorbjørnT.: That's fine, but then how do I place another node using the anchor "above" or "below"? –  Neil G Feb 17 at 22:01
    
Well, that would be a reason for having it as a node I guess. You would have to use explicit coordinates (or a coordinate calculated from the corners) instead of referring to the node name. –  Torbjørn T. Feb 17 at 22:04
add comment

marked as duplicate by Ignasi, Claudio Fiandrino, ChrisS, T. Verron, Peter Jansson Feb 18 at 10:04

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

up vote 7 down vote accepted

You can use the fit library:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fit}

\begin{document}

\begin{tikzpicture}[
myrect/.style={
  rectangle,
  draw,
  inner sep=0pt,
  fit=#1}
]
\coordinate (A) at (2,3);
\coordinate (B) at (-3,4);
\coordinate (C) at (0,2);
\coordinate (D) at (-5,6);
\coordinate (E) at (5,-2);
\node[myrect={(A) (B)}] {}; 
\node[myrect={(C) (D)},draw=cyan,rounded corners] {}; 
\node[myrect={(A) (E)},draw=cyan,fill=orange,line width=2pt] {}; 
\foreach \Coord in {A,B,C,D,E}
  \node[circle,fill,inner sep=1.5pt,label=\Coord] at (\Coord) {};
\end{tikzpicture}

\end{document}

enter image description here

share|improve this answer
    
Very clever! Thank you. –  Neil G Feb 17 at 22:07
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.