\Colorbox creates a box of color, right? Simple question: How can I control the size and the positioning of \colorbox (if possible in cm) ?

Additionally: can even the shape of colorbox be adjusted?

  • \colorbox is meant to put a box around something. That something can be blank, and therefore allow you to draw coloured boxes. However, if you're interested in arbitrary shapes, you're better off going with a graphics package.
    – Werner
    Commented Oct 21, 2020 at 15:00
  • Considering using tcolorbox
    – Alan Xiang
    Commented Oct 21, 2020 at 15:59
  • what kind of graphic-packages actually? All I want to do is to create a rectangle of a desired color and move and squish it.
    – Leon
    Commented Oct 21, 2020 at 17:58

1 Answer 1


The command \colorbox simply draw a background rectangle with the width and height of what is inside plus a margin around that is equal to \fboxsep. You can control the size and shape of the box setting \fboxsep and/or including anything taking some vertical and/or horizontal size, as text, another boxes, minipages, tabulars, etc.

The position can be controlled in the same way that you control the position any character. If you can move a "A" in some form, you can do the same with a color box of the same size. That is, there are nearly infinite ways to do it.


\colorbox{red!40}{\rule[-4em]{0pt}{4em} l} 
\colorbox{red!40}{\rule[1em]{0pt}{2em} o} 
\colorbox{blue!40}{\strut x} 
  • I guess this answers my question fairly well. But isn't there a more simple way? For instance I can control easily {textblock}{Width}(x,y). The Reason being is that I actually want to draw a frame around my blocks just by adding a rectangle behind it.
    – Leon
    Commented Oct 21, 2020 at 19:47
  • @NessLange Well, LaTeX is a macro language, i.e., it lets you define your own commands to simplify code. Or you can use an already made macro provided for some packages. But the first is know what you want to do exactly, that still is not clear to me. To provide concrete solutions we must see a concrete scenario (a document as minimal as possible, but compilable, not a chunk of code) and a clear explanation of how you want to modify it.
    – Fran
    Commented Oct 21, 2020 at 20:42
  • I found that something like \begin{textblock}{0}(1.5,2) \begin{tikzpicture} \filldraw[draw=black,fill=orange] (10.5,6) rectangle (0,0); \filldraw[draw=black,fill=blue] (10.5,6) rectangle (9.75,0); \end{tikzpicture} \end{textblock} is completely sufficient
    – Leon
    Commented Oct 22, 2020 at 9:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .