37

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.

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

5 Answers 5

67

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}

enter image description here

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

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}

enter image description here

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

3
  • 1
    How can I fix the box? The starting letter is downward the box. Commented Mar 24, 2019 at 13:52
  • How to align the box with the text ? Commented Jul 10, 2021 at 3:55
  • from memory, \fcolorbox aligns with the baseline of the internal box, which in this case is a 0x0 box. You can create a larger internal box with a \phantom so that it gets an appropriate baseline. Maybe something like \fboxsep=2mm \fboxrule=1mm \fcolorbox{black}{blue!40!white}{\phantom{\vrule width 4mm height 4mm}}. Or just use \raisebox. Commented Aug 6, 2021 at 4:30
12

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:

enter image description here

8
  • 1
    I'm wondering why you load pgfplots and set the option compact=1.7 while just loading tikz would be sufficient for your purpose... :) Commented Apr 5, 2013 at 9:17
  • @ClaudioFiandrino: Yes, my fault. I just take a scratch TeX file with pgfplots, instead of tikz.
    – m0nhawk
    Commented Apr 5, 2013 at 9:48
  • 8
    Overkill, I think, when compared with \rule Commented Apr 5, 2013 at 9:56
  • 2
    @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
    Commented Apr 5, 2013 at 11:02
  • 2
    @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
    Commented Apr 6, 2013 at 14:56
8

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

enter image description here

% 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}
5
  • 2
    \usepackage[pdf]{pstricks} \def\Square[#1,#2]{\begin{postscript}\psframe*[linecolor=#2](#1,#1)\end{postscript}} will do the same!
    – user2478
    Commented Apr 5, 2013 at 11:12
  • @Herbert Exactly what do you want the document to look like, and how should it be compiled? Commented Apr 5, 2013 at 12:49
  • @SvendMortensen: It also needs the -shell-escapeoption with pdflatex. That's all, together with the \begin{document} ...
    – user2478
    Commented Apr 5, 2013 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} Commented Apr 5, 2013 at 13:26
  • @SvendMortensen: my files: comedy.dante.de/~herbert/square.tex and comedy.dante.de/~herbert/square.pdf
    – user2478
    Commented Apr 5, 2013 at 13:37
7

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.

2
  • 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
    Commented Jul 28, 2014 at 7:53
  • 2
    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. Commented Jul 29, 2014 at 21:19

You must log in to answer this question.

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