# How to draw a Caesar Cipher diagram with tikz? [duplicate]

I just tried start rewrite code of circle like this Draw a Clock using tkz-euclide, but I haven't any idea how write frames and arrow over of circle code. I want that show circle code how cipher/decrypt words using the for example Caesar cipher. EDIT: After advive in comments I rewrite example code to solve my problem. Now, I can draw arrow over a picture with number?

\documentclass{article}

\usepackage{chancery}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{decorations.text}
\tikzset{curved text/.style={
decoration={text effects along path,
text/.expanded=#1, text align = center,
text effects/.cd, text along path, decorate}}
}
\begin{document}

\begin{tikzpicture}[x = 1em, y = 1em]
\foreach \i in {0,...,25}{ (\i*13.85:15) -- (\i*13.85:12) -- (\i*13.85:9) };
\node [align=center] {Caesar \\ Cipher};
\foreach [count=\a from 0] \text in {%
{X},
{Y},
{Z},
{A},
{B},
{C},
{D},
{E},
{F},
{G},
{H},
{I},
{J},
{K},
{L},
{M},
{N},
{O},
{P},
{Q},
{R},
{S},
{T},
{U},
{V},
{W}
}
\foreach \t [count=\r] in \text
\path [curved text=\t, rotate=-\a*13.85]
(90:15-\r) arc (90:13.85:15-\r);

\foreach [count=\b from 0] \text in {%
{A},
{B},
{C},
{D},
{E},
{F},
{G},
{H},
{I},
{J},
{K},
{L},
{M},
{N},
{O},
{P},
{Q},
{R},
{S},
{T},
{U},
{V},
{W},
{X},
{Y},
{Z}
}
\foreach \t [count=\r] in \text
\path [curved text=\t, rotate=-\b*13.85]
(90:12-\r) arc (90:13.85:12-\r);
\end{tikzpicture}

\end{document}


Something like this? \documentclass[tikz,border=2mm]{standalone}
\usepackage{libertine} % Change the font to your liking
\usepackage[T1]{fontenc}
\usetikzlibrary{decorations.text,arrows.meta,calc}

