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. Commented Nov 22, 2017 at 2:32
  • It also works with $\begin{array}{c} a^2 \\ b^2 \end{array}$ Commented 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. Commented 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. Commented Nov 22, 2017 at 9:49
  • Did you find a solution after the answers posted or links? Commented Dec 20, 2017 at 8:30

2 Answers 2

3

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. Commented 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
  • Do you know the answer to the two questions I have asked in my question? Commented Nov 22, 2017 at 8: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. tex.stackexchange.com/questions/4736/…, tex.stackexchange.com/questions/40492/… Commented Nov 22, 2017 at 9:40
  • Another ones tex.stackexchange.com/questions/95402/…, among others, tex.stackexchange.com/questions/401201/…... among others Commented Nov 22, 2017 at 9: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. Commented Nov 23, 2017 at 16:05

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.