2

I am trying to draw n open squares with numbers inside. The objective to make notes on combinatorics with n balls and n boxes types of problems. I could draw circles using help from this site. I used the following for circles:

\documentclass{article}
\usepackage{tikz}
\newcommand*\circled[1]{\tikz[baseline=(char.base)]{
        \node[shape=circle,draw,inner sep=8pt] (char) {#1};}}
\begin{document}
\circled{1} \circled{2} \circled{3} $\ldots$ \circled{k}
\end{document}

I have been looking for something similar for squares. Any help?

EDIT: I tried egreg's comments and put shape=rectangle. But how do I draw open boxes?

naphaneal
  • 2,614

2 Answers2

4

You can declare shapes by simply writing the shape name, shape= is not necessary with basic shapes. Also, rectangle is the default shape, so you don't need to declare it either.

We draw the nodes without any border, then apply a path that goes around the node. I've added minimum size=1cm to make them squares.

Output

enter image description here

Code

\documentclass{article}
\usepackage{tikz}
\newcommand*\rect[1]{
    \tikz[baseline=(char.base)]{
        \node[inner sep=8pt, minimum size=1cm] (char) {#1};
        \draw (char.north west) -- (char.south west) -| (char.north east);
        }
}
\begin{document}
\rect{1} \rect{2} \rect{3} $\ldots$ \rect{k}
\end{document}

Automated version

This gives the same output as the previous code (maybe the spacing is slightly differente), but it takes only one argument: a list of elements that it loops to create the open boxes. An exception as been set for $\ldots$ so no path is created there.

\documentclass{article}
\usepackage{tikz}
\newcommand*\rect[1]{
    \tikz[baseline=(char.base)]{
    \foreach \x [count=\xx] in {#1}{
        \def\mdot{$\ldots$}
        \node[inner sep=8pt, minimum size=1cm] (char) at (\xx*1.5, 0) {\x};
        \ifx\x\mdot
        \else
            \draw (char.north west) -- (char.south west) -| (char.north east);
        \fi
        }
    }
}
\begin{document}
\rect{1, 2, 3, $\ldots$, k}
\end{document}
Alenanno
  • 37,338
3

A PSTricks solution:

\documentclass{article}

\usepackage{pstricks}
\usepackage{multido}
\usepackage{xfp}

\def\numberBox(#1,#2)#3{%
  \rput(#1,#2){%
    \psline(\fpeval{-0.5*\width},\height)(\fpeval{-0.5*\width},0)%
           (\fpeval{0.5*\width},0)(\fpeval{0.5*\width},\height)}
  \rput(#1,\fpeval{#2+0.5*\height}){#3}}

% parameters
\def\boxes{3}       % number of boxes before the \cdots
\def\width{1}       % width of each box
\def\height{\width} % height of each box
\def\spacing{0.3}   % distance between boxes

\begin{document}

\begin{pspicture}(\fpeval{(\boxes+2)*\width+\boxes*\spacing},\height)
  \multido{\r = \fpeval{0.5*\width}+\fpeval{\width+\spacing}, \i = 1+1}{\boxes}%
    {\numberBox(\r,0){$\i$}}
  \rput(\fpeval{(\boxes+0.5)*\width+(\boxes-0.5)*\spacing},\fpeval{0.5*\height})%
    {$\cdots$}
  \rput(\fpeval{(\boxes+1.5)*\width+\boxes*\spacing},0)%
    {\numberBox(0,0){$k$}}
\end{pspicture}

\end{document}

output

All you have to do is change the values of the parameters and the drawing will by adjusted accordingly.