# How to fill a Vigenère square?

Im trying to do a Vigenère square, but I have to modify it several times, so is there any way to fill it with multido or any other method, instead of tabular and by hand.

UPDATE
All are great answers that show me how ignorant I am. I cant get compiled none because I get errors of the Undefined control sequence type. But afaik, all examples are fine. I have double checked the texlive if there is something roaming out there, but I havent found anything out of order. Sincerely I never imagined these answers.

UPDATE 2
For the aswer given by egreg it gets

! Undefined control sequence.
l.10 \NewDocumentCommand
{\vigenere}{}


So it is not recognizing the definition of the command, and gives more blah about

! Missing number, treated as zero./! Illegal parameter number in definition of \l_vigenere_body_tl.


for the Mico/Werner answer is requiring me to install Luatex, thats supposed to be installed right in livetex.

! Package luacode Error: LuaTeX is required for this package. Aborting..


The wipet answer I dont know how to use, because I think this must be done on some .sty file.

UPDATE 3 The lualatex was generated succesfully as showed in the answer. Thanks to Mico

• You don't have to modify it as it contains all the possibilities. I guess you just don't want to type it out manually, correct? – Werner Feb 17 at 23:40
• For the ConTeXt solution, you could compile here. Clean the text field, copy-paste the content below, click typeset and then download the pdf-file. As for the others, they should work with any up-to-date distribution... – Jairo A. del Rio Feb 18 at 5:24
• You mention in an Update paragraph that you cannot get any of the answers to compile. Please be more specific as to the problems you're encountering. – Mico Feb 18 at 15:10
• @Mico, Im working to post the errors – riccs_0x Feb 18 at 15:29
• LuaLaTeX is definitely a part of TeXLive. But to use LuaLaTeX rather than the default pdfLaTeX to compile the test document. you must change the item in one of the pull-down menus of your editor, to select the desired version of LaTeX. Unless I completely missed it, you haven't told us so far which editor/front end you employ. Please advise. – Mico Feb 20 at 11:31

## 6 Answers

There are so many solutions already, but I like add some more, which I used for my currently unpublished book. It's a bit tricky, but with rather simple code and easy to read and understand, I hope you like it :-)

\documentclass[border=5pt]{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[x=0.5cm, y=0.5cm]

\foreach \z [count=\zx] in {Z,...,A}{%
\foreach \b [count=\bx] in {\z,...,Z}{%
\node [anchor=base] (N\bx\zx) at (\bx,\zx) {\b};
}
}

\foreach \z [count=\zx] in {A,...,Y}{%
\foreach \b [count=\bx] in {\z,...,A}{%
\node [anchor=base] (S\bx\zx) at (27-\bx,26-\zx) {\b};
}
}

\end{tikzpicture}
\end{document}


• Its a bit tricky indeed, but at the same time this was more or less what I was thinking but never guessed the letters could be sorted like a number. Thank =) – riccs_0x Feb 20 at 16:49

Fill the table one row at a time.

\documentclass{article}
\usepackage[table]{xcolor}

\definecolor{headers}{RGB}{234,232,218}

\ExplSyntaxOn

\tl_new:N \l_vigenere_body_tl

