TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How might one define a command \rectangle, invoked via e.g.


that produces a black-outline rectangle of the specified dimensions?

share|improve this question
I needed the rectangle for a MWE. I've just seen the following easy way to make a filled rectangle, which is fine for MWE purposes: \rule{<width>}{<height>} – Mohan Dec 5 '12 at 16:49
up vote 18 down vote accepted

A way without any package is the following:

\newcommand{\rectangle}[2]{{% #1 = width, #2 = height

With \fboxsep=-\fboxrule (that in proper LaTeX syntax would be \setlength{\fboxsep}{\fboxrule}) we ask LaTeX to draw the frame "inside the box". The box we draw is empty, but its width and height are set to the specified arguments. So we build this empty box and then we frame it.

Instead of \usebox{0} I prefer to employ the primitive command \box0, which uses the box and destroys it. Since all happens inside a group, the value of \fboxsep will be restored to what it was before and we hide our tracks also for the (scratch) box register 0.

If one wants to specify also a depth, then the extension is quite easy:


so that \rectangle{10pt}{20pt} will build a rectangle 10pt wide and 20pt high, while


will build the same, but placed slightly below the baseline.

share|improve this answer
\setlength{\wd0 }{#1} and \setlength{\ht0 }{#2} would also support package calc with its expressions in the argument of \rectangle. (Note: the space after 0 is needed unless \setlength is fixed by package fixltx2e.) – Heiko Oberdiek Dec 5 '12 at 15:01

An alternative answer using TikZ is

\newcommand{\rectangle}[2]{\tikz \draw (0,0) rectangle (#1,#2);}

(You need to add \usepackage{tikz} in your preamble)

If you want to have a rectangle filled with black, replace \draw by \fill. You can also choose the color: for instance

\newcommand{\rectangleplus}[3]{\tikz \draw[#3] (0,0) rectangle (#1,#2);}

and many other things.

share|improve this answer

My attempt at using picture environment,


    \setlength{\unitlength}{1pt}    % Reset unit length to 1 pt



share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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