![在方程中对齐 tikz 图片](https://linux22.com/image/398061/%E5%9C%A8%E6%96%B9%E7%A8%8B%E4%B8%AD%E5%AF%B9%E9%BD%90%20tikz%20%E5%9B%BE%E7%89%87.png)
我正在尝试编写一个涉及 tikz 图片和数学符号的方程式。这是 MWE:
\documentclass{article}
\usepackage{tikz}
\begin{document}
$T=\begin{tikzpicture}[line cap=round,line join=round,x=1cm,y=1cm]
\draw [line width=1pt] (-6,1)-- (-3,1);
\draw [line width=1pt] (-5,2)-- (-5,0);
\draw [line width=1pt] (-4,2)-- (-4,0);
\end{tikzpicture}$
\end{document}
它产生了类似这样的东西。
但我希望图片的放置方式使得 $T=$ 东西与图片的水平线对齐。有什么想法可以做到吗?
答案1
\documentclass{article}
\usepackage{tikz}
\begin{document}
You could just use \verb|\vcenter{\hbox{\begin{tikzpicture}|$\cdots$\verb|\end{tikzpicture}}}|
\[T=\vcenter{\hbox{\begin{tikzpicture}[line cap=round,line join=round,x=1cm,y=1cm]
\draw [line width=1pt] (-6,1)-- (-3,1);
\draw [line width=1pt] (-5,2)-- (-5,0);
\draw [line width=1pt] (-4,2)-- (-4,0);
\end{tikzpicture}}}\]
or, alternatively, adjust the \verb|baseline|
\[T=\begin{tikzpicture}[line cap=round,line
join=round,x=1cm,y=1cm,baseline={1cm-0.5*height("$=$")}]
\draw [line width=1pt] (-6,1)-- (-3,1);
\draw [line width=1pt] (-5,2)-- (-5,0);
\draw [line width=1pt] (-4,2)-- (-4,0);
\end{tikzpicture}\]
\end{document}
请注意,如果tikzpicture
有节点,则可以将 的基线设置为tikzpicture
节点的基线,baseline={(X.base)}
其中X
是节点的名称。
答案2
Tikz 允许将基线对齐到坐标上。请参阅 3.1.3 手册第 127 页。
在这里,只要将它对齐就可以了\draw[line width=1pt] (-6,1)-- (-3,1);
baseline={(0, 1cm)}
但正如海科·奥伯迪克,等号位于数学轴的中心。因此,从基线坐标中减去这个高度就足够了。
\pgfmathsetmacro\MathAxis{height("$\vcenter{}$")}
baseline={(0, 1cm-\MathAxis pt)}
\documentclass{article}
\usepackage{tikz}
\begin{document}
% macro from Heiko Oberdiek solution https://tex.stackexchange.com/a/355693/138900
\pgfmathsetmacro\MathAxis{height("$\vcenter{}$")}
$T=\begin{tikzpicture}[baseline={(0, 1cm-\MathAxis pt)},line cap=round,line join=round,x=1cm,y=1cm]
\draw [line width=1pt] (-6,1)-- (-3,1);
\draw [line width=1pt] (-5,2)-- (-5,0);
\draw [line width=1pt] (-4,2)-- (-4,0);
\end{tikzpicture}$
\end{document}