![如何强制对齐宏生成的 TikZ 图片?](https://linux22.com/image/253015/%E5%A6%82%E4%BD%95%E5%BC%BA%E5%88%B6%E5%AF%B9%E9%BD%90%E5%AE%8F%E7%94%9F%E6%88%90%E7%9A%84%20TikZ%20%E5%9B%BE%E7%89%87%EF%BC%9F.png)
我是 LaTeX 的初学者,正在尝试将其用于工业文档。下面的代码绘制了一系列机械按键,其几何形状和颜色代码由标准定义。但是,当我将代码放在表中时,按键的图片会错位,可能是因为边界框会调整到每个按键的包络。我无法正确地将每个按键的垂直零轴(圆心)与表格单元格重新对齐。尝试手动调整剪辑区域可能是一种方法,但不是很干净。有没有办法将边界框的中心冻结在按键直径的中心?或者完全改进这个代码?
\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}
答案1
我列出了下面所做的更改,这些更改似乎修复了对齐问题:
- 在每个初始
\shade
命令之后,我都添加了coordinate=(C)
以供以后参考 - 我已经在两张 TikZ 图片中添加了这样的内容:无论垂直长度如何,
baseline=(C)
基线始终相交(C)
\KeyFront{2.5}{green}{90} \tabularnewline\hline
我在表格的最后一部分添加了一个额外的空格。
我希望这些是唯一的:)
\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}