I am a beginner in LaTeX, trying to use it for industrial documents. The code below draws a series of mechanical keys, whose geometry and color codes are defined by a standard. However when I place the code in the table, the pictures of the keys get misaligned, possibly because the bounding box adjusts to the envelope of eack key. I have been unable to realign properly the vertical zero axis of each key (center of circles) with the table cells. Trying to adjust manually the clipping area may be a way, but not a very clean one. Is there a way to freeze the center of the bounding box on the center of the key diameter? Or to improve this code altogether?
\documentclass{article}
\usepackage[english,]{babel}
\usepackage{tikz}
\usepackage{siunitx}% Handling Si units
\usepackage{ifthen} % Use of tests
%===================================
%BEGIN DRAWING SINGLE KEY FRONT ---
%\SingleKeyFront{scale}{color}{width1}{angle key1}{rotation angle}
\newcommand{\SingleKeyFront}[5]{%
%=======General dimensions==========
\def\Magnif{#1} %picture Magnification ratio
\def\ClippingLeftPoint{-0.4,-0.4}
\def\ClippingRightPoint{0.4,0.4}
\def\KeyAngleOne{#4} %Angular position of key
%=======Shell dimensions===========
\def\DiaShellMachining{0.925}
\def\DiaExtKey{.76}
\def\DiaExt{0.63}
\def\DiaInt{0.5}
\def\KeyWidthOne{#3}
\begin{tikzpicture}[scale=\Magnif]
\clip (\ClippingLeftPoint) rectangle (\ClippingRightPoint); %cropping picture
\shade[inner color=black!60, outer color=black! 80] (0,0) circle (\DiaInt/2);
\filldraw [draw=black!80, very thick, fill=#2! 75,even odd rule] (0,0) circle(\DiaInt/2) circle(\DiaExt/ 2); %Painted ring at front of key
\begin{scope}[rotate=#5]
\begin{scope}[rotate=\KeyAngleOne-90]
\pgfmathparse{0.5*\DiaExtKey*cos(asin (\KeyWidthOne/(\DiaExtKey)))} \let \Youter\pgfmathresult
\pgfmathparse{0.5*\DiaExt*cos(asin (\KeyWidthOne/(\DiaExt)))} \let\Yinner \pgfmathresult
\pgfmathparse{(\Youter) / (0.5*\KeyWidthOne)} \let\ThetaOne \pgfmathresult % Y coordinate of upper key corners
\pgfmathparse{(\Yinner) / (0.5*\KeyWidthOne)} \let\ThetaTwo \pgfmathresult %Y coordinate of lower key corners
\pgfmathparse{sqrt ((\Youter)^2)+((0.5*\KeyWidthOne) ^2 )} \let\ra \pgfmathresult
\filldraw[draw=black!75, very thick, fill=black!50] ({atan(\ThetaOne)}:\ra) -- ({atan(\ThetaTwo)}:0.5*\DiaExt) arc({atan(\ThetaTwo)}:{180-atan(\ThetaTwo)}:0.5*\DiaExt) -- ({180-atan(\ThetaOne))}:\ra) arc ({180-atan(\ThetaOne))}: {atan(\ThetaOne)}:\ra) ;
\end{scope}
\end{scope}
\end{tikzpicture}
}%
%END DRAWING SINGLE KEY FRONT -------
%====================================
%====================================
%BEGIN DRAWING DUAL KEY FRONT========
%Routine drawing dual key front------
%\DualKeyFront{scale}{color}{width1}{width2}{angle key1}{angle key2}{rotation angle}
\newcommand{\DualKeyFront}[7]{%
%=======General dimensions==========
\def\Magnif{#1} %picture Magnification ratio
\def\ClippingLeftPoint{-0.4,-0.4}
\def\ClippingRightPoint{0.4, 0.4}
\def\KeyAngleOne{#5} %Angular position of key
\def\KeyAngleTwo{#6} %Angular position of key
%=======Shell dimensions==========
\def\DiaShellMachining{0.925}
\def\DiaExtKey{.76}
\def\DiaExt{0.63}
\def\DiaInt{0.5}
\def\KeyWidthOne{#3}
\def\KeyWidthTwo{#4}
\begin{tikzpicture}[scale=\Magnif]
%\clip (\ClippingLeftPoint) rectangle (\ClippingRightPoint); %cropping picture
\shade[inner color=black!60, outer color=black! 80] (0,0) circle (\DiaInt/2);
\filldraw [draw=black!80, very thick, fill=#2! 75,even odd rule] (0,0) circle(\DiaInt/2) circle(\DiaExt/ 2); %Painted ring at front of key
\begin{scope}[rotate=#7]
\begin{scope}[rotate=\KeyAngleOne-90]
\pgfmathparse{0.5*\DiaExtKey*cos(asin (\KeyWidthOne/(\DiaExtKey)))} \let \Youter\pgfmathresult
\pgfmathparse{0.5*\DiaExt*cos(asin (\KeyWidthOne/(\DiaExt)))} \let\Yinner \pgfmathresult
\pgfmathparse{(\Youter) / (0.5*\KeyWidthOne)} \let\ThetaOne \pgfmathresult % Y coordinate of upper key corners
\pgfmathparse{(\Yinner) / (0.5*\KeyWidthOne)} \let\ThetaTwo \pgfmathresult %Y coordinate of lower key corners
\pgfmathparse{sqrt ((\Youter)^2)+((0.5*\KeyWidthOne) ^2 )} \let\ra \pgfmathresult
\filldraw[draw=black!75, very thick, fill=black!50] ({atan(\ThetaOne)}:\ra) -- ({atan(\ThetaTwo)}:0.5*\DiaExt) arc({atan(\ThetaTwo)}:{180-atan(\ThetaTwo)}:0.5*\DiaExt) -- ({180-atan(\ThetaOne))}:\ra) arc ({180-atan(\ThetaOne))}: {atan(\ThetaOne)}:\ra) ;
\end{scope}
\begin{scope}[rotate=\KeyAngleTwo-90]
\pgfmathparse{0.5*\DiaExtKey*cos(asin (\KeyWidthOne/(\DiaExtKey)))} \let \Youter\pgfmathresult
\pgfmathparse{0.5*\DiaExt*cos(asin (\KeyWidthOne/(\DiaExt)))} \let\Yinner \pgfmathresult
\pgfmathparse{(\Youter) / (0.5*\KeyWidthTwo)} \let\ThetaOne \pgfmathresult % Y coordinate of upper key corners
\pgfmathparse{(\Yinner) / (0.5*\KeyWidthTwo)} \let\ThetaTwo \pgfmathresult %Y coordinate of lower key corners
\pgfmathparse{sqrt ((\Youter)^2)+((0.5*\KeyWidthTwo) ^2 )} \let\ra \pgfmathresult
\filldraw[draw=black!75, very thick, fill=black!50] ({atan(\ThetaOne)}:\ra) -- ({atan(\ThetaTwo)}:0.5*\DiaExt) arc({atan(\ThetaTwo)}:{180-atan(\ThetaTwo)}:0.5*\DiaExt) -- ({180-atan(\ThetaOne))}:\ra) arc ({180-atan(\ThetaOne))}: {atan(\ThetaOne)}:\ra) ;
\end{scope}
\end{scope}
\end{tikzpicture}
}%
%END DRAWING DUAL KEY FRONT=======
%===========================
% BEGIN TOP LEVEL COMMAND DRAWING KEY FRONT
% \KeyFront}{scale}{color}{orientation}
\newcommand{\KeyFront}[3]{%
\ifthenelse{\equal{#2}{purple}}{\SingleKeyFront{#1}{#2}{.32}{0}{#3}}{}
\ifthenelse{\equal{#2}{yellow}}{\DualKeyFront{#1}{#2}{.195}{.1}{0}{60}{#3}}{}
\ifthenelse{\equal{#2}{green}}{\DualKeyFront{#1}{#2}{.195}{.1}{0}{120}{#3}}{}
\ifthenelse{\equal{#2}{blue}}{\DualKeyFront{#1}{#2}{.195}{.1}{0}{180}{#3}}{}
\ifthenelse{\equal{#2}{orange}}{\DualKeyFront{#1}{#2}{.195}{.1}{0}{240}{#3}}{}
\ifthenelse{\equal{#2}{white}}{\DualKeyFront{#1}{#2}{.195}{.1}{0}{300}{#3}}{}
\ifthenelse{\equal{#2}{neutral}}{\SingleKeyFront{#1}{gray}{.195}{0}{#3}}{}
}
%END TOP LEVEL COMMAND DRAWING KEY FRONT
\begin{document}
\begin{figure}[h]
\renewcommand{\arraystretch}{0.4}
\centering\bfseries
\begin{tabular}{|>\centering p{2.5cm}|>\centering p{2.5cm}|>\centering p{2.5cm}|>\centering p{2.5cm}|}
\hline\rule[3 ex]{0pt}{0pt}
Key form 10 (neutral)& Key form 01 (purple)&
Key form 02 (yellow)& Key form 03 (green)\tabularnewline &&&\\
\KeyFront{2.5}{neutral}{90}&
\KeyFront{2.5}{purple}{90}&
\KeyFront{2.5}{yellow}{90}&
\KeyFront{2.5}{green}{90}\tabularnewline\hline
\end{tabular}
\caption{Types of available keys}
\end{figure}
\end{document}
