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?
Asked
Active
Viewed 619 times
3
bp2017
- 3,756
- 1
- 13
- 33
3 Answers
3
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.
Steven B. Segletes
- 237,551
-
@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 '19 at 17:51
1
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}
-
1@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. – May 03 '19 at 23:51 -
1@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
\usetikzlibraryand not\usepackage. – May 04 '19 at 00:00 -
1@bp2017 This means that you need to update your TeX installation,
\tikzmarknodehas 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\tikznodefrom here. – May 04 '19 at 00: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 '19 at 06:50
-
1@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, useempheqortcolorbox, whichtake care of all these things. – May 11 '19 at 13:50
1
\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}

bp2017
- 3,756
- 1
- 13
- 33

