Making a lattice with numbers on vertices
Here is a way to do this:
Code:
\documentclass{article}
\usepackage{xstring}
\usepackage{tikz}
\usepackage{collcell}
\newcommand{\MakeBox}[1]{\makebox[2.0em][c]{#1}}%
\newcommand*{\MyBox}[1]{%
\phantom{\MakeBox{#1}}%
\IfStrEq{#1}{}{}{%
\begin{tikzpicture}[overlay, draw=red, line width=1.0pt, text=blue]
\node [draw=none, inner sep=2pt] (Node) {\MakeBox{#1}};
\draw (Node.north) -- ([yshift=2.0ex]Node.north);
\draw (Node.south) -- ([yshift=-2.0ex]Node.south);
\draw (Node.west) -- ([xshift=-1.0em]Node.west);
\draw (Node.east) -- ([xshift=1.0em]Node.east);
\end{tikzpicture}%
}%
}
\newcolumntype{C}{>{\collectcell\MyBox}c<{\endcollectcell}}
\begin{document}
$\begin{array}{*{20}{C}}
& 1 & 1 & 1 & 1 & 1 & 1 & 1 & \\[2.0ex]
& 7 & 6 & 5 & 4 & 3 & 2 & 1 & \\[2.0ex]
& 28 & 21 & 15 & 10 & 6 & 3 & 1 & \\[2.0ex]
& 84 & 56 & 35 & 20 & 10 & 4 & 1 & \\ [2.0ex]
\end{array}$
\end{document}
If you want to suppress the outer lines, then you need to indicate the last row with a call to \ThisIsLastRow
. The detection of the first column is handled by using the F
column type.
Further Enhancements
- Since I am using a
\makebox[2.0em][c]{#1}
to place the text to keep it centered, then the last column which only has single width characters looks wrong. One way to fix that would be to define two column types: one for double digits, and one for single digits. Alternatively, one could draw the lines through the text and then overlay the node text over it which will then remove the lines just around the numbers.
Code:
\documentclass{article}
\usepackage{xstring}
\usepackage{etoolbox}
\usepackage{tikz}
\usepackage{collcell}
\newtoggle{IsFirstColumn}\togglefalse{IsFirstColumn}%%
\newtoggle{IsLastRow}\togglefalse{IsLastRow}%
\newcommand{\ThisIsLastRow}{\global\toggletrue{IsLastRow}}%
\newcommand{\MakeBox}[1]{\makebox[2.0em][c]{#1}}%
\newcommand*{\MyBox}[1]{%
\phantom{\MakeBox{#1}}%
\IfStrEq{#1}{}{}{%
\begin{tikzpicture}[overlay, draw=red, line width=1.0pt, text=blue]
\node [draw=none, inner sep=2pt] (Node) {\MakeBox{#1}};
\iftoggle{IsLastRow}{}{%
\draw (Node.south) -- ([yshift=-2.0ex]Node.south);
}%
\iftoggle{IsFirstColumn}{}{%
\draw (Node.west) -- ([xshift=-1.0em]Node.west);
}%
\end{tikzpicture}%
}%
}
\newcommand*{\MyBoxFirstColumn}[1]{%
\global\toggletrue{IsFirstColumn}%
\MyBox{#1}%
\global\togglefalse{IsFirstColumn}%
}%
\newcolumntype{C}{>{\collectcell\MyBox}c<{\endcollectcell}}
\newcolumntype{F}{>{\collectcell\MyBoxFirstColumn}c<{\endcollectcell}}
\begin{document}
$\begin{array}{F*{20}{C}}
1 & 1 & 1 & 1 & 1 & 1 & 1 \\[2.0ex]
7 & 6 & 5 & 4 & 3 & 2 & 1 \\[2.0ex]
28 & 21 & 15 & 10 & 6 & 3 & 1 \\[2.0ex] \ThisIsLastRow
84 & 56 & 35 & 20 & 10 & 4 & 1 \\
\end{array}$
\end{document}
Given the output you desire, I think I would use matrix of nodes
Complete code
% arara: pdflatex
% !arara: indent: {overwrite: true}
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}[thick]
\matrix (mylattice)[
matrix of nodes,
row sep=.5cm,
column sep=.5cm,
nodes in empty cells,
execute at empty cell={\node[draw=none]{\phantom{X}};},
]{
& & & & & & & &\\
& 1 & 1 & 1 & 1 & 1 & 1 & 1 & \\
& 7 & 6 & 5 & 4 & 3 & 2 & 1 & \\
& 28 & 21 & 15 & 10 & 6 & 3 & 1 & \\
& 84 & 56 & 35 & 20 & 10 & 4 & 1 & \\
& & & & & & & & \\
};
\foreach \i [evaluate=\i as \y using int(\i+1)] in {1,...,5}
{
\foreach \j [evaluate=\j as \x using int(\j+1)]in {1,...,8}
{
\ifnum\j>1
\draw[red] (mylattice-\i-\j)--(mylattice-\y-\j);
\fi
\ifnum\i>1
\draw[blue] (mylattice-\i-\j)--(mylattice-\i-\x);
\fi
}
}
\end{tikzpicture}
\end{document}
Following the comments, if you don't want the outside pieces then you can use the following slightly modified code.
% arara: pdflatex
% !arara: indent: {overwrite: true}
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}[thick]
\matrix (mylattice)[
matrix of nodes,
row sep=.5cm,
column sep=.5cm,
nodes in empty cells,
execute at empty cell={\node[draw=none]{\phantom{X}};},
]{
1 & 1 & 1 & 1 & 1 & 1 & 1 \\
7 & 6 & 5 & 4 & 3 & 2 & 1 \\
28 & 21 & 15 & 10 & 6 & 3 & 1 \\
84 & 56 & 35 & 20 & 10 & 4 & 1 \\
};
\foreach \i [evaluate=\i as \y using int(\i+1)] in {1,...,4}
{
\foreach \j [evaluate=\j as \x using int(\j+1)]in {1,...,7}
{
\ifnum\i<4
\draw[red] (mylattice-\i-\j)--(mylattice-\y-\j);
\fi
\ifnum\j<7
\draw[blue] (mylattice-\i-\j)--(mylattice-\i-\x);
\fi
}
}
\end{tikzpicture}
\end{document}