# tikzfigure.core.rectangle

*module*

[View source](https://github.com/max-models/tikzfigure/blob/devel/src/tikzfigure/core/rectangle.py#L1-L1)

## tikzfigure.core.rectangle.Rectangle

*class*

```python
class Rectangle(TikzObject)
```

Bases: `tikzfigure.core.base.TikzObject`

A TikZ rectangle drawn with \draw rectangle or \filldraw rectangle.

**Attributes**

- `corner1` (`TikzCoordinate`) — First corner coordinate.
- `corner2` (`TikzCoordinate`) — Second corner coordinate (opposite corner).
- `tikz_command` (`str`) — The TikZ drawing command ("draw" or "filldraw").

[View source](https://github.com/max-models/tikzfigure/blob/devel/src/tikzfigure/core/rectangle.py#L9-L138)

### tikzfigure.core.rectangle.Rectangle.corner1

*property*

```python
corner1: TikzCoordinate
```

First corner coordinate of the rectangle.

[View source](https://github.com/max-models/tikzfigure/blob/devel/src/tikzfigure/core/rectangle.py#L61-L63)

### tikzfigure.core.rectangle.Rectangle.corner2

*property*

```python
corner2: TikzCoordinate
```

Second (opposite) corner coordinate of the rectangle.

[View source](https://github.com/max-models/tikzfigure/blob/devel/src/tikzfigure/core/rectangle.py#L66-L68)

### tikzfigure.core.rectangle.Rectangle.tikz_command

*property*

```python
tikz_command: str
```

The TikZ drawing command ("draw" or "filldraw").

[View source](https://github.com/max-models/tikzfigure/blob/devel/src/tikzfigure/core/rectangle.py#L71-L73)

### tikzfigure.core.rectangle.Rectangle.comment

*property*

```python
comment: str | None
```

Inherited from: `tikzfigure.core.base.TikzObject`

Comment prepended to the generated TikZ code, or ``None``.

[View source](https://github.com/max-models/tikzfigure/blob/devel/src/tikzfigure/core/base.py#L54-L56)

### tikzfigure.core.rectangle.Rectangle.kwargs

*property*

```python
kwargs: dict
```

Inherited from: `tikzfigure.core.base.TikzObject`

Keyword-style TikZ options as a plain dict.

[View source](https://github.com/max-models/tikzfigure/blob/devel/src/tikzfigure/core/base.py#L69-L71)

### tikzfigure.core.rectangle.Rectangle.label

*property*

```python
label: str | None
```

Inherited from: `tikzfigure.core.base.TikzObject`

Unique identifier for this object within the figure.

[View source](https://github.com/max-models/tikzfigure/blob/devel/src/tikzfigure/core/base.py#L49-L51)

### tikzfigure.core.rectangle.Rectangle.layer

*property*

```python
layer: int | None
```

Inherited from: `tikzfigure.core.base.TikzObject`

Layer index this object belongs to.

[View source](https://github.com/max-models/tikzfigure/blob/devel/src/tikzfigure/core/base.py#L59-L61)

### tikzfigure.core.rectangle.Rectangle.options

*property*

```python
options: list[OptionValue]
```

Inherited from: `tikzfigure.core.base.TikzObject`

Flag-style TikZ options without values.

[View source](https://github.com/max-models/tikzfigure/blob/devel/src/tikzfigure/core/base.py#L64-L66)

### tikzfigure.core.rectangle.Rectangle.from_dict

*method* · *classmethod*

```python
def from_dict(d: dict[str, Any]) -> Rectangle
```

Reconstruct a Rectangle from a dictionary.

**Parameters**

- `d` (`dict[str, Any]`) — Dictionary as produced by to_dict().

**Returns**

- (`Rectangle`) — A new Rectangle instance.

[View source](https://github.com/max-models/tikzfigure/blob/devel/src/tikzfigure/core/rectangle.py#L114-L138)

### tikzfigure.core.rectangle.Rectangle.to_dict

*method*

```python
def to_dict() -> dict[str, Any]
```

Serialize this rectangle to a plain dictionary.

**Returns**

- (`dict[str, Any]`) — A dictionary with type, corners, tikz_command, and all base-class keys.

[View source](https://github.com/max-models/tikzfigure/blob/devel/src/tikzfigure/core/rectangle.py#L94-L112)

### tikzfigure.core.rectangle.Rectangle.to_tikz

*method*

```python
def to_tikz(output_unit: str | None = None) -> str
```

Generate the TikZ rectangle command for this rectangle.

**Returns**

- (`str`) — A \draw or \filldraw command string ending with a newline,
- (`str`) — optionally preceded by a comment line.

[View source](https://github.com/max-models/tikzfigure/blob/devel/src/tikzfigure/core/rectangle.py#L75-L92)

### tikzfigure.core.rectangle.Rectangle.add_comment

*method*

```python
def add_comment(string_in: str) -> str
```

Inherited from: `tikzfigure.core.base.TikzObject`

Prepend a TikZ comment line to a string if a comment is set.

**Parameters**

- `string_in` (`str`) — The TikZ code string to annotate.

**Returns**

- (`str`) — The original string prefixed with ``% <comment>\n`` when a
- (`str`) — comment is set, otherwise the string unchanged.

[View source](https://github.com/max-models/tikzfigure/blob/devel/src/tikzfigure/core/base.py#L107-L119)

### tikzfigure.core.rectangle.Rectangle.copy

*method*

```python
def copy(**overrides: Any) -> TikzObject
```

Inherited from: `tikzfigure.core.base.TikzObject`

Return a copy of this object with optional constructor-style overrides.

[View source](https://github.com/max-models/tikzfigure/blob/devel/src/tikzfigure/core/base.py#L216-L222)

### tikzfigure.core.rectangle.Rectangle.tikz_options

*method*

```python
def tikz_options(output_unit: str | None = None) -> str
```

Inherited from: `tikzfigure.core.base.TikzObject`

Render all options as a single TikZ option string.

Combines flag-style options and keyword options into the format
expected inside TikZ square brackets, e.g.
``"thick, color=red, line width=1pt"``.

**Parameters**

- `output_unit` (`str | None`) (default: `None`) — If provided, any :class:`~tikzfigure.units.TikzDimension` values are converted to this unit before rendering.

**Returns**

- (`str`) — A comma-separated string of TikZ options.

[View source](https://github.com/max-models/tikzfigure/blob/devel/src/tikzfigure/core/base.py#L73-L105)