\tikzset{curved text/.style={decorate,
decoration={text effects along path,
text={#1}, text align=center,
text effects/.cd, text along path}}}

\begin{document}
\begin{tikzpicture}[x=1em,y=1em]
%   set up
\pgfmathsetmacro\angdiv{360/26}
\pgfmathtruncatemacro\caeser{7} % Input Caeser shift here! (positive for clockwise)
\coordinate (n-0) at (90+\angdiv/2:7) {};
\coordinate (m-0) at (90-\caeser*\angdiv+\angdiv/2:5) {};
%   draw Caeser diagram
\foreach \i in {0,...,25}{%
($({90-(\i-1/2)*\angdiv}:8)$) -- ($(({90-(\i-1/2)*\angdiv}:6.5)$)
($({90-(\i-1/2)*\angdiv}:4.5)$) -- ($(({90-(\i-1/2)*\angdiv}:6)$)
};
\foreach [count=\a from 0] \text in {A,B,...,Z}{
\pgfmathtruncatemacro\b{\a+1}%
\path [curved text=\text] (n-\a) arc [start angle=90-(\a-1/2)*\angdiv, delta angle=-\angdiv, radius=7] node (n-\b) {};
\path [curved text=\text] (m-\a) arc [start angle=90-(\a+\caeser-1/2)*\angdiv, delta angle=-\angdiv, radius=5] node (m-\b) {}; % Inner circle
}
%   draw arrows
\draw [arrows={-Latex}]
(65:9.5) to[bend left=20,edge label=$+3$] (40:9.5);
\end{tikzpicture}
\end{document}


The thing you most likely need to change in this code is the Caeser shift, which you can do so in the line of \pgfmathtruncatemacro\caeser{7}. I have also indicated in the code exactly where it is. Note that a positive Caeser shift is a clockwise translation.

This method is extendable to a Caeser wheel with 5 letters, for example. I've indicated the locations in the code to make the change to produce the given output. \documentclass[tikz,border=2mm]{standalone}
\usepackage{libertine} % Change the font to your liking
\usepackage[T1]{fontenc}
\usetikzlibrary{decorations.text,arrows.meta,calc}

\tikzset{curved text/.style={decorate,
decoration={text effects along path,
text={#1}, text align=center,
text effects/.cd, text along path}}}

\begin{document}
\begin{tikzpicture}[x=1em,y=1em]
%   set up
\pgfmathsetmacro\angdiv{360/5} <-----------------
\pgfmathtruncatemacro\caeser{2} % Input Caeser shift here! (positive for clockwise)
\coordinate (n-0) at (90+\angdiv/2:7) {};
\coordinate (m-0) at (90-\caeser*\angdiv+\angdiv/2:5) {};
%   draw Caeser diagram
\foreach \i in {0,...,4}{% <-----------------------
($({90-(\i-1/2)*\angdiv}:8)$) -- ($(({90-(\i-1/2)*\angdiv}:6.5)$)
($({90-(\i-1/2)*\angdiv}:4.5)$) -- ($(({90-(\i-1/2)*\angdiv}:6)$)
};
\foreach [count=\a from 0] \text in {A,B,...,E}{% <------------------
\pgfmathtruncatemacro\b{\a+1}%
\path [curved text=\text] (n-\a) arc [start angle=90-(\a-1/2)*\angdiv, delta angle=-\angdiv, radius=7] node (n-\b) {};
\path [curved text=\text] (m-\a) arc [start angle=90-(\a+\caeser-1/2)*\angdiv, delta angle=-\angdiv, radius=5] node (m-\b) {}; % Inner circle
}
%   draw arrows
\draw [arrows={-Latex}]
(65:9.5) to[bend left=20,edge label=$+3$] (40:9.5);
\end{tikzpicture}
\end{document}

• It's what I want. I am very grateful, you saved my time for this, because I am beginner in using tikz.
– user128716
May 27, 2017 at 9:03
• @nowek7 Thank you for accepting my answer! But do wait around to see if someone else comes up with a better solution or a solution that you like better. Mine was focused on drawing the Caeser diagram because it was the initial question. But good attempt on your end! A good way to learn TikZ is to give it a go at it yourself (that's the reason why I'm attempting this question as well) :)
– Troy
May 27, 2017 at 9:07

You can also simplify your \foreach commands:

\documentclass{article}

\usepackage{chancery}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{decorations.text}
\tikzset{curved text/.style={
decoration={text effects along path,
text/.expanded=#1, text align = center,
text effects/.cd, text along path, decorate}}
}
\begin{document}

\begin{tikzpicture}[x = 1em, y = 1em]
\foreach \i in {0,...,25}{ (\i*13.85:15) -- (\i*13.85:12) -- (\i*13.85:9) };
\node [align=center] {Caesar \\ Cipher};
\foreach [count=\a from 0] \text in {X,Y,Z,A,B,...,W}
\foreach \t [count=\r] in \text
\path [curved text=\t, rotate=-\a*13.85]
(90:15-\r) arc (90:13.85:15-\r);

\foreach [count=\b from 0] \text in {A,B,...,Z}
\foreach \t [count=\r] in \text
\path [curved text=\t, rotate=-\b*13.85]
(90:12-\r) arc (90:13.85:12-\r);

\draw[->](55:16) arc(55:13.85:16) node[midway, above, xshift=6pt]{$+3$};
\end{tikzpicture}

\end{document} A version with the shift as an optional argument (default 3).

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\newcommand\Ceasar{%
\begin{tikzpicture}
\def\R{2}
\def\dR{0.5}
\def\Ri{1.4}
\def\dRi{0.5}
\def\Rotate{#1}%% Number of shifts
\coordinate (origin) at (0,0);
\draw[fill=gray!60] (origin) circle (1pt);
\draw (origin) circle (\R);
\draw (origin) circle (\R+\dR);
\draw (origin) circle (\Ri);
\draw (origin) circle (\Ri+\dRi);
\foreach \Letter [count=\ind from 0,evaluate=\ind as \ang using 90-\ind*360/26] in {A,B,...,Z}{%
\node[rotate={\ang-90}] at ($(origin)+(\ang:{\R+\dR/2})$) {\Letter};
\draw({\ang+0.5*360/26}:\R)--({\ang+0.5*360/26}:\R+\dR);
}
\foreach \Letter [count=\ind from 0,evaluate=\ind as \ang using 90-\ind*360/26-\Rotate*360/26] in {A,B,...,Z}{%
\node[rotate={\ang-90}] at ($(origin)+(\ang:{\Ri+\dRi/2})$) {\Letter};
\draw({\ang+0.5*360/26}:\Ri)--({\ang+0.5*360/26}:\Ri+\dRi);
}
\draw[-latex] (90:\Ri-0.3) arc (90:{90-\Rotate*360/26}:\Ri-0.3)node[pos=0.5,anchor=90-\Rotate*180/26]{$\Rotate$};
\end{tikzpicture}
}
\begin{document}
\Ceasar \Ceasar \Ceasar[-5]
\end{document} 