# How to draw a square of 1cm in LaTeX filled with color?

How to draw a square of 1cm in LaTeX filled with colour? I found mdframed and framebox does this, but there need to write text inside it. I just need to draw a box.

• \usepackage{tikz} in the preamble, and \tikz{\path[draw=red,fill=blue] (0,0) rectangle (1cm,1cm);} where you need the rectangle. – T. Verron Apr 5 '13 at 9:08

Use the simple \rule command:

\documentclass{article}
\usepackage{xcolor}
\newcommand\crule[3][black]{\textcolor{#1}{\rule{#2}{#3}}}
\begin{document}

\crule{1cm}{1cm} \crule[blue]{1cm}{1cm} \crule[red!50!white!100]{1cm}{1cm}

\end{document}


• Gotta love \rule. Very versatile considering its original purpose. – Sean Allred Apr 5 '13 at 14:55
• I think overprinting two colored rules would be general enough for draw/fill distinction :) – percusse Mar 15 '14 at 11:30
• How to make this into several lines? maybe in n x m grid? tabular and newline doesn't work :-( – andrybak Nov 1 '15 at 16:55
• use a tabularx and define the left rule with !{\vrule width 1cm} – user2478 Nov 1 '15 at 21:06
• How to make a vertical line with the boxes so that no space between the boxes? – hhh Jan 13 '20 at 11:41

No-one has a box that is drawn and filled yet*, which is possibly what was meant in the original question. And for what it's worth, here is a completely different way of getting a 10mm box - use fbox around a null box. 8-)

\usepackage{xcolor}
...
\fboxsep=4mm \fboxrule=1mm
\fcolorbox{black}{blue!40!white}{\null}


Edit: * my apologies, there are solutions above with drawn/filled boxes. No pictures of such though.

• How can I fix the box? The starting letter is downward the box. – SORIF HOSSAIN Mar 24 '19 at 13:52

You can use tikz and \newcommand to have a small command for box.

\documentclass{article}

\usepackage{lipsum}
\usepackage{tikz}

\newcommand{\mycbox}[1]{\tikz{\path[draw=#1,fill=#1] (0,0) rectangle (1cm,1cm);}}

\begin{document}
\lipsum[1-2]
\mycbox{blue}
\mycbox{red}
\end{document}


Result:

• I'm wondering why you load pgfplots and set the option compact=1.7 while just loading tikz would be sufficient for your purpose... :) – Claudio Fiandrino Apr 5 '13 at 9:17
• @ClaudioFiandrino: Yes, my fault. I just take a scratch TeX file with pgfplots, instead of tikz. – m0nhawk Apr 5 '13 at 9:48
• Overkill, I think, when compared with \rule – Brent.Longborough Apr 5 '13 at 9:56
• @Brent.Longborough Although perhaps a bit overkill, I would always recommend Tikz over \rule. To me its easier to understand and it gives better, more controllable results. – Mythio Apr 5 '13 at 11:02
• @Herbert: because it has a lot more options for styling, etc as far as I'm aware. Although I admit my experience with \rule is limited, so it might have options too. – Mythio Apr 6 '13 at 14:56

With PSTricks. Compile it with pdflatex -shell-escape main.

% the filename is main.tex
\documentclass{article}
\usepackage{filecontents}
\usepackage{graphicx}

\begin{filecontents*}{dummy.tex}
\documentclass[pstricks]{standalone}
\begin{document}
\begin{pspicture}(\Side,\Side)
\psframe*[linecolor=\Color](\Side,\Side)
%\psframe[fillstyle=solid,fillcolor=\Color,linestyle=none,dimen=middle](\Side,\Side)
%\psframe[fillstyle=solid,fillcolor=\Color,linestyle=none,linewidth=0](\Side,\Side)
\end{pspicture}
\end{document}
\end{filecontents*}

\def\Square[#1,#2]{%
\immediate\write18{\unexpanded{latex "\def\Side{#1}\def\Color{#2}\input{dummy}"}}%
\immediate\write18{dvips dummy}%
\immediate\write18{ps2pdf dummy.ps dummy-#2.pdf}%
\includegraphics{dummy-#2}%
}

\begin{document}
This is a red square \Square[12pt,red] and this is a blue one \Square[15pt,blue].
\end{document}


## Edit 1:

Based on HV's comment below: The code above apparently can be simplified as follows. Again, compile it with pdflatex -shell-escape main.

% main.tex
\documentclass{article}
\usepackage[pdf]{pstricks}

\def\Square[#1,#2]{%
\begin{postscript}
\psframe*[linecolor=#2](#1,#1)
\end{postscript}}

\begin{document}
This is a red square \Square[12pt,red] and this is a blue one \Square[15pt,blue].
\end{document}


## Edit 2:

The code in edit 1 does not produce a tight output for the squares. Therefore, we have to explicitly pass --hires to pdfcrop as follows.

\documentclass[preview,varwidth,border=12pt]{article}
\usepackage{pstricks}
\usepackage[pdfcrop={--hires}]{auto-pst-pdf}

\def\Square[#1,#2]{%
\begin{postscript}
\psframe*[linecolor=#2](#1,#1)
\end{postscript}}

\begin{document}
This is a red square \Square[12pt,red] and this is a blue one \Square[15pt,blue].
\end{document}

• \usepackage[pdf]{pstricks} \def\Square[#1,#2]{\begin{postscript}\psframe*[linecolor=#2](#1,#1)\end{postscript}} will do the same! – user2478 Apr 5 '13 at 11:12
• @Herbert Exactly what do you want the document to look like, and how should it be compiled? – Svend Tveskæg Apr 5 '13 at 12:49
• @SvendMortensen: It also needs the -shell-escapeoption with pdflatex. That's all, together with the \begin{document} ... – user2478 Apr 5 '13 at 13:05
• @Herbert The following will not compile: \documentclass{article} \usepackage[pdf]{pstricks} \usepackage{graphicx} \def\Square[#1,#2]{ \begin{postscript} \psframe*[linecolor=#2](#1,#1) \end{postscri‌​pt} } \begin{document} This is a red square \Square[12pt,red] and this is a blue one \Square[15pt,blue]. \end{document} – Svend Tveskæg Apr 5 '13 at 13:26
• @SvendMortensen: my files: comedy.dante.de/~herbert/square.tex and comedy.dante.de/~herbert/square.pdf – user2478 Apr 5 '13 at 13:37

All solutions above expand to

\hbox{\pdfliteral{0 0 1 rg}\vrule height1cm width1cm depth0cm\pdfliteral{0 g}}


at pdfTeX level. The only difference (when TikZ is used) is that you need to read 26560 lines of code from 56 external files. What does TikZ? It implements programming language for picture programming using programming language for typesetting. Sometimes seems to be more functional to use the original language for typesetting directly. I mean that this is this case.

• While I agree that calling TikZ for the sole purpose of drawing a square is overkill, I firmly disagree with this false simplicity: the \rule solution doesn't expand to those low level commands when XeTeX is used or a route such as latex+dvipdfmx or latex+dvips+ps2pdf. – egreg Jul 28 '14 at 7:53
• If a person often uses TikZ then the cognitive resource cost of avoiding it may well be much greater than the cpu's resource cost of using it. – Jim Hefferon Jul 29 '14 at 21:19