Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question

1 Answer 1

up vote 4 down vote accepted

I have listed the changes I've made below that seemingly fixes the alignment:

  • After each initial \shade command, I have added coordinate=(C) to reference it later
  • I've added baseline=(C) to both TikZ pictures such that the baseline always meets (C) no matter the vertical length
  • I've put an additional space in \KeyFront{2.5}{green}{90} \tabularnewline\hline in the last part of the tabular.

I hope these are the only ones :)

\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,baseline=(C)]
\clip (\ClippingLeftPoint) rectangle (\ClippingRightPoint); %cropping picture 
\shade[inner color=black!60, outer color=black! 80] (0,0) circle (\DiaInt/2) coordinate (C);
\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,baseline=(C)]
\clip (\ClippingLeftPoint) rectangle (\ClippingRightPoint); %cropping picture 
\shade[inner color=black!60, outer color=black! 80] (0,0) circle (\DiaInt/2) coordinate (C);

\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{\label{Keys}Types of available keys}
\renewcommand{\arraystretch}{1.2}
\end{figure}
\end{document}

enter image description here

share|improve this answer
    
Thanks. This is great. I had searched on my own, and there was no way I would have found this nice solution. –  Yves Jul 28 '12 at 12:00
    
@Yves My pleasure. –  percusse Jul 28 '12 at 17:40
    
In fact there is still a strange alignment problem in the horizontal direction. It is made visible by adding the "framed" option of the tikzlibrary "backgrounds" in the tikz pictures, and increasing the width of the colums of the table to say 4 cm. The neutral key remains centered, but the other ones are shifted to the left of the cell. Changing the fonts may create similar unexpected alignement changes.... –  Yves Jul 29 '12 at 6:42
    
@Yves Sorry for the delay. I'll look into it when I have the chance. –  percusse Aug 1 '12 at 21:15
    
@Yves This is probably have to do with the column specifications but I'm not sure yet. It's strange indeed. –  percusse Aug 4 '12 at 11:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.