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?
Lone Learner
  • 3,226
  • 24
  • 44
  • Can't really answer your questions properly, but try setting text width=5cm. – Torbjørn T. Nov 22 '17 at 02:32
  • It also works with $\begin{array}{c} a^2 \\ b^2 \end{array}$ – Cragfelt Nov 22 '17 at 02: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. – John Kormylo Nov 22 '17 at 04: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 '17 at 09:49
  • Did you find a solution after the answers posted or links? – Cragfelt Dec 20 '17 at 08:30

2 Answers2

2

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

flav
  • 4,714
  • 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 '17 at 08: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

Cragfelt
  • 4,005
  • Do you know the answer to the two questions I have asked in my question? – Lone Learner Nov 22 '17 at 08:39
  • @LoneLearner Unfortunately not. My knowledge in LaTeX is limited at that level and these such of things are very profound for me, I have to say. However, surfing into TeX.SE I have found many interensting answers that could be a good starting point for those concerns of yours. https://tex.stackexchange.com/questions/4736/what-is-the-difference-between-fragile-and-robust-commands, https://tex.stackexchange.com/questions/40492/what-are-the-differences-between-align-equation-and-displaymath – Cragfelt Nov 22 '17 at 09:40
  • Another ones https://tex.stackexchange.com/questions/95402/what-is-the-difference-between-aligned-in-displayed-mode-and-starred-align, among others, https://tex.stackexchange.com/questions/401201/difference-between-align-and-alignedt... among others – Cragfelt Nov 22 '17 at 09:41
  • @LoneLearner Someone has downvoted my answer and Flavs's too. I've just noticed that your question has been marked as duplicate as well. – Cragfelt Nov 23 '17 at 16:05