Aligning tikz picture in equation

\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}

enter image description here

Note that if the tikzpicture has nodes, you can set the baseline of the tikzpicture to, say, the baseline of a node, baseline={(X.base)}, where X is the name of the node.


Tikz allows to align the base line on a coordinate. See p 127 of the 3.1.3 manual.

here, it is enough to align it on the line \draw[line width=1pt] (-6,1)-- (-3,1);

baseline={(0, 1cm)}

But, as explained by Heiko Oberdiek, the equals sign is centered around the math axis. It is therefore sufficient to subtract this height from the baseline coordinate.

\pgfmathsetmacro\MathAxis{height("$\vcenter{}$")}
baseline={(0, 1cm-\MathAxis pt)}

enter image description here

\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}

Tags:

Tikz Pgf