# How to get vertical-mode math-expression inside tikz node?

I am aware that \boxed automatically invokes display-mode math, so \boxed{2+2=4} is equivalent to frame drawn around $2+2=4$. But how do I get vertical-mode math expression inside tikzpicture node to draw a frame around it with tikz?

In case the OP meant "stacked" rather than "vertical mode", there is this:

\documentclass[border=3pt]{standalone}
\usepackage{amsmath,tabstackengine}
\TABstackMath
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\path node[draw]
{
\alignCenterstack{
2+2=&4\\
12+2=&14
}
};
\end{tikzpicture}
\end{document}


With vertical mode, one contends with the full \linewidth, whereas a stack can emulate certain features of vertical mode, while remaining in horizontal mode. Thus, the side margins only extend to the end of the content.

• To be exact, I meant placing gather environment (which qualifies as vertical-mode math?) of amsmath into a node. – bp2017 May 21 at 17:49
• @bp2017 Got it. I had some confusion as it wasn't clear whether you wanted all the side margin that comes automatically with vertical mode or not. – Steven B. Segletes May 21 at 17:51

You could switch to text mode with \text but it might be easier to just use \fbox. With TikZ I recommend tikzmark since this detects the current mode and you do not have to worry about these things any more.

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\begin{document}
$\text{\boxed{2+2=4}}$
$\fbox{2+2=4}$
$\tikzmarknode[draw,inner sep=0.8ex]{tn1}{2+2=4}$
\end{document}


• @bp2017 It automatically brings you in the mode of it surroundings, which is math mode in this case. This can be seen from the fact that the spacing is consistent. To see that even more explicitly try $\tikzmarknode[draw,inner sep=0.8ex]{tn1}{2^2=4}$ and note that ^ in not allowed in text mode. – user121799 May 3 at 23:51
• @bp2017 Yes, it is a separate library (not package) written by Loop Space, and has its own documentation under CTAN here. I agree that the CTAN entry is a bit misleading, but to the best of my knowledge it is a library, meaning you load it with \usetikzlibrary and not \usepackage. – user121799 May 4 at 0:00
• @bp2017 This means that you need to update your TeX installation, \tikzmarknode has been added last year. Now it is a good time to update since TeXLive2019 is out. If you use overleaf and cannot update your installation, you could temporarily use \tikznode from here. – user121799 May 4 at 0:08
• I couldn't make following work: \documentclass{article} \usepackage{amsmath} \usepackage{tikz} \usetikzlibrary{tikzmark} \begin{document} % following line doesn't work %\begin{gather*}\tikzmarknode[draw,inner sep=0.8ex]{tn1}{2+2=4\\2+2=4}\end{gather*} % following line works works %\begin{gather*}2+2=4\\2+2=4\end{gather*} \end{document} – bp2017 May 11 at 6:50
• @bp2017 This doesn't work for many reasons. One is that you need to add an alignment if you have a line break in a node. But even if you add it, this won't work because gather "looks" for the \\ . If you want to draw a box around a gather environment, use empheq or tcolorbox, whichtake care of all these things. – user121799 May 11 at 13:50
\documentclass{standalone}
\usepackage{amsmath}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\path node[draw]
{ \parbox{\textwidth}
{ \begin{gather*}
2+2=4\\
2+2=4
\end{gather*}
}
};
\end{tikzpicture}
\end{document}