14

I want to make a rectangular 26x26 table where in the first row contains the letters A, B, ..., Z, the second row B, C, ..., Z, A, the third row C, D, ..., Z, A, B, etc.

One (not efficient) way is the following:

\begin{tikzpicture}
  \draw[step=.5cm,gray,thick]
  (-6,-6) grid (6,6);

  \draw (-5.75,5.75) node{A} (-5.25,5.75) node{B} (-4.75,5.75) node{C}  (-4.25,5.75) node{D}  (-3.75,5.75) node{E}  etc; % this is the first row

  \draw (-5.75,5.25) node{B} (-5.25,5.25) node{C} (-4.75,5.25) node{D}  (-4.25,5.25) node{E}  (-3.75,5.25) node{F}  etc; % this is the second row

  % and so on for the rest rows  
\end{tikzpicture}

Any ideas how to to make this happen more efficiently?

Also what if I want to use the Greek alphabet A, B, Γ, ..., Ω instead of the English alphabet.

4 Answers 4

20

Another method:

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
\foreach \i in {0,...,25} {
  \foreach \j in {0,...,25} {
    \edef\k{\ifnum\numexpr\i+\j\relax>25
        \the\numexpr\i+\j-26\relax
      \else
        \the\numexpr\i+\j\relax
      \fi}
  \node[draw,minimum size=0.5cm,inner sep=0pt]
    at (\i*0.5,-\j*0.5) {\strut\symbol{\numexpr`A+\k\relax}};
  }
  \node at (-0.5,-\i*0.5) {\strut\i};
  \node at (\i*0.5,0.5)   {\strut\i};
}
\end{tikzpicture}
\end{document}

It works as in normal programming language (like C), although integer calculation in TeX is a bit more difficult.

enter image description here

3
  • Nice one! You can also use \pgfmathparse{\i + \j>25 ? int(mod(\i+\j,26)) : int(\i+\j)}\edef\k{\pgfmathresult} for ifthenelse part.... Sorry, I defined the for each loops as \foreach \x [count=\i] in {A,...,Z} { \foreach \y [count=\j] in {A,...,Z} {
    – percusse
    May 20, 2012 at 15:36
  • 2
    @percusse: Presumably just \pgfmathparse{int(mod(\i+\j,26))} would work without the conditional? May 20, 2012 at 16:30
  • @BenAlpert Haha, I overanalyzed that apparently. You are right.
    – percusse
    May 20, 2012 at 16:38
13

I took the example of the manual:

\documentclass[]{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=0.6,transform shape]
\foreach \x [count=\xi] in {A,...,Z}
\foreach \y [count=\yi] in {\x,...,Z}
\node [draw, top color=white, bottom color=blue!50, minimum size=0.666cm]
at (\xi,-\yi) {$\mathstrut\y$};
\end{tikzpicture}
\end{document}

enter image description here

Egreg pointed out that you need also a lower part:

\documentclass[border=10]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=0.6,transform shape]
\foreach \x [count=\xi] in {A,...,Z}
 {%
   \foreach \y [count=\yi] in {\x,...,Z}
   {
      \node [draw, top color=white, bottom color=blue!50, minimum size=0.666cm]
         at (\xi,-\yi) {$\mathstrut\y$};
     \xdef\Ax{\yi}
   }
  \pgfmathsetmacro\Ax{\Ax+1}
   \foreach \y [count=\yi from \Ax ] in {A,...,\x}
     {
      \ifnum\yi<27
         \node [draw, top color=white, bottom color=blue!50, minimum size=0.666cm]
         at (\xi,-\yi) {$\mathstrut\y$};
       \fi
    }
 }
\end{tikzpicture}
\end{document}

enter image description here


As required he a solution with greek letters:

\newcommand\greekletter[1]{%
\ifcase #1 \relax
\or
\alpha\or
\beta\or
\gamma\or
\delta\or
\epsilon\or
\zeta\or
\eta\or
\theta\or
\iota\or
\kappa\or
\lambda\or
\mu\or
\nu\or
\xi\or
\mathrm{o}\or
\pi\or
\rho\or
\sigma\or
\tau\or
\upsilon\or
\phi\or
\chi\or
\psi\or
\omega
\else A
\fi
}

Here the complete MWE:

\documentclass[border=10]{standalone}
\usepackage{tikz}
\newcommand\greekletter[1]{%
\ifcase #1 \relax
\or 
\alpha\or
\beta\or
\gamma\or
\delta\or
\epsilon\or
\zeta\or
\eta\or
\theta\or
\iota\or
\kappa\or
\lambda\or
\mu\or
\nu\or
\xi\or
\mathrm{o}\or
\pi\or
\rho\or
\sigma\or
\tau\or
\upsilon\or
\phi\or
\chi\or
\psi\or
\omega
\else A
\fi
}
\begin{document}
\begin{tikzpicture}[scale=0.6,transform shape]
\foreach \xx [count=\xxi] in {1,...,24}
 {%
   \foreach \yy [count=\yyi] in {\xx,...,24}
   {
      \node [draw, top color=white, bottom color=blue!50, minimum size=0.666cm]
         at (\xxi,-\yyi) {$\mathstrut\expandafter\greekletter{\yy}$};
     \xdef\Ax{\yyi}
   }
  \pgfmathsetmacro\Ax{\Ax+1}
   \foreach \yy [count=\yyi from \Ax ] in {1,...,\xx}
     {
      \ifnum\yyi<27
         \node [draw, top color=white, bottom color=blue!50, minimum size=0.666cm]
         at (\xxi,-\yyi) {$\mathstrut\expandafter\greekletter{\yy}$};
       \fi
    }
 }
\end{tikzpicture}
\end{document}
6
  • Then you have to complete the lower half
    – egreg
    May 20, 2012 at 12:56
  • @egreg: What should be set in the lower part? May 20, 2012 at 13:01
  • @egreg: This way? May 20, 2012 at 13:12
  • @egreg: now ;-) May 20, 2012 at 13:21
  • Thank you for your answers. One more question. Ok with the english alphabet, what if I want to use the Greek Alphabet A,B,\Gammma,...,\Omega. Which are the necessary changes?
    – C.D.
    May 20, 2012 at 16:59
6

Here is a variant of Leo's code that can deal with arbitrary alphabets. I'm using fontspec to be able to easily use Greek letters.

% Compile with xelatex or lualatex.
\documentclass{article}
\usepackage{tikz}
\usepackage{fontspec}
\setmainfont{Linux Libertine O}

\def\alphabet{Α,Β,Γ,Δ,Ε,Ζ,Η,Θ,Ι,Κ,Λ,Μ,Ν,Ξ,Ο,Π,Ρ,Σ,Τ,Υ,Φ,Χ,Ψ,Ω}
\def\alphabetlength{24}

\begin{document}
\begin{tikzpicture}
\foreach \row in {1,...,\alphabetlength} {
  \foreach[count=\x] \letter in \alphabet {
    \pgfmathsetmacro\column{Mod(-\row+\x,\alphabetlength)}
    \node[draw,minimum size=0.5cm,inner sep=0pt]
        at (\column*0.5,-\row*0.5) {\strut\letter};
    }
    \node at (-0.5,-\row*0.5) {\strut\row};
    \node at ({\row*0.5-0.5},0)   {\strut\row};
}
\end{tikzpicture}
\end{document}

result

5
\documentclass[border=10]{standalone}
\usepackage{tikz}
\begin{document} 
\footnotesize
\begin{tikzpicture}[every node/.style={draw, circle, minimum size=16pt}]
\foreach \x [count=\xi]  in {A,...,Z}
 {\foreach \y [count=\yi] in {\x,...,Z}
   {\node at (6*\xi ex,-6*\yi ex) {\y};}%
  \ifnum\yi>1 \foreach \z [count=\zi from \yi ] in {A,...,\x}
      {\node at (6*\xi ex +6 ex,-6*\zi ex) {\z};}\fi}
\end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

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