3

I want to write multiple lines of text within a TikZ rectangle. This is how I did it.

\documentclass{article}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}
\node at (0, 0) [rectangle,draw,minimum size=2cm] {
    \begin{tabular}{c} \( a^2 \) \\ \( b^2 \) \end{tabular}
};
\end{tikzpicture}

\end{document}

enter image description here

This works fine. But I want to understand why using another environment, such as, align* environment or displayed math does not work well like the tabular environment does.

For example, the following code

\documentclass{article}
\usepackage{tikz}
\usepackage{amsmath}
\begin{document}

\begin{tikzpicture}
\node at (0, 0) [rectangle,draw,minimum size=2cm] {
    \begin{align*} a^2 \\ b^2 \end{align*}
};
\end{tikzpicture}

\end{document}

leads to the following error.

! Missing \endgroup inserted.
<inserted text> 
                \endgroup 
l.8     \begin{align*} a^2 \\ b^2 \end{align*}

The following code

\documentclass{article}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}
\node at (0, 0) [rectangle,draw,minimum size=2cm] {
    \[ a^2 \]
};
\end{tikzpicture}

\end{document}

leads to the following error.

! Missing $ inserted.
<inserted text> 
                $
l.8     \[ a^
             2 \]

Here are my questions:

  1. Why is special about tabular environment (compared to align* or displayed math) that makes it possible to use it as text within a node?
  2. What do the Missing \endgroup inserted or Missing $ inserted errors mean?
5
  • Can't really answer your questions properly, but try setting text width=5cm. Nov 22, 2017 at 2:32
  • It also works with $\begin{array}{c} a^2 \\ b^2 \end{array}$
    – Cragfelt
    Nov 22, 2017 at 2:37
  • Tabular is only as wide as it needs to be, but align fills the whole page. Except that, unless you specify text width a node has nothing to fill. Note that using text width is equivalent to putting the contents into a \parbox. Nov 22, 2017 at 4:00
  • Regarding your question on the error with \[ a^2 \], try changing it by $a^2$ and the error dissapears. Somehow, math delimiters \[ ... \] are not allowed inside TikZ.
    – Cragfelt
    Nov 22, 2017 at 9:49
  • Did you find a solution after the answers posted or links?
    – Cragfelt
    Dec 20, 2017 at 8:30

2 Answers 2

1

Another solution using \shortstack[position]{... \\ ... \\ ...}

The \shortstack command produces a stack of objects. The valid positions are:

  • r Moves the objects to the right of the stack
  • l Moves the objects to the left of the stack
  • c Moves the objects to the center of the stack (default)
\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\begin{document}

Using ``shortstack'' \\

\begin{tikzpicture}
\node at (0, 0) [rectangle,draw,minimum size=2cm] {
\shortstack{$a^2$\\$b^2$}
};
\end{tikzpicture}

\end{document}

enter image description here

1
  • 1
    (+1) For sportmanship and for explanation. Someone downvoted both answers for some reason at the moment they marked it as duplicate.
    – Cragfelt
    Dec 20, 2017 at 8:32
0

You can use array or alined environments

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\begin{document}

Using ``array'' \\

\begin{tikzpicture}
\node at (0, 0) [rectangle,draw,minimum size=2cm] {
$\begin{array}{c} a^2 \\ b^2 \end{array}$
};
\end{tikzpicture}
\bigskip

Using ``aligned'' \\

\begin{tikzpicture}
\node at (0, 0) [rectangle,draw,minimum size=2cm] {
$\begin{aligned} a^2 \\ b^2 \end{aligned}$
};
\end{tikzpicture}

\end{document}

enter image description here

And regarding your concerns about using one environment or another, I found some important information about it on answers of the following questions

1.Align/Array equation - explain the options Answer "array is designed for matrices and sets its content in inline math mode and is not optimised for aligning a single equation." -@David Carlisle

2.Aligned equations inside of TikZ node. Comment "align is displayed, set off normal text and centered. Here it requires a minipage environment or \parbox, so it needs specifying a width. At least for the centering." – @Stefan Kottwitz

4

Not the answer you're looking for? Browse other questions tagged or ask your own question.