6

I am trying to create a boxed vertical vector of math symbols in TikZ. My goal is to have all the terms centered in the box (vertically and horizontally) and have the box auto-sized to fit around everything with the desired sep. I was able to achieve this using a minipage combined with a gather* environment, but it seems hacky since I have to manually remove some extra space at the top and set the minipage width. I'm wondering if there is a "proper" or more elegant way of achieving this result.

\documentclass[tikz,dvipsnames]{standalone}
\standaloneconfig{border=1mm}
\usepackage{amsmath}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture} \node[draw=black,fill=white,rounded corners,ultra thick,inner sep=2.5mm] (u) {
\begin{minipage}{8mm} \centering \vspace{-5mm} \begin{gather} u^0 \ u^1 \ \vdots \ u^{N-1} \end{gather} \end{minipage} \vspace{-5mm}};

\end{tikzpicture}

\end{document}

jared
  • 231
  • The space above \vdots is, sadly, built in, and not originating with amsmath. Take a look at this question: https://tex.stackexchange.com/q/528774 – barbara beeton Jul 26 '23 at 19:24
  • @barbarabeeton If that's the case for \vdots, then I will accept that, but I'm manually editing the space above u^0 and the width of the box. I'm not sure if this can be done without a minipage, but it would be simpler if that wasn't required. – jared Jul 26 '23 at 19:29
  • I was only addressing the \vdots spacing -- that's inherited from plain TeX. @David has taken care of the centering and overall dimensions; both he and I have worked on the amsmath user documentation over the years, so I'm glad you find it understandable. – barbara beeton Jul 26 '23 at 23:19

2 Answers2

8

All AMS alignments have a ...ed form that is set natural width rather than being a full width display, so you can avoid minipage with gathered

enter image description here

\documentclass[tikz,dvipsnames]{standalone}
\standaloneconfig{border=1mm}
\usepackage{amsmath}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture} \node[draw=black,fill=white,rounded corners,ultra thick,inner sep=2.5mm] (u) {%
$\begin{gathered} u^0 \ u^1 \[-.7em] \vdots \ u^{N-1} \end{gathered}$% }; \end{tikzpicture}

\end{document}

David Carlisle
  • 757,742
  • I think I prefer your solution over Sandy's since it doesn't require going in and out of math mode (everything is in one gathered environment). I haven't heard about the ...ed amsmath alignment forms. Do you have a link that explains the differences and when one might want to use one over the other? – jared Jul 26 '23 at 20:09
  • 2
    texdoc amsldoc will bring up the doc but there are no cases where you could use either. align, gather, ... are like equation: display environments that you start from a paragraph and they make a full width math display. aligned, gathered, ... are like array they are a natural width alignments that makes a term within an existing math formula. @jared – David Carlisle Jul 26 '23 at 20:13
  • The example in "3.7 Alignment building blocks" clarifies things nicely for me. – jared Jul 26 '23 at 20:17
  • I suppose I should scold you for using em as a vertical dimension. (Yes, I know the nominal default font is square, but not all are.) – barbara beeton Jul 26 '23 at 23:25
  • @barbarabeeton you should never scold me – David Carlisle Jul 26 '23 at 23:29
  • @barbarabeeton What should be used instead of em? I'm using this in a beamer presentation with a sans-serif font, so it might be relevant. – jared Jul 27 '23 at 05:08
  • @jared barbara is suggesting I use 1ex rather than .7em although it doesn't make a lot of difference here really, both are just chosen "by eye" to look about right and the both change with the current font size, which is the important part. – David Carlisle Jul 27 '23 at 08:24
5

There's no need for minipage or gather or centering. Just add align=center to your node options and you can use multiline contents with \\. Optional vertical spacing can be added.

enter image description here

\documentclass{article}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture} \node[draw=black, fill=white, rounded corners, ultra thick, inner sep=2.5mm, align=center] (u) {
$u^0$ \ $u^1$ \ $\vdots$ \[.8ex] $u^{N-1}$ }; \end{tikzpicture}

\end{document}

Sandy G
  • 42,558
  • That's certainly a lot simpler. Is the [0.8ex] somehow dealing with the extra space above \vdots or is it doing something else? – jared Jul 26 '23 at 19:57
  • 1
    Note this uses tighter superscript spacing most noticable in the last row where the bottom of the N is well below the top of the u, this may be good or bad depending on what the OP wants:-) (+1 either way:-) – David Carlisle Jul 26 '23 at 19:59
  • @DavidCarlisle I didn't notice that, but it seems insignificant that it does not matter for my use case. Is it tighter because it's not using displaystyle? – jared Jul 26 '23 at 20:01
  • 1
    @jared yes exactly – David Carlisle Jul 26 '23 at 20:02
  • 1
    @jared: The [.8ex] adds vertical space after the \vdots. As David Carlisle correctly notes, the spacing here is tighter than yours so I thought adding space below looked better than removing space above. – Sandy G Jul 26 '23 at 20:03