\NewDocumentCommand{\vigenere}{}
{
% make the first row
\tl_set:Nn \l_vigenere_body_tl { \hline \rowcolor{headers} }
\tl_put_right:Nx \l_vigenere_body_tl
{
\int_step_function:nN {26} \vigenere_first:n
}
\tl_put_right:Nn \l_vigenere_body_tl { \\ \hline }
\int_step_inline:nn { 26 }
{
\tl_put_right:Nn \l_vigenere_body_tl { \bfseries \int_to_Alph:n { ##1 } }
\tl_put_right:Nx \l_vigenere_body_tl
{
\int_step_function:nnN { ##1 } { ##1+25 } \vigenere_further:n
}
\tl_put_right:Nn \l_vigenere_body_tl { \\ \hline }
}
\group_begin:
\setlength{\tabcolsep}{0.5pt}\sffamily
\begin{tabular}{ | >{\columncolor{headers}}c | *{26}{c|} }
\l_vigenere_body_tl
\end{tabular}
\group_end:
}

\cs_new:Nn \vigenere_first:n { & \exp_not:N \bfseries \int_to_Alph:n { #1 } }
\cs_new:Nn \vigenere_further:n
{
& \int_to_Alph:n { \int_mod:nn { #1-1 } { 26 } + 1 }
}
\ExplSyntaxOff

\begin{document}

\vigenere

\end{document}


Here's a LuaLaTeX-based solution.

\documentclass{article}
\usepackage[letterpaper,margin=1in]{geometry} % set page parameters suitably
\usepackage{array} % for '\extrarowheight' macro
\usepackage[table]{xcolor}

\usepackage{luacode}
\begin{luacode}

s="ABCDEFGHIJKLMNOPQRSTUVWXYZ"

-- an auxiliary function
function rowprint ( t )
for i = 1,26 do
tex.sprint ( "&" .. t:sub(i,i) )
end
tex.sprint ("\\\\ \\hline")
end

-- the main function
function vigenere ()
tex.sprint ( "\\hline" )
-- header row
tex.sprint ( "\\rowcolor{lightgray}" )
for i = 1,26 do
tex.sprint ( "&\\textbf{\\textsf{" .. s:sub(i,i) .. "}}" )
end
tex.sprint ("\\\\ \\hline")

-- data rows (from A to Z )
t = s
for j=1,26 do
t = s:sub(j) .. s:sub(1,j-1)
tex.sprint (  "\\cellcolor{lightgray}\\textbf{\\textsf{" .. t:sub(1,1) .. "}}" )
rowprint ( t )
end
end

\end{luacode}

\begin{document}
\begin{center}
\setlength\tabcolsep{3pt}
\setlength\extrarowheight{3pt}
\begin{tabular}{|*{27}{c|}}
\directlua{vigenere()}
\end{tabular}
\end{center}
\end{document}

• A follow-up: In case the letters in entire table, and not just in the header column and row, shoul be rendered in the prevailing sans-serif font, just (a) change "&\\textbf{\\textsf{" .. s:sub(i,i) .. "}}" to "&\\textbf{" .. s:sub(i,i) .. "}", (b) change "\\cellcolor{lightgray}\\textbf{\\textsf{" .. t:sub(1,1) .. "}}" to "\\cellcolor{lightgray}\\textbf{" .. t:sub(1,1) .. "}", and (c) insert \sffamily immediately before \begin{tabular}. – Mico Feb 18 at 7:05

Another way of cycling through the alphabet using pgffor:

\documentclass{article}

\usepackage[margin=1in]{geometry}
\usepackage{pgffor,xcolor,eqparbox}

\makeatletter
\newcommand{\ALPH}[1]{%
\ifnum #1>26
\@Alph{\numexpr#1-26}%
\else
\@Alph{#1}%
\fi
}
\makeatother

\begin{document}

\begingroup
\makeatletter
\sffamily\footnotesize
\centering
\fcolorbox{black}{brown!50!white}{\eqmakebox[vignere]{\vphantom{Q}}}% Top-left box
\foreach \Char in {1, ..., 26} {%
\fcolorbox{black}{brown!50!white}{\eqmakebox[vignere]{\vphantom{Q}\bfseries\@Alph{\Char}}}% header
}\\[-1pt]
\foreach \CharA in {0, ..., 25} {%
\fcolorbox{black}{brown!50!white}{\eqmakebox[vignere]{\vphantom{Q}\bfseries\@Alph{\numexpr\CharA + 1}}}% Left column
\foreach \CharB in {0, ..., 25} {%
\fbox{\eqmakebox[vignere]{\vphantom{Q}\ALPH{\numexpr\CharA + \CharB + 1}}}%
}%
\\[-1pt]
}
\par
\endgroup

\end{document}


Just for fun, a ConTeXt solution:

%%% Funny colors
\usecolors[svg]
%%% For a smaller image
\setupbodyfont[small]
\starttext
%%% ConTeXt counterpart of standalone
\startTEXpage[offset=1pt]
%%% The actual fun
\startluacode
local uwu = {
background = "color",
backgroundcolor = "darkkhaki",
style="\\ssbf"
}
context.bTABLE({align="middle", style = "\\ss"})
for i = 0, 26 do
context.bTR()
for j = 0, 26 do
if i == 0 and j == 0 then
context.bTD(uwu)
context("")
elseif i*j == 0 then
context.bTD(uwu)
context(string.char(64+math.max(i,j)))
else
context.bTD()
context(string.char(65+(i+j-2)%26))
end
context.eTD()
end
context.eTR()
end
context.eTABLE()
\stopluacode
\stopTEXpage
\stoptext


Just for fun, another solution based on \foreach macro from OpTeX:

\def\list{ABCDEFGHIJKLMNOPQRSTUVWXYZ}
\def\cyclelist#1#2\relax{\def\list{#2#1}}
\def\fr#1{\hbox to1.3em{\hss#1\hss\vrule height10pt depth4pt}}

\vbox{
\hbox{\ea\foreach\list \do{\hbox to1.3em{\hss\bf #1\hss}}}
\smallskip\hrule
\ea\foreach\list \do{%
\hbox{\llap{\bf#1 }\vrule\ea\foreach\list \do{\fr{##1}}}
\hrule
\ea\cyclelist\list \relax
}%
}

\bye