I need to do diagrams like this for homework.
Presumably I could do this by putting tables inside tables, but that seems really tedious.
Is there any way to achieve such an effect with shorter, cleaner code?
I need to do diagrams like this for homework.
Presumably I could do this by putting tables inside tables, but that seems really tedious.
Is there any way to achieve such an effect with shorter, cleaner code?
You could use a matrix with TikZ:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\tikzset{
mymatrix/.style={
matrix of nodes,
column sep=2mm,
row sep={-\pgflinewidth},
nodes={
draw,
fill=blue!20,
text width=1.25ex,
text height=1.5ex,
text depth=0pt,
align=center,
anchor=center,
},
row 1/.style={
row sep=2mm,
nodes={
draw=none,
fill=none,
}
}
},
}
\begin{document}
\begin{tikzpicture}
\matrix[mymatrix] {
7 & 0 & 1 & 2 & 0 & 3 \\
7 & 7 & 7 & 2 & & 2 \\
{} & 0 & 0 & 0 & & 3 \\
{} & {} & 1 & 1 & & 1 \\
};
\end{tikzpicture}
\end{document}
If you have an assignment where you'll need to typeset arbitrary diagrams that resemble tables, then it'd be ideal to automate the process with a few macros.
Code
Below, the \createCol
macro accepts four inputs, and creates a column with blue cells. If you want a column that only has a number at the top but no blue cells, you \noBlueCol
is a macro that accepts a single input. LaTeX treats tables as though they're text (well, sort of—the tables get put into boxes and are inserted in line with rows of text), and because each table has the same vertical size, they'll all end up aligned in rows so long as you don't break paragraphs.
\documentclass[12pt]{article}
\usepackage[table,svgnames]{xcolor}
\newcommand{\createCol}[4]{\begin{tabular}{c}
#1
\\
{}
\\\hline
\multicolumn{1}{|c|}{\cellcolor{LightBlue}#2}
\\\hline
\multicolumn{1}{|c|}{\cellcolor{LightBlue}#3}
\\\hline
\multicolumn{1}{|c|}{\cellcolor{LightBlue}#4}
\\\hline
\end{tabular}
}
\newcommand{\noBlueCol}[1]{\begin{tabular}{c}
#1
\\
{}
\\
\multicolumn{1}{c}{\phantom{1}}
\\
\multicolumn{1}{c}{}
\\
\multicolumn{1}{c}{}
\\
\end{tabular}
}
\begin{document}
\sffamily
\createCol{1}{2}{3}{4}
\createCol{1}{2}{}{}
\createCol{2}{4}{6}{}
\noBlueCol{2}
\createCol{3}{4}{5}{6}
\createCol{3}{2}{1}{0}
\end{document}
Output
With square cells:
\documentclass{article}
\usepackage[table,svgnames]{xcolor}
\usepackage{array}
\newcommand{\mystrut}{%
\rule{0pt}{\dimexpr0.5\dp\strutbox+\fontcharht\font`0}%
\rule[-0.5\dp\strutbox]{0pt}{0.5\dp\strutbox}%
}
\newcommand{\stack}[4]{%
\begingroup\renewcommand{\arraystretch}{0}%
\setlength{\tabcolsep}{0pt}%
\begin{tabular}[t]{|>{\cellcolor{LightBlue}\mystrut}wc{\dimexpr\dp\strutbox+\fontcharht\font`0}|}
\multicolumn{1}{c}{#1}\\[1ex]
\hline
#2 \\ \hline
#3 \\ \hline
#4 \\ \hline
\end{tabular}%
\endgroup
}
\newcommand{\nostack}[1]{%
\makebox[\dimexpr\dp\strutbox+\fontcharht\font`0+2\arrayrulewidth]{#1}%
}
\begin{document}
\begin{center}
\stack{7}{7}{}{}\hfill
\stack{0}{7}{0}{}\hfill
\stack{1}{7}{0}{1}\hfill
\stack{2}{2}{0}{1}\hfill
\nostack{0}\hfill
\stack{3}{2}{3}{1}\hfill
\stack{0}{2}{3}{0}\hfill
\stack{4}{4}{3}{0}\hfill
\stack{2}{4}{2}{0}\hfill
\stack{3}{4}{2}{3}\hfill
\stack{0}{0}{2}{3}\hfill
\nostack{3}\hfill
\nostack{2}\hfill
\stack{1}{0}{1}{3}\hfill
\stack{2}{0}{1}{2}\hfill
\nostack{0}\hfill
\nostack{1}\hfill
\stack{7}{7}{1}{2}\hfill
\stack{0}{7}{0}{2}\hfill
\stack{1}{7}{0}{1}
\end{center}
\bigskip
\noindent
\mbox{\small
\stack{7}{7}{}{}\enspace
\stack{0}{7}{0}{}\enspace
\stack{1}{7}{0}{1}\enspace
\stack{2}{2}{0}{1}\enspace
\nostack{0}\enspace
\stack{3}{2}{3}{1}%
}
\end{document}